Mahamudul Hasan Rubel
HomeAboutProjectsSkillsExperienceBlogPhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • About
  • Projects
  • Skills
  • Experience
  • Blog
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
WordPressJune 20, 20264 min read

Enqueuing scripts and styles the correct way in WordPress

Enqueuing scripts and styles the correct way is essential for site performance. Learn to manage assets using wp_enqueue_scripts to avoid conflicts.

WordPressPHPWeb DevelopmentFrontendPerformanceTutorial
Close-up of a vintage typewriter with paper labeled Wordpress.

I remember my early days as a WordPress developer, back when I thought header.php was the perfect dumping ground for every <script> and <link> tag I needed. I’d manually add jQuery, a slider library, and custom CSS files directly into the template. It worked until it didn’t. The moment I installed a plugin that also loaded a different version of jQuery, my site turned into a broken mess of JavaScript console errors.

If you’re still hardcoding assets, you’re missing out on the core dependency management system that makes WordPress robust.

Why Enqueuing Scripts and Styles the Correct Way Matters

The WordPress enqueue system is a central registry for all your assets. When you use wp_enqueue_script() and wp_enqueue_style(), you aren't just telling the browser to load a file. You are telling WordPress: "This file exists, it depends on these other files, and it should load at this specific point."

This allows WordPress to handle dependencies automatically. If you tell WordPress that your custom script depends on jQuery, it ensures jQuery loads first. It also prevents duplicate loads if multiple plugins try to load the same library.

The Basic Implementation

To start, you need to hook into wp_enqueue_scripts. Never call these functions directly in your template files; always wrap them in a function and hook it.

PHP
function my_theme_enqueue_assets() {
    #6A9955">// Enqueue a CSS file
    wp_enqueue_style(
        'my-theme-main', 
        get_template_directory_uri() . '/css/main.css', 
        [], 
        '1.0.0'
    );

    #6A9955">// Enqueue a JS file
    wp_enqueue_script(
        'my-theme-scripts', 
        get_template_directory_uri() . '/js/scripts.js', 
        ['jquery'], 
        '1.0.0', 
        true #6A9955">// Loads in the footer
    );
}
add_action('wp_enqueue_scripts', 'my_theme_enqueue_assets');

That true at the end of the wp_enqueue_script call is crucial. It tells WordPress to print the script in the footer, which is a massive win for your site's performance metrics, especially when you're working on INP explained and how to actually improve it in production.

Handling Dependencies and Versioning

Close-up of software development tools displaying code and version control systems on a computer monitor.

One common mistake I see is skipping the version argument. Always pass a version string (like '1.0.0') or, better yet, the file modification time. Using filemtime() ensures the browser cache breaks whenever you update your source code.

PHP
wp_enqueue_style(
    'my-theme-main', 
    get_template_directory_uri() . '/css/main.css', 
    [], 
    filemtime(get_template_directory() . '/css/main.css')
);

This simple trick saves me about two days of "why aren't my CSS changes showing up?" headaches every year. It’s a small detail, but it’s the difference between a amateur setup and a professional workflow.

When Things Go Wrong

We once tried to optimize a site by manually dequeuing core scripts to save a few kilobytes. It broke the block editor entirely because we didn't account for the complex dependency chain WordPress maintains. When you're managing complex systems, you have to respect the registry.

If you are building custom functionality, you might be tempted to load your assets everywhere. Don't. Use conditional tags like is_page() or is_singular() to load your heavy JS files only when needed. If you're building out data structures, you might find my guide on Custom post types without a plugin: A Developer’s Guide helpful for keeping your theme lean.

Frequently Asked Questions

Close-up of a magnifying glass focusing on the phrase 'Frequently Asked Questions'.

Why should I use wp_enqueue_scripts instead of just adding a link tag? Because other developers and plugins won't know your file is already loaded. This leads to script conflicts, duplicated libraries, and broken functionality.

Can I load scripts in the header? Yes, but only if they are absolutely required for the initial render, like critical CSS or specialized font loading. Everything else should be deferred to the footer.

What happens if I forget the dependency array? If your script relies on jQuery but you leave the array empty, WordPress will load your script as soon as it's registered. If the browser hasn't downloaded jQuery yet, your script will throw a $ is not defined error.

I'm still occasionally tripped up by complex plugin conflicts where two different versions of a library battle for control. When that happens, I usually look at the network tab in my browser to see who is calling what. It’s a messy process, but mastering the enqueue system is the only way to keep your environment predictable. Keep your hooks clean, use filemtime for cache busting, and always declare your dependencies.

Back to Blog

Similar Posts

Focused view of programming code displayed on a laptop, ideal for tech and coding themes.
WordPressJune 20, 20264 min read

Debugging the WordPress white screen of death: A Pro Guide

Debugging the WordPress white screen of death is easier when you stop guessing. Learn to enable debug logs, trace errors, and restore your site in minutes.

Read more
Close view of computer screen displaying HTML code with an authentication error.
WordPress
June 20, 2026
4 min read

Custom post types without a plugin: A Developer’s Guide

Learn to create custom post types without a plugin using native WordPress functions. Control your data structure and keep your site lightweight and fast.

Read more
Creative flat lay of gadgets and 'Beginners Guide' text on wooden table.
WordPressJune 20, 20264 min read

Your first WordPress hook: A beginner’s guide to customization

Your first WordPress hook is the key to modifying site behavior safely. Learn how to use actions and filters to extend functionality without touching core.

Read more