0

Hi I have a js script that needs to be included after a specific js script.

function wpb_adding_scripts() {
wp_register_script('egovt-add-js',get_stylesheet_directory_uri(). '/add.js','','1.1', true);
wp_enqueue_script('egovt-add-js');
}
add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' );

This add.js file is included, but not in the positions that I need, I need it to be after another js file, named first.js (for ex.)

1 Answer 1

2

That's part of the default functionality available in wp_enqueue_script/wp_register_script.
The third argument, as seen in the official documentation of enqueue/register script, https://developer.wordpress.org/reference/functions/wp_enqueue_script/, https://developer.wordpress.org/reference/functions/wp_register_script/, is the dependencies array.

$deps string[] Optional
An array of registered script handles this script depends on. Default: array()

So lets say you have script with the handle "sliderjs", using your code it would look like this

function wpb_adding_scripts() {
    wp_register_script('egovt-add-js', get_stylesheet_directory_uri() . '/add.js', ['sliderjs'],'1.1', true);
    wp_enqueue_script('egovt-add-js');
}
add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' );

// A multi line arguments example
/*
wp_register_script(
    'egovt-add-js',
    get_stylesheet_directory_uri() . '/add.js',
    ['sliderjs'], // <- this is the dependencies arrray
    '1.1',
    true
);
*/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.