Skip to content

Instantly share code, notes, and snippets.

@danixland
Created February 27, 2016 09:02
Show Gist options
  • Save danixland/0398978966289faa7e91 to your computer and use it in GitHub Desktop.
Save danixland/0398978966289faa7e91 to your computer and use it in GitHub Desktop.
// opengraph metatags
function danixland_opengraph() {
global $post;
$output = '';
$image = get_template_directory_uri() . '/img/standard-logo.png';
if ( is_home() || is_front_page() ) {
$output = '<meta property="og:url" content="' . get_bloginfo('url') . '" />' . "\n";
$output .= '<meta property="og:type" content="website" />' . "\n";
$output .= '<meta property="og:title" content="' . wp_title('&bull;', false, 'right') . '" />' . "\n";
$output .= '<meta property="og:description" content="' . get_bloginfo( 'description' ) . '" />' . "\n";
$output .= '<meta property="og:image" content="' . $image . '" />' . "\n";
} elseif ( is_singular() ) {
$output = '<meta property="og:url" content="' . get_the_permalink() . '" />' . "\n";
$output .= '<meta property="og:type" content="article" />' . "\n";
$output .= '<meta property="og:title" content="' . get_the_title() . '" />' . "\n";
$output .= '<meta property="og:description" content="' . get_the_excerpt() . '" />' . "\n";
if ( is_single() && has_post_thumbnail($post->ID) ) {
$image = get_the_post_thumbnail_url($post->ID, "medium");
}
$output .= '<meta property="og:image" content="' . $image . '" />' . "\n";
}
echo $output;
}
add_action( 'wp_head', 'danixland_opengraph' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment