0

The block of code below is supposed to generate a list of dependencies for a given handle e.g. jquery.

function get_deps() {
    global $wp_scripts;
    $wp_scripts->all_deps( 'jquery' );
    $output = [];    // Initialize an empty array
    foreach( $wp_scripts->to_do as $handle ) {
        $dep = wp_scripts()->registered[ $handle ];
        $output [] = $dep->src;
    }
    $result = $output;
    return $result;
}

The function below is a quick test of the get_deps function to check if it spits out the expected list of dependencies.

function display_scripts() {
    print_r( get_deps() ) ;
}
add_action( 'wp_enqueue_scripts', 'display_scripts',  PHP_INT_MAX );

This works as expected however if print_r( get_deps() ) ; is called with the script_loader_tag filter, it produces a unexpected list.

As a quick test, if I replace jquery with lazysizes which has no dependencies like so:

function get_deps() {
    global $wp_scripts;
    $wp_scripts->all_deps( 'lazysizes' );
    $output = [];    // Initialize an empty array
    foreach( $wp_scripts->to_do as $handle ) {
        $dep = wp_scripts()->registered[ $handle ];
        $output [] = $dep->src;
    }
    $result = $output;
    return $result;
}

And quickly validate it with:

function display_scripts() {
    print_r( get_deps() ) ;
}
add_action( 'wp_enqueue_scripts', 'display_scripts',  PHP_INT_MAX );

The output is: Array ( ). So far so good. Now if print_r( get_deps() ) ; included in a block of code with the script_loader_tag filter, the results are completely different. Here's the test:

function test() {
    print_r( get_deps() ) ;
}
add_filter( 'script_loader_tag', 'test', 10 );

which produces:

Array
(
    [0] => /wp-includes/js/jquery/jquery.min.js
    [1] => /wp-includes/js/jquery/jquery-migrate.min.js
    [2] => 
    [3] => https://domain.com/wp-content/themes/theme-name/js/plugins.js
    [4] => https://domain.com/wp-content/themes/theme-name/js/script.js
)
Array
(
    [0] => /wp-includes/js/jquery/jquery-migrate.min.js
    [1] => 
    [2] => https://domain.com/wp-content/themes/theme-name/js/plugins.js
    [3] => https://domain.com/wp-content/themes/theme-name/js/script.js
)

I've no idea why it does this since it should be Array(). What am I doing wrong or overlooking?

1
  • wp_enqueue_scripts and the script_loader_tag are two completely different WordPress functions. What is it, you are trying to achieve? what's the end goal?
    – Lewis
    Sep 12, 2022 at 10:02

1 Answer 1

0

I think the for each loop is a little wonky, if I'm understanding your question, you can try the following.👌

// Generate a list of dependencies for a given handle.
function get_deps() {
    global $wp_scripts;

    $deps = array();
    foreach ( $wp_scripts->registered as $handle => $script ) {
        if ( in_array( 'jquery', $script->deps, true ) ) {
            $deps[] = $handle;
        }
    }

    echo '<pre>';
    print_r( $deps );
    echo '</pre>';
}

Would output the following;

Array
(
    [0] => common
    [1] => editor
    [2] => wp-ajax-response
    [3] => wp-api-request
    [4] => heartbeat
    [5] => jquery-ui-core
    [6] => jquery-effects-core
    [7] => jquery-form
    [8] => jquery-color
    [9] => schedule
    [10] => etc...

)

Edit: Added for completeness.

// Generate a list of dependencies for a given handle.
function get_deps() {
    global $wp_scripts;

    $deps = array();
    foreach ( $wp_scripts->registered as $handle => $script ) {
        if ( in_array( 'jquery-migrate', $script->deps, true ) ) {
            $deps[] = $handle;
        }
    }
    
    return $deps;
}

function foo_enqueue_scripts() {
    $the_deps = get_deps();
    
    echo '<pre>';
    print_r( $the_deps );
    echo '</pre>';
    
    /* output from print_r:
     * Array
     * (
     *     [0] => jquery
     * )
     */
    
    wp_enqueue_script( 'my-foo-js', get_stylesheet_directory_uri() . '/foo.js', $the_deps, '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'foo_enqueue_scripts' );
4
  • that's doesn't return any output.
    – Motivated
    Sep 12, 2022 at 6:32
  • It does for me; I'm not understanding your question. 🤔
    – Lewis
    Sep 12, 2022 at 10:04
  • Are you using a particular hook to return the output?
    – Motivated
    Sep 12, 2022 at 17:26
  • No just the, bog standard wp_enqueue_script() hook. (See updated answer.)
    – Lewis
    Sep 13, 2022 at 1:32

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.