Fix No Thumbnail & Title Issue When Sharing WordPress Post On Facebook

Thumbnails not showing up when sharing link n Facebook? You can easily resolve this issue by fixing your WordPress theme.

Usually, when you share any link such as page or post on Facebook, an image from the respective post is automatically fetched and appears next to the shared link. The problem comes when sometimes you to try share a link and it doesn’t show up any image and title next to it. What’s happening?

The problem is Facebook script that runs on each page load and searches for all image tags to display the ones it finds appropriate. Many-times script times out without fetching any image and title.

To overcome this sharing issue, Facebook simply recommends its developers to justify exactly which image to pick by adding a meta tag in the header of the post or page.

This cab easily be done on static websites by adding following meta tag code in the header:

<meta property="og:image" content="http://yourwebsite.com/default-thumbnail.jpg"/>

But doesn’t works on dynamic websites such as WordPress. For WordPress you will need to dig into your theme function file to tell Facebook script use main post image or featured thumbnail or pick a default image (if post contains no image in it).

So all you got to do is open your theme’s function.php file and add the following code:

function insert_image_src_rel_in_head() {
	global $post;
	if ( !is_singular()) //if it is not a post or a page
		return;
	if(!has_post_thumbnail( $post->ID )) {
		$default_image="http://yourwebsite.com/default-thumbnail.jpg"; //replace this for a default image
		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_image_src_rel_in_head', 5 );

That’s it. It works perfectly on most WordPress websites.

If in-case you prefer a plugin then you may go for Facebook Thumb Fixer plugin which also fixes (sometimes) the problem of the missing or wrong thumbnail when a post is shared on Facebook.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.