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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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' );
Method 2
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:
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0
