Skip to content

Instantly share code, notes, and snippets.

@makbeta
Created June 26, 2013 06:30
Show Gist options
  • Save makbeta/5865209 to your computer and use it in GitHub Desktop.
Save makbeta/5865209 to your computer and use it in GitHub Desktop.
WordPress: Social media image tag based on featured image field
<?php
function social_media_image() {
global $post;
$output = '';
$featured_image = get_post_featured_image($post->ID, 'medium');
if( $featured_image['id'] != null) {
$output .= '<link rel="img_src" type="image/jpeg" href="'.$featured_image['url'].'" />'."\n";
$output .= '<meta property="og:image" content="'.$featured_image['url'].'"/>'."\n";
}
if($output != '') {
print $output;
}
}
//add social media image, based on featured image
add_action( 'wp_head', 'social_media_image');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment