Created
January 12, 2012 23:40
-
-
Save blogan/1603840 to your computer and use it in GitHub Desktop.
Facebook Open Graph Protocol Meta Plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| Plugin Name: Facebook Open Graph Protocol Meta | |
| Plugin URI: http://blogan.net/blog/ | |
| Description: Adds Facebook Open Graph Protocol metadata to header on single posts. Requires <a href="http://justintadlock.com/archives/2008/05/27/get-the-image-wordpress-plugin">get-the-image</a> plugin developed by <a href="http://justintadlock.com/">Justin Tadlock</a>. | |
| Version: 0.7 | |
| Author: Brent Logan | |
| Author URI: http://blogan.net | |
| License: GPL2 | |
| */ | |
| /* | |
| * [get_the_link] (http://www.turingtarpit.com/2009/03/get-the-image-link/) by [Chandima Cumaranatunge] (http://www.turingtarpit.com). | |
| */ | |
| function get_the_link($args = array()) { | |
| if (function_exists('get_the_image')) { | |
| $args = wp_parse_args($args); | |
| $args['echo'] = false; // prevent echo output | |
| $imageElement = get_the_image($args); | |
| $pattern = '/src=([\'"])?(.*?)\\1/i'; | |
| preg_match($pattern, $imageElement, $matches); | |
| return $matches[2]; | |
| } else { | |
| return '<!-- Install and activate "Get the Image" plugin first. -->'; | |
| } | |
| } | |
| function fb_ogp_meta() { | |
| if ( is_single() || is_page() ) { ?> | |
| <meta property="og:title" content="<?php echo trim(wp_title(' ',false)); ?>" /> | |
| <meta property="og:type" content="article" /> | |
| <meta property="og:locale" content="en_US" /> | |
| <meta property="og:url" content="<?php echo get_permalink(); ?>" /> | |
| <meta property="og:image" content="<?php echo get_the_link( array( 'default_image' => 'http://www.blogan.net/blog/wp-content/uploads/2011/01/avatar2010-tall.png', 'size' => 'medium', 'image_scan' => true ) ); ?>" /> | |
| <meta property="og:site_name" content=" <?php bloginfo('name'); ?>" /> | |
| <meta property="fb:admins" content="500024518" /> | |
| <?php global $post; | |
| if (is_single()) { | |
| $fb_ogp_excerpt = esc_html( strip_tags( get_the_excerpt($post->ID) ) ); | |
| echo '<meta property="og:description" content="'; echo $fb_ogp_excerpt; echo '" />' . PHP_EOL; | |
| } | |
| } elseif ( is_home() ) { ?> | |
| <meta property="og:title" content="<?php bloginfo('name'); ?>" /> | |
| <meta property="og:type" content="blog" /> | |
| <meta property="og:locale" content="en_US" /> | |
| <meta property="og:url" content="<?php bloginfo('url'); ?>" /> | |
| <meta property="og:image" content="http://www.blogan.net/blog/wp-content/uploads/2011/01/avatar2010-tall.png" /> | |
| <meta property="og:site_name" content="<?php bloginfo('name'); ?>" /> | |
| <meta property="fb:admins" content="500024518" /> | |
| <meta property="og:description" content="I\'m Brent Logan and I\'ve been here since late 2004, writing about family, food, and fun." /> | |
| <?php } | |
| } | |
| add_action('wp_head', 'fb_ogp_meta'); | |
| ?> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
get_the_link by Chandima Cumaranatunge.