wp_enqueue_script

The timeline below displays how wordpress function wp_enqueue_script has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.4

/**
 * Enqueues a script.
 *
 * Registers the script if `$src` provided (does NOT overwrite), and enqueues it.
 *
 * @see WP_Dependencies::add()
 * @see WP_Dependencies::add_data()
 * @see WP_Dependencies::enqueue()
 *
 * @since 2.1.0
 * @since 6.3.0 The $in_footer parameter of type boolean was overloaded to be an $args parameter of type array.
 *
 * @param string           $handle    Name of the script. Should be unique.
 * @param string           $src       Full URL of the script, or path of the script relative to the WordPress root directory.
 *                                    Default empty.
 * @param string[]         $deps      Optional. An array of registered script handles this script depends on. Default empty array.
 * @param string|bool|null $ver       Optional. String specifying script version number, if it has one, which is added to the URL
 *                                    as a query string for cache busting purposes. If version is set to false, a version
 *                                    number is automatically added equal to current installed WordPress version.
 *                                    If set to null, no version is added.
 * @param array|bool       $args     {
 *     Optional. An array of additional script loading strategies. Default empty array.
 *     Otherwise, it may be a boolean in which case it determines whether the script is printed in the footer. Default false.
 *
 *     @type string    $strategy     Optional. If provided, may be either 'defer' or 'async'.
 *     @type bool      $in_footer    Optional. Whether to print the script in the footer. Default 'false'.
 * }
 */
function wp_enqueue_script($handle, $src = '', $deps = array(), $ver = false, $args = array())
{
    _wp_scripts_maybe_doing_it_wrong(__FUNCTION__, $handle);
    $wp_scripts = wp_scripts();
    if ($src || !empty($args)) {
        $_handle = explode('?', $handle);
        if (!is_array($args)) {
            $args = array('in_footer' => (bool) $args);
        }
        if ($src) {
            $wp_scripts->add($_handle[0], $src, $deps, $ver);
        }
        if (!empty($args['in_footer'])) {
            $wp_scripts->add_data($_handle[0], 'group', 1);
        }
        if (!empty($args['strategy'])) {
            $wp_scripts->add_data($_handle[0], 'strategy', $args['strategy']);
        }
    }
    $wp_scripts->enqueue($handle);
}

WordPress Version: 6.3

/**
 * Enqueues a script.
 *
 * Registers the script if $src provided (does NOT overwrite), and enqueues it.
 *
 * @see WP_Dependencies::add()
 * @see WP_Dependencies::add_data()
 * @see WP_Dependencies::enqueue()
 *
 * @since 2.1.0
 * @since 6.3.0 The $in_footer parameter of type boolean was overloaded to be an $args parameter of type array.
 *
 * @param string           $handle    Name of the script. Should be unique.
 * @param string           $src       Full URL of the script, or path of the script relative to the WordPress root directory.
 *                                    Default empty.
 * @param string[]         $deps      Optional. An array of registered script handles this script depends on. Default empty array.
 * @param string|bool|null $ver       Optional. String specifying script version number, if it has one, which is added to the URL
 *                                    as a query string for cache busting purposes. If version is set to false, a version
 *                                    number is automatically added equal to current installed WordPress version.
 *                                    If set to null, no version is added.
 * @param array|bool       $args     {
 *      Optional. An array of additional script loading strategies. Default empty array.
 *      Otherwise, it may be a boolean in which case it determines whether the script is printed in the footer. Default false.
 *
 *      @type string    $strategy     Optional. If provided, may be either 'defer' or 'async'.
 *      @type bool      $in_footer    Optional. Whether to print the script in the footer. Default 'false'.
 * }
 */
function wp_enqueue_script($handle, $src = '', $deps = array(), $ver = false, $args = array())
{
    _wp_scripts_maybe_doing_it_wrong(__FUNCTION__, $handle);
    $wp_scripts = wp_scripts();
    if ($src || !empty($args)) {
        $_handle = explode('?', $handle);
        if (!is_array($args)) {
            $args = array('in_footer' => (bool) $args);
        }
        if ($src) {
            $wp_scripts->add($_handle[0], $src, $deps, $ver);
        }
        if (!empty($args['in_footer'])) {
            $wp_scripts->add_data($_handle[0], 'group', 1);
        }
        if (!empty($args['strategy'])) {
            $wp_scripts->add_data($_handle[0], 'strategy', $args['strategy']);
        }
    }
    $wp_scripts->enqueue($handle);
}

WordPress Version: 6.2

