Could we help you? Please click the banners. We are young and desperately need the money
Whether you're building a lightweight WordPress theme or a full-featured plugin, properly loading scripts and styles is critical. Done wrong, you could introduce duplicate assets, slow down page loads, or break things entirely. In this guide, we'll walk through the fundamentals—and the advanced nuances—of how to enqueue scripts and styles the right way in WordPress.
Let’s get this out of the way: you should never manually add JavaScript or CSS via <script> or <link> tags in your theme’s header.php or footer.php files.
Instead, use WordPress's built-in "wp_enqueue_script()" and "wp_enqueue_style()" functions. These handle things like:
function mytheme_enqueue_assets() {
wp_enqueue_style('mytheme-style', get_stylesheet_uri());
wp_enqueue_script(
'mytheme-main-js',
get_template_directory_uri() . '/assets/js/main.js',
array('jquery'),
'1.0',
true // load in footer
);
}
add_action('wp_enqueue_scripts', 'mytheme_enqueue_assets');
Output:
<link rel="stylesheet" id="mytheme-style-css" href="https://example.com/wp-content/themes/your-theme/style.css" type="text/css" media="all" />
<script type="text/javascript" src="https://example.com/wp-content/themes/your-theme/assets/js/main.js" id="mytheme-main-js-js"></script>
Never load everything everywhere. Example: Load a script only on single post pages:
function enqueue_comment_script() {
if (is_single()) {
wp_enqueue_script('comment-reply');
}
}
add_action('wp_enqueue_scripts', 'enqueue_comment_script');
This pattern is useful when your script might be loaded conditionally later:
function register_custom_scripts() {
wp_register_script('my-analytics', 'https://example.com/js/analytics.js', array(), '2.0', true);
if (is_page('thank-you')) {
wp_enqueue_script('my-analytics');
}
}
add_action('wp_enqueue_scripts', 'register_custom_scripts');
In a plugin, use:
function plugin_styles_and_scripts() {
wp_enqueue_style(
'myplugin-css',
plugin_dir_url(__FILE__) . 'css/plugin-style.css',
array(),
'1.1'
);
wp_enqueue_script(
'myplugin-js',
plugin_dir_url(__FILE__) . 'js/plugin-script.js',
array('jquery'),
'1.1',
true
);
}
add_action('wp_enqueue_scripts', 'plugin_styles_and_scripts');
Want to remove a script loaded by a plugin or theme? Use wp_dequeue_script() and wp_deregister_script().
function remove_extra_scripts() {
wp_dequeue_script('some-plugin-script');
wp_deregister_script('some-plugin-script');
}
add_action('wp_enqueue_scripts', 'remove_extra_scripts', 100);
Tip: Use high priority like 100 to ensure your code runs after the original enqueue.
Let WordPress handle script order using dependencies:
wp_enqueue_script('custom-script', 'path/to/script.js', array('jquery', 'wp-element'), null, true);
WordPress ensures: jquery and wp-element are loaded before custom-script.
Need to pass PHP data into JavaScript? Use wp_localize_script():
function localize_script_example() {
wp_enqueue_script('localized-js', get_template_directory_uri() . '/js/localized.js', array(), null, true);
wp_localize_script('localized-js', 'MyData', array(
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('my_nonce')
));
}
add_action('wp_enqueue_scripts', 'localize_script_example');
// localized.js
console.log(MyData.ajax_url); // Outputs: https://example.com/wp-admin/admin-ajax.php
If you're creating settings pages or custom admin UI, load your scripts with:
function admin_enqueue_scripts($hook) {
if ($hook === 'toplevel_page_myplugin-settings') {
wp_enqueue_style('admin-style', plugin_dir_url(__FILE__) . 'css/admin.css');
wp_enqueue_script('admin-js', plugin_dir_url(__FILE__) . 'js/admin.js', array('jquery'), null, true);
}
}
add_action('admin_enqueue_scripts', 'admin_enqueue_scripts');
Hint: The $hook value can be inspected with "error_log($hook)"; to know what to target.
Enqueuing scripts and styles in WordPress is more than just calling a function—it's a key part of proper WordPress development. Whether you're building a theme, a plugin, or an enterprise-level project, understanding how and when to enqueue, conditionally load, deregister, and localize scripts will keep your code performant, future-proof, and maintainable.
Start with the basics, but don't be afraid to use the advanced techniques shown here to make your scripts smarter and cleaner.