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: https://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: https://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\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 "\n"; print "\n"; print "\n"; if(!empty ($postImage)){ print "\n"; } print "\n"; }else{ print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; } print "\n"; print "\n"; print "\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 😉
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!
Hi Dan, I am really glad this plugin helped 🙂
Cheer, Have fun