Skip to content

Instantly share code, notes, and snippets.

@gthayer
Created October 5, 2015 18:51
Show Gist options
  • Save gthayer/3951619b0aeaed6cc987 to your computer and use it in GitHub Desktop.
Save gthayer/3951619b0aeaed6cc987 to your computer and use it in GitHub Desktop.
//Replace og:image with either the post featured image, or an image of the speaker
//Requires Yoast
add_action( 'wpseo_opengraph', 'replace_og_image', 30 );
function replace_og_image() {
global $post;
$featured_image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full');
$speaker = get_field('speaker', $post->ID);
$speaker_image = wp_get_attachment_image_src(get_post_thumbnail_id($speaker['0']->ID), 'full');
if ( $featured_image['0'] ) {
$og_image = $featured_image['0'];
} else if ( $speaker_image['0'] ) {
$og_image = $speaker_image['0'];
}
if ( $og_image ) {
add_filter('wpseo_opengraph_image', function() use($og_image){
return $og_image;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment