WordPress wp_enqueue_script() Function

Beginner
4 min read
Updated: Nov 2026
3 examples
Script loader

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.

WordPress
<?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' );
CallWhat 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
WordPress
wp_enqueue_script( string $handle, string $src = '', string[] $deps = [], string|bool|null $ver = false, array|bool $args = [] ): void

Parameters

ParameterDescription
$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 ValueDescription
voidNothing is returned. The script is added to the queue.

WordPress Examples

Three short theme examples. Each one shows the code and what it prints.

Example 2 — Load after jQuery

Tell WordPress your script needs jQuery.

WordPress
<?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' );

How It Works

  1. array( 'jquery' ) lists the dependency.
  2. WordPress prints jQuery first, then your file.
  3. You do not need to add jQuery yourself.

Example 3 — Use defer

Load the script without blocking the page (WordPress 6.3 and later).

WordPress
<?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' );

How It Works

  1. The last argument is an array instead of true.
  2. strategy can be defer or async.
  3. in_footer keeps the script in the footer.

Frequently Asked Questions

It adds a JavaScript file to the page queue. WordPress prints the script tag and handles dependencies and duplicates.
Use wp_enqueue_scripts for the front end and admin_enqueue_scripts for the dashboard.
A hand-written tag skips the queue. It can load twice, load in the wrong order, and be hard for plugins to manage.
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.
It is a unique name for the script. Other scripts use it in their dependency list.

Next: wp_get_archives()

Print or return a list of monthly, yearly, daily, or recent post archives.

wp_get_archives() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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