0

I have a small bit of code which creates the OpenGraph tags for each post. It seems to work OK, but the actual Featured image is not grabbed by Facebook when I copy the URL into a new FB post.

Or rather, the -default- image is grabbed, but not the Featured image. But then if I open the 'Object Debugger For Facebook' and get a new scrape for the page, the correct image always appears. This has gotten tedious.

The only error I ever see in the Object Debugger is that "the image may be too small. Images must be at least 200x200..."... which is not true. The image is always the WP medium size (300 x 209, I believe?)

I've checked that the code in the page header generates properly.

I am not using a caching plug-in or a CDN.

So: What am I doing wrong? How can I insure that FB always grabs the correct og:image.

function insert_fb_in_head() {
    global $post;
    if ( !is_singular()) //if it is not a post or a page
        return;

//echo '<meta property="fb:admins" content="YOUR USER ID"/>';

  echo '<meta property="og:title" content="' . get_the_title() . '"/>';
  echo '<meta property="og:type" content="article"/>';
  echo '<meta property="og:url" content="' . get_permalink() . '"/>';
  echo '<meta property="og:site_name" content="example site"/>';
  echo '<meta property="og:description" content="' . get_post_meta(get_the_ID(), '_yoast_wpseo_metadesc', true). '"/>';

if(!has_post_thumbnail( $post->ID )) { //the post does not have featured image, use a default image
    $default_image="http://jchmusic.com/images/default-250.jpg"; //replace this with a default image on your server or an image in your media library
    echo '<meta property="og:image" content="' . $default_image . '"/>';
}
else{
    $thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
    echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>';
}
echo "";
}
 add_action( 'wp_head', 'insert_fb_in_head', 5 );
  /* Facebook ends *************/
4
  • this code looks ok. you need to setup tracing/loging to see what happens when FB send a request. You might have a problematic interaction from other parts of your code Sep 19, 2016 at 18:19
  • @MarkKaplun Not sure what you mean by 'setup tracing logging'. Do you mean in my browser? Can you give me some specific steps? TIA.
    – jchwebdev
    Sep 19, 2016 at 19:03
  • first thing, IIRC FB gives you the response it has acted upon. You might want to first use the debugger before sharing on FB itself and see the result. Otherwise you need to simulate a request from FB with a tool like wget and check what is the response Sep 19, 2016 at 19:38
  • I tried a wget and it pulls the correct og:Image. And even more annoying, when I go to the Facebook Object debugger and click 'Current Scrape' it shows the -correct- og:image tag but the -wrong- image. The moment I click the New Scrape button, it magically displays the correct image. There gotta be a 'trick' to having FB grab the correct image on the first trip, right?
    – jchwebdev
    Sep 19, 2016 at 22:32

1 Answer 1

1

While you might believe that you have an error in WordPress, the actual problem actually is on facebooks end. When they already have an image, you need to force a rescrape (iirc) via their Open Graph Debugger(?). facebook caches images of various sizes by Url. Meaning that if you already served an image, they already have it on their servers and refuse to download. You now have two options:

  1. Rescrape with facebook tools
  2. Provide a different image on a different url

Whatever seems simpler to you is what you should do.

2
  • I'm finding it hard to believe that -everyone- who creates sites that share with FB have to go through such a 'manual' rigamarole. Surely large sites must have some 'trick' which either does not require a 'rescrape' OR automates the process.
    – jchwebdev
    Sep 20, 2016 at 19:53
  • @jchwebdev Yes, you can change the actual Url if you need a rescrape. You can also build a specific endpoint where you point the crawler. What you do there totally depends on you – but this link should not be part of the answer.
    – kaiser
    Sep 21, 2016 at 10:15

Your Answer

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

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