Last active
January 27, 2018 03:41
-
-
Save schilke/b0bebb303e0d7fe9f4ed to your computer and use it in GitHub Desktop.
Add Open Graph to Meta Tags
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/**************************************************************************************************************** | |
* ATTENTION! Don't copy the above line if you're going to use this in your functions.php - just the code below * | |
****************************************************************************************************************/ | |
function fb_opengraph() { | |
global $post; | |
if(is_single()) { // article or post - not front page or archive | |
if(has_post_thumbnail($post->ID)) { | |
$img_src = wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'medium'); | |
// if the post/page has a post thumbnail: use this | |
} else { | |
$img_src = get_stylesheet_directory_uri() . '/<path-to-default-image>/<default image>.jpg'; | |
/* else use the default image | |
edit with your data for the defaut image | |
the directory must be inside the theme directory */ | |
} | |
if($excerpt = $post->post_excerpt) { | |
$excerpt = strip_tags($post->post_excerpt); | |
$excerpt = str_replace("", "'", $excerpt); | |
// excerpt for the article - if any | |
} else { | |
$excerpt = get_bloginfo('description'); | |
// else use the blog description | |
} | |
?> | |
<meta property="og:title" content="<?php echo the_title(); ?>"/> | |
<meta property="og:description" content="<?php echo $excerpt; ?>"/> | |
<meta property="og:type" content="article"/> | |
<meta property="og:url" content="<?php echo the_permalink(); ?>"/> | |
<meta property="og:site_name" content="<?php echo get_bloginfo(); ?>"/> | |
<meta property="og:image" content="<?php echo $img_src; ?>"/> | |
<?php | |
} else { | |
return; | |
} | |
} | |
add_action('wp_head', 'fb_opengraph', 5); | |
/**************************************************************************************************************** | |
* ATTENTION! Don't copy the below line if you're going to use this in your functions.php - just the code above * | |
****************************************************************************************************************/ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment