Created
October 17, 2014 20:35
-
-
Save foxydot/4d9b298600c3e348af0a to your computer and use it in GitHub Desktop.
Open Graph addition for WP.
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
/*** 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