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

3 s
3

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.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *