Facebook is not taken correctly og: Image at the initial stage

I have a Wordpress site with a little php code that creates OpenGraph tags for each post. Unfortunately, Featured image (og: image) does not capture Facebook when I copy the URL to a new FB post.

Just to clarify the flow of events:

  • My users create Wordpress.

  • Then someone shares it on Facebook, copying the link into the FB post. Facebook displays the correct text, but captures the wrong og: image.

  • I go to the debugger object and check the Wordpress message. It does not show errors and all the correct og tags (including the correct og: image), but displays the default image (incorrect) for the site in its thumbnail!

  • Then, if I click the Scrape New button, the corresponding thumbnail will appear (corresponding to the og: image tag).

  • Now, if the user uses the same Wordpress post on Facebook, FB - displays the correct og: image in the FB post.

So, initially the image is -default-grabbed, but not Featured (ie post_thumbnail () in Wordpress. However, if I open the "Object Debugger for Facebook" and get a new scratch for the page, the correct image always appears. This is tedious.

, Object Debugger , ( ), , og: image Facebook , , -. , -says- , !

, - , , " . 200x200..."... . - 300 x 209.

, .

CDN.

: , FB og: without WITH, Scrape New.

: () , "" , . , . , .

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 '<meta property="og:image:width" content="' . esc_attr( $thumbnail_src[1] ) . '"/>';
    echo '<meta property="og:image:height" content="' . esc_attr( $thumbnail_src[2] ) . '"/>';      

}
echo "";
}
 add_action( 'wp_head', 'insert_fb_in_head', 5 );
  /* Facebook ends *************/
0

Source: https://habr.com/ru/post/1667545/


All Articles