0

I'm developing a plugin that will allow some CSS optimization. But first of all, i need to get the list of all stylesheets (later i'll do something for scripts too).

Doing this:

function debug_print_styles () {
    global $wp_styles;
    ?>
    <h2>Debug print styles</h2>
        <?php
            foreach ($wp_styles->queue as $handle) {
                echo $handle."<br />";
            }
        ?>
    <?php
}

clearly works as intended if used on a post/page template. But if i want that list on an admin settings page, this won't fit, as it prints only the styles for the admin page. I looked at many forums, including this one, but found the same thing i already did.

So, is there a way to achieve this? Thank you.

2

So, is there a way to achieve this?

Indeed, there is - I'm doing it in my own optimization plugin - but it's going to involve more work than you might first suspect.

  1. Use add_action() to register a hook that triggers on the front-end to output a JSON-encoded array of registered styles
  2. Use the HTTP API (or an AJAX call) to make a request against the front-end to trigger that hook
  3. Read the results of your HTTP API call from your /wp-admin/ script

There are a number of additional considerations if you want to cover all your bases here, but that should be everything you need to get started.

2
  • I was able to get it this way, so i'm making this accepted answer. This result can be achieved with buffers too, so i'll post an answer myself later, to cover both ways – Daniele Squalo Sep 9 '18 at 11:57
  • Buffering wp_head and wp_footer contents gives some results, but they're not the same. HTTP API is definitely the way. – Daniele Squalo Sep 9 '18 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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