Skip to content

Instantly share code, notes, and snippets.

@michelep
Last active April 13, 2021 21:52
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 michelep/8c555e37274f1b33707fcae8e350f7b9 to your computer and use it in GitHub Desktop.
Save michelep/8c555e37274f1b33707fcae8e350f7b9 to your computer and use it in GitHub Desktop.
OpenGraph integration for Wordpress theme
In header.php theme file, add this between <head> and </head> tags:
<?php
if ( is_single() && 'post' == get_post_type() ) {
if(has_post_thumbnail($post->ID)) {
$img_src = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
}
if($excerpt = get_the_excerpt()) {
$excerpt = strip_tags($excerpt);
$excerpt = str_replace("", "'", $excerpt);
} else {
$excerpt = get_bloginfo('description');
}
?>
<!-- OpenGraph meta for social -->
<meta property="og:title" content="<?php the_title(); ?>" />
<meta property="og:type" content="article" />
<meta property="og:image" content="<?php echo $img_src; ?>" />
<meta property="og:url" content="<?php the_permalink(); ?>" />
<meta property="og:description" content="<?php echo $excerpt; ?>" />
<meta property="og:site_name" content="<?php echo get_bloginfo(); ?>"/>
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:site" content="@[twitter username]" />
<?php
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment