Created
June 7, 2011 12:29
-
-
Save mrlannigan/1012138 to your computer and use it in GitHub Desktop.
WPquestions.com #2394 rev1
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 | |
/**** ADD FB IMAGES***/ | |
//Adding the Open Graph in the Language Attributes | |
function add_opengraph_doctype( $output ) { | |
return $output . ' xmlns:og="http://opengraphprotocol.org/schema/" xmlns:fb="http://www.facebook.com/2008/fbml"'; | |
} | |
add_filter('language_attributes', 'add_opengraph_doctype'); | |
//Lets add Open Graph Meta Info | |
function insert_fb_in_head() { | |
global $post; | |
if ( !is_singular()) //if it is not a post or a page | |
return; | |
echo '<meta property="fb:admins" content="685991022"/>'; | |
echo '<meta property="og:title" content="' . get_the_title() . '"/>'; | |
echo '<meta property="og:type" content="article"/>'; | |
echo '<meta property="og:url" content="' . get_permalink() . '"/>'; | |
echo '<meta property="og:site_name" content="DDGDaily.com"/>'; | |
echo '<meta property="og:image" content="' . catch_that_image(false) . '"/>'; | |
echo "\n"; | |
} | |
add_action( 'wp_head', 'insert_fb_in_head', 5 ); | |
//grab first image url from post content | |
// base function from http://www.wprecipes.com/how-to-get-the-first-image-from-the-post-and-display-it | |
function catch_that_image($inLoop = true) { | |
global $post, $posts; | |
$first_img = ''; | |
if ($inLoop) { | |
ob_start(); | |
ob_end_clean(); | |
} | |
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches); | |
$first_img = $matches[1][0]; | |
if(empty($first_img)){ //Defines a default image | |
$first_img = home_url("/wp-content/uploads/newavatar.png"); | |
} | |
return $first_img; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment