Skip to content

Instantly share code, notes, and snippets.

@bryanmonzon
Created May 14, 2014 22:49
Show Gist options
  • Save bryanmonzon/553781558707b63b97fe to your computer and use it in GitHub Desktop.
Save bryanmonzon/553781558707b63b97fe to your computer and use it in GitHub Desktop.
There are a couple of extra functions in here but this should do the trick.
function gmc_facebook_twitter_social_meta()
{
global $post;
$default_profile_image = 'http://www.globalmomschallenge.org/wp-content/uploads/2014/03/opengraph-default.jpg';
if( is_single() ){
$title = get_the_title( $post->ID );
$url = get_permalink( $post->ID );
$description = gmc_has_description() ? gmc_get_the_description() : strip_tags( get_the_excerpt() );
if( has_post_thumbnail( $post->ID ) ) {
$fb_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'blog-thumb' );
$fb_image = $fb_image[0];
$tw_card = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'blog-thumb' );
$tw_card = $tw_card[0];
}else{
$fb_image = $default_profile_image;
$tw_card = $default_profile_image;
}
}elseif( is_home() ){
$title = get_bloginfo('name') . ' - ' . gmc_get_blog_title();
$url = get_permalink( get_option('page_for_posts'));
$description = get_bloginfo('description');
$fb_image = $default_profile_image;
$tw_card = $default_profile_image;
}elseif( is_front_page() ){
$title = get_bloginfo('name');
$url = home_url();
$description = get_bloginfo('description');
$fb_image = $default_profile_image;
$tw_card = $default_profile_image;
}elseif( is_page() ){
$title = get_the_title( $post->ID ) . ' - ' . get_bloginfo('name');
$url = get_permalink( $post->ID );
$description = get_the_content( $post->ID );
$fb_image = $default_profile_image;
$tw_card = $default_profile_image;
}else{
$title = get_bloginfo('name');
$url = home_url();
$description = get_bloginfo('description');
$fb_image = $default_profile_image;
$tw_card = $default_profile_image;
}
?>
<!-- for Facebook -->
<meta property="og:title" content="<?php echo $title; ?>" />
<meta property="og:type" content="article" />
<meta property="og:image" content="<?php echo $fb_image; ?>" />
<meta property="og:url" content="<?php echo $url; ?>" />
<meta property="og:description" content='<?php echo wp_kses_decode_entities( $description ); ?>' />
<!-- for Twitter -->
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="<?php echo $title; ?>" />
<meta name="twitter:description" content='<?php echo wp_kses_decode_entities($description); ?>' />
<meta name="twitter:image" content="<?php echo $tw_card; ?>" />
<?php
}
add_action( 'wp_head', 'gmc_facebook_twitter_social_meta' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment