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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
< ?php
/*
Plugin Name: Facebook Open Graph Meta Wordpress Plugin
Plugin URI: http://www.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://www.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

