Last active
April 13, 2021 21:52
-
-
Save michelep/8c555e37274f1b33707fcae8e350f7b9 to your computer and use it in GitHub Desktop.
OpenGraph integration for Wordpress theme
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
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