Skip to content

Instantly share code, notes, and snippets.

@markdcraftww
Last active December 17, 2015 19:39
Show Gist options
  • Save markdcraftww/5662339 to your computer and use it in GitHub Desktop.
Save markdcraftww/5662339 to your computer and use it in GitHub Desktop.
Add Facebook Open Graph tags to Wordpress
in functions.php:
function wp_opengraph_for_posts() {
if ( is_singular() ) {
global $post;
setup_postdata( $post );
$output = '<meta property="og:type" content="article" />' . "\n";
$output .= '<meta property="og:title" content="' . esc_attr( get_the_title() ) . '" />' . "\n";
$output .= '<meta property="og:url" content="' . get_permalink() . '" />' . "\n";
$output .= '<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 ), '' );
$output .= '<meta property="og:image" content="' . $imgsrc[0] . '" />' . "\n";
}
echo $output;
}
}
add_action( 'wp_head', 'wp_opengraph_for_posts' );
in 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