By trusktr


2012-06-03 02:37:26 8 Comments

Is there some way to get the $handle for each script that has been enqueued?

Is there some array that holds all the handles so that I can loop through it and do something using each $handle?

2 comments

@Josh Habdas 2017-06-07 09:37:49

Is there some way to get the $handle for each script that has been enqueued?

You could try inspecting $wp_scripts->queue at a specific hook but it won't give you a list of all handles used by WordPress, believe it or not.

For example, you could hook into wp_head, which runs the wp_print_scripts action, to get a list of $handles for the stock Twenty Seventeen theme in WP v4.7.5:

function get_enqueued_scripts () {
    $scripts = wp_scripts();
    var_dump( array_keys( $scripts->groups ) );
}

add_action( 'wp_head', 'get_enqueued_scripts' );

And the list of $handles from $wp_scripts->groups will output:

enter image description here

At this point, if you were to compare what exists in $wp_scripts->queue you will only see a subset of the above.

Therefore, even wp_print_scripts will not provide a full list of handles as shown above, if that's what you're after. And it's not possible to always rely on grouped dependencies to get them either.

@Milo 2012-06-03 03:04:58

the $wp_scripts global holds all the script data:

function wpa54064_inspect_scripts() {
    global $wp_scripts;
    foreach( $wp_scripts->queue as $handle ) :
        echo $handle;
    endforeach;
}
add_action( 'wp_print_scripts', 'wpa54064_inspect_scripts' );

@jgraup 2016-08-30 14:33:49

As of WP 4.2.0 - $wp_scripts = wp_scripts(); might be better than accessing the global directly since it will initialize $wp_scripts if it has not been set.

@Ryszard Jędraszyk 2019-04-06 14:43:04

You should add priority ,9999 like add_action( 'wp_print_scripts', wpa54064_inspect_scripts', 9999 );, else you will be seeing scripts which are dequeued by functions.php file

Related Questions

Sponsored Content

1 Answered Questions

1 Answered Questions

1 Answered Questions

How do you output enqueued scripts to an admin page?

1 Answered Questions

[SOLVED] Get only enqueued styles and scripts of the current theme

2 Answered Questions

4 Answered Questions

[SOLVED] How to use Head JS with all enqueued scripts?

1 Answered Questions

Sponsored Content