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 *************/