/**
 * Enqueues a script.
 *
 * Registers the script if $src provided (does NOT overwrite), and enqueues it.
 *
 * @see WP_Dependencies::add()
 * @see WP_Dependencies::add_data()
 * @see WP_Dependencies::enqueue()
 *
 * @since 2.1.0
 *
 * @param string           $handle    Name of the script. Should be unique.
 * @param string           $src       Full URL of the script, or path of the script relative to the WordPress root directory.
 *                                    Default empty.
 * @param string[]         $deps      Optional. An array of registered script handles this script depends on. Default empty array.
 * @param string|bool|null $ver       Optional. String specifying script version number, if it has one, which is added to the URL
 *                                    as a query string for cache busting purposes. If version is set to false, a version
 *                                    number is automatically added equal to current installed WordPress version.
 *                                    If set to null, no version is added.
 * @param bool             $in_footer Optional. Whether to enqueue the script before `</body>` instead of in the `<head>`.
 *                                    Default 'false'.
 */
function wp_enqueue_script($handle, $src = '', $deps = array(), $ver = false, $in_footer = false)
{
    _wp_scripts_maybe_doing_it_wrong(__FUNCTION__, $handle);
    $wp_scripts = wp_scripts();
    if ($src || $in_footer) {
        $_handle = explode('?', $handle);
        if ($src) {
            $wp_scripts->add($_handle[0], $src, $deps, $ver);
        }
        if ($in_footer) {
            $wp_scripts->add_data($_handle[0], 'group', 1);
        }
    }
    $wp_scripts->enqueue($handle);
}

WordPress Version: 6.1

/**
 * Enqueue a script.
 *
 * Registers the script if $src provided (does NOT overwrite), and enqueues it.
 *
 * @see WP_Dependencies::add()
 * @see WP_Dependencies::add_data()
 * @see WP_Dependencies::enqueue()
 *
 * @since 2.1.0
 *
 * @param string           $handle    Name of the script. Should be unique.
 * @param string           $src       Full URL of the script, or path of the script relative to the WordPress root directory.
 *                                    Default empty.
 * @param string[]         $deps      Optional. An array of registered script handles this script depends on. Default empty array.
 * @param string|bool|null $ver       Optional. String specifying script version number, if it has one, which is added to the URL
 *                                    as a query string for cache busting purposes. If version is set to false, a version
 *                                    number is automatically added equal to current installed WordPress version.
 *                                    If set to null, no version is added.
 * @param bool             $in_footer Optional. Whether to enqueue the script before `</body>` instead of in the `<head>`.
 *                                    Default 'false'.
 */
function wp_enqueue_script($handle, $src = '', $deps = array(), $ver = false, $in_footer = false)
{
    _wp_scripts_maybe_doing_it_wrong(__FUNCTION__, $handle);
    $wp_scripts = wp_scripts();
    if ($src || $in_footer) {
        $_handle = explode('?', $handle);
        if ($src) {
            $wp_scripts->add($_handle[0], $src, $deps, $ver);
        }
        if ($in_footer) {
            $wp_scripts->add_data($_handle[0], 'group', 1);
        }
    }
    $wp_scripts->enqueue($handle);
}

WordPress Version: 5.5

/**
 * Enqueue a script.
 *
 * Registers the script if $src provided (does NOT overwrite), and enqueues it.
 *
 * @see WP_Dependencies::add()
 * @see WP_Dependencies::add_data()
 * @see WP_Dependencies::enqueue()
 *
 * @since 2.1.0
 *
 * @param string           $handle    Name of the script. Should be unique.
 * @param string           $src       Full URL of the script, or path of the script relative to the WordPress root directory.
 *                                    Default empty.
 * @param string[]         $deps      Optional. An array of registered script handles this script depends on. Default empty array.
 * @param string|bool|null $ver       Optional. String specifying script version number, if it has one, which is added to the URL
 *                                    as a query string for cache busting purposes. If version is set to false, a version
 *                                    number is automatically added equal to current installed WordPress version.
 *                                    If set to null, no version is added.
 * @param bool             $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
 *                                    Default 'false'.
 */
function wp_enqueue_script($handle, $src = '', $deps = array(), $ver = false, $in_footer = false)
{
    _wp_scripts_maybe_doing_it_wrong(__FUNCTION__, $handle);
    $wp_scripts = wp_scripts();
    if ($src || $in_footer) {
        $_handle = explode('?', $handle);
        if ($src) {
            $wp_scripts->add($_handle[0], $src, $deps, $ver);
        }
        if ($in_footer) {
            $wp_scripts->add_data($_handle[0], 'group', 1);
        }
    }
    $wp_scripts->enqueue($handle);
}

