Skip to content

Instantly share code, notes, and snippets.

@tlongren
Last active September 24, 2018 13:48
Show Gist options
  • Save tlongren/de3f80c5f0b54a01e24f to your computer and use it in GitHub Desktop.
Save tlongren/de3f80c5f0b54a01e24f to your computer and use it in GitHub Desktop.
Add Open Graph Markup to WordPress Theme
<?php
function add_opengraph_markup() {
if (is_single()) {
global $post;
if(get_the_post_thumbnail($post->ID, 'thumbnail')) {
$thumbnail_id = get_post_thumbnail_id($post->ID);
$thumbnail_object = get_post($thumbnail_id);
$image = $thumbnail_object->guid;
} else {
// set default image
$image = '';
}
//$description = get_bloginfo('description');
$description = substr(strip_tags($post->post_content),0,200) . '...';
?>
<meta property="og:title" content="<?php the_title(); ?>" />
<meta property="og:type" content="article" />
<meta property="og:image" content="<?=$image?>" />
<meta property="og:url" content="<?php the_permalink(); ?>" />
<meta property="og:description" content="<?=$description?>" />
<meta property="og:site_name" content="<?=get_bloginfo('name')?>" />
<?php
}
}
add_action('wp_head', 'add_opengraph_markup');
?>
@tlongren
Copy link
Author

tlongren commented Apr 2, 2015

If no image is set for a post or page, the $image variable can set or dynamically constructed to point to an image on your server. To point it to a static file, line 11 would look like:

$image = 'http://yourserver.com/images/whatever.png';

@arximughal
Copy link

added this to my theme's function.php file, now wp-admin won't return anything but a blank page.
removing this code corrects the wp-admin problem.
any idea what's happening ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment