4

I am working on a CSS and JS compiler and need to find a way to list the contents of wp_head()

I am trying to get a list of all CSS/JS files and inline CSS on any give page.

Hooking on the wp_head action does not do anything

I was hoping that something like this would work

function head_content($list){

    print_r($list);


}

add_action('wp_head', 'head_content');

any help is appreciated.

UPDATE:

got something working

function head_content($list){

    print_r($list);

    return $list;

}

add_filter('print_styles_array', 'head_content');
add_filter('print_script_array', 'head_content');

this lists all css/js files handles

  • wp_head is an action, actions don't have data passed to them. it's just an event that other things are hooked to, like wp_enqueue_scripts. – Milo May 15 '16 at 17:43
  • @Milo thnx for the info , is there any way to get the contents of the head tag? – Benn May 15 '16 at 17:55
4

I wanted to search-and-replace in the header, but Neither @majick or @Samuel Elh answers worked for me directly. So, combining their answers I got what eventually works:

function start_wp_head_buffer() {
    ob_start();
}
add_action('wp_head','start_wp_head_buffer',0);

function end_wp_head_buffer() {
    $in = ob_get_clean();

    // here do whatever you want with the header code
    echo $in; // output the result unless you want to remove it
}
add_action('wp_head','end_wp_head_buffer', PHP_INT_MAX); //PHP_INT_MAX will ensure this action is called after all other actions that can modify head

It was added to functions.php of my child theme.

| improve this answer | |
  • you nailed it bud! – Benn Oct 11 '16 at 13:29
  • any particular reason for this 998 ? – Benn Oct 11 '16 at 13:29
  • 1
    @Benn no, I like it more than 999 :) – Putnik Oct 11 '16 at 13:38
3

Simple solution is to listen for wp_head in a custom function, just like WordPress does in wp-includes/general-template.php for wp_head() function.

I mean something like:

function head_content() {
    ob_start();
    do_action('wp_head');
    return ob_get_clean();
}
// contents
var_dump( head_content() );

Later, use regex or other tool to filter the contents you are targeting..

Hope that helps.

| improve this answer | |
  • Use \DOMDocument or another xml parser. Do not use regex. Never, ever use regex on html. – mopsyd May 12 '18 at 19:21
2

You could buffer the wp_head output by adding some wrapper actions to it:

add_action('wp_head','start_wp_head_buffer',0);
function start_wp_head_buffer() {ob_start;}
add_action('wp_head','end_wp_head_buffer',99);
function end_wp_head_buffer() {global $wpheadcontents; $wpheadcontents = ob_get_flush();}

You can then call global $wpheadcontents; elsewhere to access the content and process it.

But, in this case, it may be simpler to just get the information you are looking for directly from the global $wp_styles and $wp_scripts variables.

function print_global_arrays() {
    global $wp_styles, $wp_scripts;
    echo "Styles Array:"; print_r($wp_styles);
    echo "Scripts Array:"; print_r($wp_scripts);
}
add_action('wp_enqueue_scripts','print_global_arrays',999);
| improve this answer | |

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.