Looking to Build/Redesign a Website or Blog ?

To speak to Rakshit, call +91 968 704 2303

[Facebook] Avoid No Thumbnail When Sharing Post

Hello Friends,

I got complain from my client regarding the No Thumbnail or Unrelated image when sharing blog post with Facebook. Facebook scripts reads all <img /> tags and display all images in choose options when you land on facebook share page. Sometimes, if time outs with facebook share script, it won’t show even single image.

To come up with a solution for this, facebook tells the developer to specify which image to use when someone share article or post on facebook. Below is the code (meta tag) which you need to specify in your head tag.

<meta property="og:image" content="http://www.programmingfacts.com/wp-content/uploads/php-freelancer-rakshit-patel.png"/>

You need to place above tag in all single pages with different related image so when user share the article or post on facebook, they will see your mentioned image as option to select.

If your site is static than you can do this easily by placing above code in all pages and change the image url accordingly.

But if your site is dynamic, CMS driven than you need to write code accordingly so it will generate code for each page.

If you are using WordPress than here is the code for you. Just copy and paste it in functions.php file.

function generate_meta_img_facebook() {
 global $post;
 if ( !is_singular()) //if it is not a post or a page
 if(!has_post_thumbnail( $post->ID )) { 
  // no featured image than define default image below
  $facebook_thumb = "http://www.programmingfacts.com/wp-content/uploads/php-freelancer-rakshit-patel.png";
  $facebook_thumb_arr = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
  $facebook_thumb = $facebook_thumb_arr[0];
 echo '<meta property="og:image" content="' . esc_attr( $facebook_thumb ) . '"/>';
add_action( 'wp_head', 'generate_meta_img_facebook', 5 );

No Thumbnail Issue while Facebook Share, Article Sharing with Thumbnail on Facebook, Share post with thumbnail on Facebook, No Thumb Image Sharing Post on Facebook, all above issues are resolved now.


No Comments Yet.

Leave a Reply

Your email address will not be published.