admin管理员组

文章数量:1417054

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

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

Share Improve this question edited May 15, 2016 at 18:03 Benn asked May 15, 2016 at 17:31 BennBenn 1,0631 gold badge15 silver badges32 bronze badges 2
  • 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 Commented May 15, 2016 at 17:43
  • @Milo thnx for the info , is there any way to get the contents of the head tag? – Benn Commented May 15, 2016 at 17:55
Add a comment  | 

3 Answers 3

Reset to default 8

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.

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.

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);

本文标签: wp headIs there a way to read or list wphead() contents