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.

Similar Posts