Skip to content

Instantly share code, notes, and snippets.

@pingram3541
Created September 25, 2015 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pingram3541/b83d672bfd147c971d0a to your computer and use it in GitHub Desktop.
Save pingram3541/b83d672bfd147c971d0a to your computer and use it in GitHub Desktop.
Add Facebook Open Graph Meta Tags
/* ---------------------------------------------------------------------------
* FACEBOOK OPENGRAPH SETTINGS
* ---------------------------------------------------------------------------
* @wp-filter - language_attributes
* @retun - adds og lang tags to doc element
*
* @wp-hook wp_head
* @return - adds og meta tags to pages
* --------------------------------------------------------------------------- */
add_filter('language_attributes', 'tda_doctype_opengraph');
function tda_doctype_opengraph($output) {
return $output . '
xmlns:og="http://opengraphprotocol.org/schema/"
xmlns:fb="http://www.facebook.com/2008/fbml"';
}
add_action('wp_head', 'tda_fb_opengraph', 5);
function tda_fb_opengraph() {
global $post;
//$date = date("Y-m-d");
//$time = time();
if(is_single() || is_front_page()) {
if(has_post_thumbnail($post->ID)) {
$img_src = wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'medium');
} else {
$img_src = 'http://thedeviantadvantagebook.com/wp-content/uploads/2014/09/sandi-speaks.jpg';
}
if($excerpt = $post->post_excerpt) {
$excerpt = strip_tags($post->post_excerpt);
$excerpt = str_replace("", "'", $excerpt);
} else {
$excerpt = get_bloginfo('description');
}
?>
<meta property="og:title" content="<?php echo the_title(); ?>"/>
<meta property="og:description" content="<?php echo $excerpt; ?>"/>
<meta property="og:type" content="article"/>
<meta property="og:url" content="<?php echo the_permalink(); ?>?"/>
<meta property="og:site_name" content="<?php echo get_bloginfo(); ?>"/>
<meta property="og:image" content="<?php echo $img_src; ?>"/>
<?php
} else {
//return;
?>
<! -- no fb meta output -->
<?php
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment