Skip to content

Instantly share code, notes, and snippets.

@foxydot
Created October 17, 2014 20:35
Show Gist options
  • Save foxydot/4d9b298600c3e348af0a to your computer and use it in GitHub Desktop.
Save foxydot/4d9b298600c3e348af0a to your computer and use it in GitHub Desktop.
Open Graph addition for WP.
/*** HEADER ***/
add_action('wp_head','msdlab_add_open_graph_meta',1);
/**
* Add new image sizes
*/
add_image_size('facebook', 200, 200, TRUE);
add_image_size('linkedin', 180, 110, TRUE);
/**
* Add open graph data, if it's not already being added by another plugin.
*/
function msdlab_add_open_graph_meta(){
$ret = '';
if(is_cpt('post') && is_single()){
global $post, $wpseo;
remove_action('wpseo_opengraph',array($GLOBALS['wpseo_og'],'image'),30);
if(wpseo_get_value( 'opengraph-image' )){ //yoast defined
$attachment_id = get_attachment_id_from_src(wpseo_get_value( 'opengraph-image' ));
} elseif(has_post_thumbnail($post->ID)){ //featured image
$attachment_id = get_post_thumbnail_id($post->ID);
} else {
$args = array(
'post_type' => 'team_member',
'meta_key' => '_team_member__team_user_id',
'meta_value'=> $post->post_author,
);
$author_bio = array_pop(get_posts($args));
if($author_bio){
$attachment_id = get_post_thumbnail_id($author_bio->ID);
} else {
$attachment_id = get_option('msdsocial_default_avatar');
}
}
if($attachment_id){
$sizes = array('linkedin','facebook');
foreach($sizes AS $size){
$image = wp_get_attachment_image_src($attachment_id,$size);
$ret .= '
<meta property="og:image" content="'.$image[0].'" /> <!-- '.$image[1].'x'.$image[2].' Image for '.$size.' -->
<meta property="og:image:width" content="'.$image[1].'" />
<meta property="og:image:height" content="'.$image[2].'" />';
}
}
}
print $ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment