8

I would like to know how to get the list of scripts enqueued in wordpress via wp_enqueue_script. I have done some research and checked the wp core itself but the closest I can get is something like:

add_options_page('Wp Sysinfo', 'Wp Sysinfo', 'manage_options', 'wp-sysinfo', 'sysinfo_page');

function sysinfo_page(){
    global $wp_scripts;
    print_r($wp_scripts->queue);
}

However it only show scripts in the admin pages not the front end.

FYI: Im building a plugin to display system information in wordpress. This is to provide plugin/theme authors useful info to troubleshoot problems reported by users.

Edit:

In short, I need a way to get all scripts and styles enqueued in both admin and frontend, and view them within a custom admin page.

kosinix
  • 1,779
  • 1
  • 18
  • 22
  • 3
    You're looking at the script queue from the context of an admin page, so it makes sense that the `WP_Scripts` object would contain only those scripts. In addition, which scripts are enqueued "on the front-end" will depend on which post, page, etc so it's not like there's one answer anyway. – Jeff-Meadows Mar 02 '13 at 00:58
  • @jeff. yes i am in the admin, it needs to be there. however I need a way to get the list of scripts loaded in the front end too. – kosinix Mar 03 '13 at 13:06
  • @kosinix, did you ever find an answer to this? I'm having the same problem – Jeremiah Prummer Sep 22 '14 at 03:40
  • But where's the call to `my_enqueued_scripts`? – brasofilo Sep 24 '14 at 23:48
  • @brasofilo - corrected. should be sysinfo_page. call's coming from add_options_page – kosinix Sep 25 '14 at 00:05

2 Answers2

6

There is a dedicated Wordpress action hook for styles as well as scripts. These hooks will be triggered after every script or style is enqueued:

function inspect_scripts() {
    global $wp_scripts;
    print_r($wp_scripts->queue);
}
add_action( 'wp_print_scripts', 'inspect_scripts' );

function inspect_styles() {
    global $wp_styles;
    print_r($wp_styles->queue);
}
add_action( 'wp_print_styles', 'inspect_styles' );
  • Similar example to different question: https://wordpress.stackexchange.com/a/54066/117731 – vhs Jun 07 '17 at 09:19
  • You should add priority `,9999` like `add_action( 'wp_print_scripts', inspect_scripts', 9999 );`, else you will be seeing scripts which are dequeued by functions.php file – Ryszard Jędraszyk Apr 06 '19 at 14:43
0

When I did <?php global $wp_scripts; var_dump($wp_scripts->queue); ?> on the index.php page of the standard 2014 Wordpress theme it showed me non-admin scripts that were being queued. If you just do var_dump on the $wp_scripts variable itself you'll get lots of scripts, I am guessing all the scripts in the theme or something like that.

So if you are only showing this info to the site administrator then it should be fine just to echo it out like at the bottom of the page. I used a memory profiling plugin once (it showed me load times/memory loads used by various plugins) and it put that information at the bottom of the page, it was pretty useful.

But like the comments say if you are only displaying the queued scripts then in the wp-admin area that is only the admin scripts, on the front end you'll get more.

Todd Nestor
  • 136
  • 4