Skip to content

Instantly share code, notes, and snippets.

@gcoop
Last active September 24, 2015 19:48
Show Gist options
  • Save gcoop/800060 to your computer and use it in GitHub Desktop.
Save gcoop/800060 to your computer and use it in GitHub Desktop.
Simple script to add obvious Facebook Open Graph attributes to Wordpress posts.
<?php
function fbOg()
{
if (is_single())
{
global $post;
$images = preg_split("/\"/", get_the_post_thumbnail( $post->ID, 'thumbnail' ));
$first_image = isset($images[5]) ? $images[5] : 'http://example.com/blogLogo.jpg'; // Hardcoded fallback.
$attrs = array(
"title" => the_title(null, null, false),
"url" => get_permalink($post),
"site_name" => get_bloginfo('name'),
"description"=>get_bloginfo('description'),
"image" => $first_image,
"type" => "article",
"admins" => "505178361,285902231,100000848644298"
);
foreach ($attrs as $k => $v)
{
echo('<meta property="og:'.$k.'" content="'.$v.'" />'."\n");
}
}
}
add_action('wp_head', 'fbOg');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment