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!

  • did you ever solve this? I have the same problem. Jun 8, 2019 at 21:24

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.

  • Indeed, I try to hook this function in a template page :) Thanks! I will try to change tomorrow and let you now as soon as possible.
    – apanzett
    Jun 21, 2013 at 19:27
  • Any code that this code depends upon has to run before this code, obviously, so hook that code to wp_head as well or earlier. If this only has to run on one template you can probably put all of your code before get_header in the template file but my guess is that that won't work in your case-- that it needs to run more globally than that.
    – s_ha_dum
    Jun 21, 2013 at 19:33
  • If I place in functions.php, can I extract a parameter with get_query_var('id') in order to generate after in that function a MySql query through id?
    – apanzett
    Jun 21, 2013 at 19:42
  • get_query_var depends on the global $wp_query object, so "Yes", probably.
    – s_ha_dum
    Jun 21, 2013 at 19:43
  • It works, but Facebook Like Button not display the correct content. If I paste URL on Facebook, I see the correct content but If I click on the Like Button, the content is wrong.
    – apanzett
    Jun 22, 2013 at 11:03

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

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

function my_doc_head() {

function my_seo_meta() {
    global $wp_query;
    $postid = $wp_query->post->ID;
    //insert whatever code to build $output 
    echo $output;
  • Your answer does not make any sense! Would you like to explain why wp_head does not work and your custom hook works ?
    – Sumit
    Mar 24, 2016 at 5:38
  • I would love to explain it but I am not experienced enough and do not understand WordPress well enough to explain it. All I know is, my function added as an action to wp_head() did not work. In my research , I found this post, read what s_ha_dum said and thought "maybe that's why I can't get my function to work, it isn't firing soon enough". So I created my action and inserted it before wp_head() and voila! Maybe it helps to know my function gets data from custom fields and inserts it into the document head (not the WordPress header).
    – Gigi Gerow
    Mar 24, 2016 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.