I want to show what scripts and styles have loaded on a page and which plugin / theme / file has added them to the page.

We can show all scripts and styles using:

<?php
  global $wp_scripts, $wp_styles;
  var_dump( $wp_scripts );
  var_dump( $wp_styles );
?>

Or more elegantly using this answer: How do I get the $handle for all enqueued scripts?

I now want to be able to see where they were enqueued. For example if I had a JS file called foobar.js and it was enqueued by plugins/organge/orange.php I would want to output this file location along with the script.

This is for development to help isolate what is loading what.

up vote 4 down vote accepted

This is not possible the way you think. It would maybe be possible if you use Reflections or debug_backtrace(), but there's no reliable way to do this. WordPress does not keep a stack or queue where it tracks file names.

The only thing I could imagine is just hooking into the action and inside wp_enqueue_scripts():

wp_enqueue_scripts

and attach a tracking mechanism of attached callbacks there. Note the s and that this is not the function that you use to attach scripts to the stack.

<?php
/** Plugin Name: (WPSE #152658) Script Loader Callback Inspector */
add_action( 'wp_enqueue_scripts', function()
{
    var_dump( $GLOBALS['wp_filter'][ current_filter() ] );
}, PHP_INT_MAX -1 );

This would leave you with a queue of all attached callbacks. You can then trace them back with your IDE.

  • kaiser, just to point out, your debug_backtrace() link is in German. – Pieter Goosen Jul 4 '14 at 14:45
  • @PieterGoosen That was not by accident. German simply is more accurate. ... fixed – kaiser Jul 4 '14 at 14:47
  • LOL OK. Unfortunately I don't understand German, so I'm not aware of that :-) – Pieter Goosen Jul 4 '14 at 14:51

I'm using something like this

 foreach( $wp_scripts->queue as $handle ) :
         $obj = $wp_scripts->registered [$handle];
         $filename = $obj->src;
  • 1
    with ref to the orig question - this would list the js file e.g. foobar.js. The OP wants to list the php file that enqueued foobar.js - e.g. plugins/organge/orange.php, which your code doesn't list – Nat Nov 24 '16 at 6:13

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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