WordPress Version: 5.4

/**
 * Enqueue a script.
 *
 * Registers the script if $src provided (does NOT overwrite), and enqueues it.
 *
 * @see WP_Dependencies::add()
 * @see WP_Dependencies::add_data()
 * @see WP_Dependencies::enqueue()
 *
 * @since 2.1.0
 *
 * @param string           $handle    Name of the script. Should be unique.
 * @param string           $src       Full URL of the script, or path of the script relative to the WordPress root directory.
 *                                    Default empty.
 * @param string[]         $deps      Optional. An array of registered script handles this script depends on. Default empty array.
 * @param string|bool|null $ver       Optional. String specifying script version number, if it has one, which is added to the URL
 *                                    as a query string for cache busting purposes. If version is set to false, a version
 *                                    number is automatically added equal to current installed WordPress version.
 *                                    If set to null, no version is added.
 * @param bool             $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
 *                                    Default 'false'.
 */
function wp_enqueue_script($handle, $src = '', $deps = array(), $ver = false, $in_footer = false)
{
    $wp_scripts = wp_scripts();
    _wp_scripts_maybe_doing_it_wrong(__FUNCTION__);
    if ($src || $in_footer) {
        $_handle = explode('?', $handle);
        if ($src) {
            $wp_scripts->add($_handle[0], $src, $deps, $ver);
        }
        if ($in_footer) {
            $wp_scripts->add_data($_handle[0], 'group', 1);
        }
    }
    $wp_scripts->enqueue($handle);
}

WordPress Version: 4.7

/**
 * Enqueue a script.
 *
 * Registers the script if $src provided (does NOT overwrite), and enqueues it.
 *
 * @see WP_Dependencies::add()
 * @see WP_Dependencies::add_data()
 * @see WP_Dependencies::enqueue()
 *
 * @since 2.1.0
 *
 * @param string           $handle    Name of the script. Should be unique.
 * @param string           $src       Full URL of the script, or path of the script relative to the WordPress root directory.
 *                                    Default empty.
 * @param array            $deps      Optional. An array of registered script handles this script depends on. Default empty array.
 * @param string|bool|null $ver       Optional. String specifying script version number, if it has one, which is added to the URL
 *                                    as a query string for cache busting purposes. If version is set to false, a version
 *                                    number is automatically added equal to current installed WordPress version.
 *                                    If set to null, no version is added.
 * @param bool             $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
 *                                    Default 'false'.
 */
function wp_enqueue_script($handle, $src = '', $deps = array(), $ver = false, $in_footer = false)
{
    $wp_scripts = wp_scripts();
    _wp_scripts_maybe_doing_it_wrong(__FUNCTION__);
    if ($src || $in_footer) {
        $_handle = explode('?', $handle);
        if ($src) {
            $wp_scripts->add($_handle[0], $src, $deps, $ver);
        }
        if ($in_footer) {
            $wp_scripts->add_data($_handle[0], 'group', 1);
        }
    }
    $wp_scripts->enqueue($handle);
}

WordPress Version: 4.5

/**
 * Enqueue a script.
 *
 * Registers the script if $src provided (does NOT overwrite), and enqueues it.
 *
 * @see WP_Dependencies::add()
 * @see WP_Dependencies::add_data()
 * @see WP_Dependencies::enqueue()
 *
 * @since 2.1.0
 *
 * @param string           $handle    Name of the script. Should be unique.
 * @param string           $src       Full URL of the script, or path of the script relative to the WordPress root directory.
 * @param array            $deps      Optional. An array of registered script handles this script depends on. Default empty array.
 * @param string|bool|null $ver       Optional. String specifying script version number, if it has one, which is added to the URL
 *                                    as a query string for cache busting purposes. If version is set to false, a version
 *                                    number is automatically added equal to current installed WordPress version.
 *                                    If set to null, no version is added.
 * @param bool             $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
 *                                    Default 'false'.
 */
function wp_enqueue_script($handle, $src = false, $deps = array(), $ver = false, $in_footer = false)
{
    $wp_scripts = wp_scripts();
    _wp_scripts_maybe_doing_it_wrong(__FUNCTION__);
    if ($src || $in_footer) {
        $_handle = explode('?', $handle);
        if ($src) {
            $wp_scripts->add($_handle[0], $src, $deps, $ver);
        }
        if ($in_footer) {
            $wp_scripts->add_data($_handle[0], 'group', 1);
        }
    }
    $wp_scripts->enqueue($handle);
}

WordPress Version: 4.3

