How to set default image for Facebook sharing in WordPress blog posts ?

A picture equals to many words. Facebook can be the biggest traffic source for your website, web 2.0 is social web. It may happen that your article is very catchy, get shared many times on Facebook but many of them don’t click the link. Attaching certain picture may increase click rate of Facebook news feed.

Increase of click rate means increased visitors, increased social sharing. To select certain image for a post you need to add some meta tags in header. Its obvious that we need different meta tags for each post. Static meta tags won’t do.

We will add a custom field and then fetch it in header file.

Add a custom field named “fbimage”. On post edit page under “Custom Fields” click enter new. Type “fbimage” and under value type image name you want to select for that post. ( Upload the image to wp-content/uploads)

( Note that you need to add new custom field only once, next time you just need to enter only value selecting field name from dropdown )

Update the post.

Now edit your theme from Appearance –> Editor. Select header.php.

Anywhere between <head> to </head> add below code.

<?php if(is_single()){ $values = get_post_custom_values(‘fbimage’,$post->ID); if($values[0]!=”){?>
<meta property=”og:image” content=”<?php $values = get_post_custom_values(‘fbimage’,$post->ID); echo $values[0]; ?>”/>
<link rel=”image_src” href=”<?php $values = get_post_custom_values(‘fbimage’,$post->ID); echo $values[0]; ?>” / >

Update the file and you’re done.

From next time you write a post, add value to “fbimage” custom field and enjoy increased click rate.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: