Skip to content
 

Facebook Open Graph Meta WordPress Plugin

Today I wanted put Facebook Open Graph Protocol meta tags on my blog. I searched for a long time and I was not able to get a simple and working plugin for WordPress. So I decided to write my own. If you want use this plugin please make sure you replace the tag [REPLACE] with appropriate values.

<?php
/*
Plugin Name: Facebook Open Graph Meta WordPress Plugin
Plugin URI: http://imthi.com/blog/programming/facebook-open-graph-meta-wordpress-plugin.php
Description: This plugin will help you to insert open graph meta tags for the posts
Author: Imthiaz Rafiq
Version: 1.0
Author URI: http://imthi.com/
*/

function getTheExcerptCustom( $post_id, $auto_generate = true, $length = 50, $ellipsis = ' [...]' ) {
	$post = get_post( $post_id );
	$excerpt = $post->post_excerpt;
	if ( !$excerpt && $auto_generate ) {
		$content = strip_tags( strip_shortcodes( $post->post_content ) );
		$content = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", " ", $content);
		$words = explode( ' ', $content );
		if ( count( $words ) > $length ){
			$excerpt = implode( ' ', array_slice( $words, 0, $length ) ) . $ellipsis;
		}else{
			$excerpt = $content;
		}
	}
	return $excerpt;
}

function insertFBOpenGraphMeta() {
	$postImage = false;
	print "\n<!-- Start FB Open Graph Meta -->\n";
	if(is_singular()){
		$attachmentDetails = &get_children( "numberposts=1&post_type=attachment&post_mime_type=image&post_parent=" . get_the_ID() );
		if(!empty ($attachmentDetails)){
			$attachmentDetails = array_shift($attachmentDetails);
			$postImage = array_shift(wp_get_attachment_image_src($attachmentDetails->ID,'thumbnail'));
		}
		print "<meta property=\"og:title\" content=\"". the_title('','',FALSE) ."\" />\n";
		print "<meta property=\"og:url\" content=\"". get_permalink() ."\" />\n";
		print "<meta property=\"og:description\" content=\"". strip_tags(getTheExcerptCustom(get_the_ID())) ."\" />\n";
		if(!empty ($postImage)){
			print "<meta property=\"og:image\" content=\"{$postImage}\" />\n";
		}
		print "<meta property=\"og:type\" content=\"article\" />\n";
	}else{
		print "<meta property=\"og:title\" content=\"". get_bloginfo('name') ."\" />\n";
		print "<meta property=\"og:url\" content=\"". get_bloginfo('url') ."\" />\n";
		print "<meta property=\"og:description\" content=\"". get_bloginfo('description') ."\" />\n";
		print "<meta property=\"og:image\" content=\"[REPLACE]\" />\n";
		print "<meta property=\"og:type\" content=\"blog\" />\n";
	}
	print "<meta property=\"fb:app_id\" content=\"[REPLACE]\" />\n";
	print "<meta property=\"fb:admins\" content=\"[REPLACE]\" />\n";
	print "<!-- End FB Open Graph Meta -->\n";
}

add_action('wp_head', 'insertFBOpenGraphMeta');

This plugin is already activate on my blog. If anyone shares or likes a link from my blog it will have all full details on facebook ;-)

Download Facebook Open Graph Meta WordPress Plugin

  • http://danpurdy.co.uk Dan P

    HI there,

    Thanks for this, i had been having trouble trying to get the excerpt of my post to play nicely with the description but this worked a treat!

    • http://imthi.com Imthiaz

      Hi Dan, I am really glad this plugin helped :-)

      Cheer, Have fun

  • http://greenmonster.in Anjali Kapur

    i want to show post description instead of blog description. ?