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?

Read More

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

Related posts

2 comments

  1. 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.

  2. I had this same problem. I solved the problem by creating my own hook.

    This is in my header.php file. My hook is placed before wp_head()

    <?php my_doc_head(); ?>
    <?php wp_head(); ?>
    </head>
    

    This hook and the callback function added to the hook are defined in my function.php.

    function my_doc_head() {
        do_action('my_doc_head');
    }
    
    add_action('my_doc_head','my_seo_meta');
    function my_seo_meta() {
        global $wp_query;
        $postid = $wp_query->post->ID;
        //insert whatever code to build $output 
        echo $output;
        wp_reset_query();
    }
    

Comments are closed.