Home WordPress General Functions wp_enqueue_script() WordPress wp_enqueue_script() Function Definition In WordPress, wp_enqueue_script() adds a JavaScript file to your pages. WordPress prints the <script> tag for you, makes sure dependencies load first, and never loads the same script twice.
Always call it inside a function hooked to wp_enqueue_scripts. Do not write <script> tags by hand in header.php.
Syntax Hook a function to wp_enqueue_scripts and enqueue the script inside it.
<?php
function mytheme_scripts() {
wp_enqueue_script(
'mytheme-app',
get_template_directory_uri() . '/js/app.js',
array(),
'1.0.0',
true
);
}
add_action( 'wp_enqueue_scripts', 'mytheme_scripts' ); Call What it does wp_enqueue_script( $handle, $src )Adds a script file with a unique name. wp_enqueue_script( $handle, $src, array( 'jquery' ) )Loads the script after jQuery. wp_enqueue_script( $handle, $src, array(), '1.0', true )Loads the script in the footer.
For CSS, use wp_enqueue_style() the same way.
Full declaration wp_enqueue_script( string $handle, string $src = '', string[] $deps = [], string|bool|null $ver = false, array|bool $args = [] ): void Parameters Parameter Description $handlestring. A unique name for the script, such as mytheme-app. $srcstring. The full URL of the file. Leave it empty for scripts WordPress already knows, such as jquery. Default is empty. $depsarray. Handles of scripts that must load first. Default is an empty array. $verstring|bool|null. Version added to the URL as ?ver=. false (default) uses the WordPress version. null adds nothing. $argsarray|bool. Pass true to load in the footer. Or pass an array such as array( 'strategy' => 'defer', 'in_footer' => true ). Default is an empty array.
Return Value Return Value Description void Nothing is returned. The script is added to the queue.
WordPress Examples Three short theme examples. Each one shows the code and what it prints.
1. Add a script in the footer 2. Load after jQuery 3. Use defer Example 2 — Load after jQuery Tell WordPress your script needs jQuery.
<?php
function mytheme_slider_script() {
wp_enqueue_script(
'mytheme-slider',
get_template_directory_uri() . '/js/slider.js',
array( 'jquery' ),
'1.0.0',
true
);
}
add_action( 'wp_enqueue_scripts', 'mytheme_slider_script' ); <script src=".../wp-includes/js/jquery/jquery.min.js" id="jquery-core-js"></script>
<script src=".../themes/mytheme/js/slider.js?ver=1.0.0" id="mytheme-slider-js"></script> How It Works array( 'jquery' ) lists the dependency.WordPress prints jQuery first, then your file. You do not need to add jQuery yourself. Example 3 — Use defer Load the script without blocking the page (WordPress 6.3 and later).
<?php
function mytheme_defer_script() {
wp_enqueue_script(
'mytheme-extra',
get_template_directory_uri() . '/js/extra.js',
array(),
'1.0.0',
array(
'strategy' => 'defer',
'in_footer' => true,
)
);
}
add_action( 'wp_enqueue_scripts', 'mytheme_defer_script' ); <script src=".../js/extra.js?ver=1.0.0" id="mytheme-extra-js" defer data-wp-strategy="defer"></script> How It Works The last argument is an array instead of true. strategy can be defer or async.in_footer keeps the script in the footer.Frequently Asked Questions What does wp_enqueue_script() do? It adds a JavaScript file to the page queue. WordPress prints the script tag and handles dependencies and duplicates.
Which hook should I use? Use wp_enqueue_scripts for the front end and admin_enqueue_scripts for the dashboard.
Why not add a script tag in header.php? A hand-written tag skips the queue. It can load twice, load in the wrong order, and be hard for plugins to manage.
What can the last argument be? true loads the script in the footer. In WordPress 6.3 and later you can pass an array with strategy (defer or async) and in_footer.
What is the handle? It is a unique name for the script. Other scripts use it in their dependency list.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
11 people found this page helpful
Helpful Share Copy link Suggestion