/**
 * Enqueue a script.
 *
 * Registers the script if $src provided (does NOT overwrite), and enqueues it.
 *
 * @see WP_Dependencies::add(), WP_Dependencies::add_data(), WP_Dependencies::enqueue()
 *
 * @since 2.6.0
 *
 * @param string      $handle    Name of the script.
 * @param string|bool $src       Path to the script from the root directory of WordPress. Example: '/js/myscript.js'.
 * @param array       $deps      An array of registered handles this script depends on. Default empty array.
 * @param string|bool $ver       Optional. String specifying the script version number, if it has one. This parameter
 *                               is used to ensure that the correct version is sent to the client regardless of caching,
 *                               and so should be included if a version number is available and makes sense for the script.
 * @param bool        $in_footer Optional. Whether to enqueue the script before </head> or before </body>.
 *                               Default 'false'. Accepts 'false' or 'true'.
 */
function wp_enqueue_script($handle, $src = false, $deps = array(), $ver = false, $in_footer = false)
{
    $wp_scripts = wp_scripts();
    _wp_scripts_maybe_doing_it_wrong(__FUNCTION__);
    if ($src || $in_footer) {
        $_handle = explode('?', $handle);
        if ($src) {
            $wp_scripts->add($_handle[0], $src, $deps, $ver);
        }
        if ($in_footer) {
            $wp_scripts->add_data($_handle[0], 'group', 1);
        }
    }
    $wp_scripts->enqueue($handle);
}

WordPress Version: 4.2

/**
 * Enqueue a script.
 *
 * Registers the script if $src provided (does NOT overwrite), and enqueues it.
 *
 * @see WP_Dependencies::add(), WP_Dependencies::add_data(), WP_Dependencies::enqueue()
 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
 *
 * @since 2.6.0
 *
 * @param string      $handle    Name of the script.
 * @param string|bool $src       Path to the script from the root directory of WordPress. Example: '/js/myscript.js'.
 * @param array       $deps      An array of registered handles this script depends on. Default empty array.
 * @param string|bool $ver       Optional. String specifying the script version number, if it has one. This parameter
 *                               is used to ensure that the correct version is sent to the client regardless of caching,
 *                               and so should be included if a version number is available and makes sense for the script.
 * @param bool        $in_footer Optional. Whether to enqueue the script before </head> or before </body>.
 *                               Default 'false'. Accepts 'false' or 'true'.
 */
function wp_enqueue_script($handle, $src = false, $deps = array(), $ver = false, $in_footer = false)
{
    $wp_scripts = wp_scripts();
    _wp_scripts_maybe_doing_it_wrong(__FUNCTION__);
    if ($src || $in_footer) {
        $_handle = explode('?', $handle);
        if ($src) {
            $wp_scripts->add($_handle[0], $src, $deps, $ver);
        }
        if ($in_footer) {
            $wp_scripts->add_data($_handle[0], 'group', 1);
        }
    }
    $wp_scripts->enqueue($handle);
}

WordPress Version: 3.7

/**
 * Enqueue a script.
 *
 * Registers the script if $src provided (does NOT overwrite), and enqueues it.
 *
 * @see WP_Dependencies::add(), WP_Dependencies::add_data(), WP_Dependencies::enqueue()
 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
 *
 * @since 2.6.0
 * @param string      $handle    Name of the script.
 * @param string|bool $src       Path to the script from the root directory of WordPress. Example: '/js/myscript.js'.
 * @param array       $deps      An array of registered handles this script depends on. Default empty array.
 * @param string|bool $ver       Optional. String specifying the script version number, if it has one. This parameter
 *                               is used to ensure that the correct version is sent to the client regardless of caching,
 *                               and so should be included if a version number is available and makes sense for the script.
 * @param bool        $in_footer Optional. Whether to enqueue the script before </head> or before </body>.
 *                               Default 'false'. Accepts 'false' or 'true'.
 */
function wp_enqueue_script($handle, $src = false, $deps = array(), $ver = false, $in_footer = false)
{
    global $wp_scripts;
    if (!is_a($wp_scripts, 'WP_Scripts')) {
        if (!did_action('init')) {
            _doing_it_wrong(__FUNCTION__, sprintf(__('Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.'), '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>'), '3.3');
        }
        $wp_scripts = new WP_Scripts();
    }
    if ($src) {
        $_handle = explode('?', $handle);
        $wp_scripts->add($_handle[0], $src, $deps, $ver);
        if ($in_footer) {
            $wp_scripts->add_data($_handle[0], 'group', 1);
        }
    }
    $wp_scripts->enqueue($handle);
}