Skip to content

Instantly share code, notes, and snippets.

@matthewhudson
Forked from abecoffman/functions.php
Created September 4, 2012 13:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewhudson/3621261 to your computer and use it in GitHub Desktop.
Save matthewhudson/3621261 to your computer and use it in GitHub Desktop.
Adds Facebook Graph meta tags to Wordpress blog pages and thumbnail support to your Wordpress theme
<?php
if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
}
function the_facebook_graph_data() {
// setup the default site attributes
$graph;
$graph["site_name"] = "YOUR_SITE_NAME";
$graph["admins"] = "YOUR_FACEBOOK_ID";
$graph["app_id"] = "YOUR_APP_ID";
// setup the default page attributes
$graph["type"] = "blog";
$graph["title"] = "DEFAULT_TITLE";
$graph["image"] = "LINK_TO_DEFAULT_BLOG_IMAGE";
$graph["description"] = "YOUR_WEBSITE_DESCRIPTION";
$graph["url"] = "YOUR_WEBSITE_URL";
// override the page attributes for individual posts
global $post;
$postid = $post->ID;
if(is_single()) {
$graph["type"] = "article";
$graph["title"] = $post->post_title;
// Image (thumbnail)
if(has_post_thumbnail($postid)) {
$image = wp_get_attachment_image_src(
get_post_thumbnail_id($postid),'post-thumbnail', false);
$graph["image"] = $image[0];
}
// Description (excerpt)
$excerpt;
if($post->post_excerpt) {
$excerpt = strip_tags($post->post_excerpt);
} else {
$excerpt = strip_tags($post->post_content);
}
$excerpt = str_replace(array("\n", "\r", "\t"), ' ', $excerpt);
$graph["description"] = substr($excerpt, 0, 125);
// URL (permalink)
$graph["url"] = get_permalink($postid);
}
foreach($graph as $key => $value) {
echo '<meta property="og:' . $key . '" content="' . $value . '" />';
}
}
add_action('wp_head', 'the_facebook_graph_data');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment