skip to Main Content

I have the following hook for facebook’s og:image tag.

In my template file:

<?php do_action('wp_head', $mypost) ?>

In my functions file:

add_action('wp_head', 'add_fb_og');
function add_fb_og($mypost) {
  if (isset($mypost["wpcf-bgleft"][0])) {
    echo '<meta property="og:image" content="'.$mypost["wpcf-bgleft"][0].'" />';
  }
}

What I’m trying to do is to pass the background image of a custom field to wp_head so that it will output <meta property="og:image" content="http://example.com/mybackgroundimage.jpg" /> for facebook’s consumption.

While the output is there, facebook is not picking up the og:image tag.
I get the warning while using it’s debug tool:

The ‘og:image’ property should be explicitly provided, even if a value can be inferred from other tags.

I could see the og:image tag in source view, so I think the head was processed before it got to the do_action in the template file.

How can I insert a og:image based on a custom field then?

I’m also using the WordPress SEO by Yoast plugin if that matters.

2

Answers


  1. Chosen as BEST ANSWER

    Ok, I found out how to do it.

    1. Don't call do_action('wp_head') in the templates section.

    2. In the functions.php, do:

      add_action('wpseo_head', 'add_fb_og');
      function add_fb_og($mypost) {
        global $post;
      
        $mypost = get_post_custom($offid);
        if (isset($mypost["wpcf-bgleft"][0])) {
          echo '<meta property="og:image" content="'.$mypost["wpcf-bgleft"][0].'" />';
        }
      }
      

    Note I'm using Yoast SEO's hook and calling global $post to get the post data, so I don't have to pass any parameter.


  2. WordPress SEO by yoast also uses facebook opengraphs. So it might be interfaring try disabling it. but if you can view the tag placed correctly and in the right place. Then the problem might be in some other end.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search