add_action and wp_head not display content in head section

I have a big problem.
In a WordPress page I’ve a function that generate og meta tags. I want to “append” that generated meta in the head section. So, I write this code:

add_action('wp_head', '_set_meta_tag');
function _set_meta_tag()
{
    global $nome;
    global $descr;
    global $file;
    global $path_meta;

    $output = "";
    $output .= '<meta property="og:title" content="'.$nome.'" />';
    $output .= '<meta property="og:type" content="article" />';
    $output .= '<meta property="og:image" content="'; $output .= get_bloginfo('template_url'); $output .= '/includes/img/immagini_evento/'.$file.'" />';
    $output .= '<meta property="og:url" content="'.$path_meta.'" />';
    $output .= '<meta property="og:description" content="'.truncate(htmlentities($descr),200).'" />';
    $output .= '<meta property="og:site_name" content="'; $output .= get_bloginfo('name'); $output .='" />';

    echo $output;}

add_action('wp_head', '_set_meta_tag'); not display $output. Why?

I’ve prooved also to echoes a simple “hello world”, but nothing happened!

2 Answers
2

That code must run before the wp_head hook fires or nothing will happen, and when tested in a mu-plugin file it does work.

I suspect that you are trying to hook that function too late– perhaps from inside a theme template file after get_header (in most cases). Place that code in the theme’s functions.php or in a (mu-)plugin file and it should work at least insofar as echoing content goes. I did not analyze it for other bugs.

Leave a Comment