Skip to content

Instantly share code, notes, and snippets.

@hsquareweb
Created November 19, 2012 15:23
Show Gist options
  • Save hsquareweb/4111243 to your computer and use it in GitHub Desktop.
Save hsquareweb/4111243 to your computer and use it in GitHub Desktop.
WP: Facebook Open Graph Sharing
<!-- Make the content easily recognizable by social networks, we can use the Open Graph (http://ogp.me) spesification so when somebody shares our content, the social network can show accurate information about the page that is shared. -->
<!-- Add to functions.php -->
function opengraph_for_posts() {
if ( is_singular() ) {
global $post;
setup_postdata( $post );
$og_type = '<meta property="og:type" content="article" />' . "\n";
$og_title = '<meta property="og:title" content="' . esc_attr( get_the_title() ) . '" />' . "\n";
$og_url = '<meta property="og:url" content="' . get_permalink() . '" />' . "\n";
$og_description = '<meta property="og:description" content="' . esc_attr( get_the_excerpt() ) . '" />' . "\n";
if ( has_post_thumbnail() ) {
$imgsrc = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
$og_image = '<meta property="og:image" content="' . $imgsrc[0] . '" />' . "\n";
}
echo $og_type . $og_title . $og_url . $og_description . $og_image;
}
}
add_action( 'wp_head', 'opengraph_for_posts' );
<!-- Update header.php -->
<html <?php language_attributes(); ?> prefix="og: http://ogp.me/ns#">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment