Skip to content

Instantly share code, notes, and snippets.

@Jany-M
Last active June 6, 2021 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Jany-M/2abdb626cf094a36b578eaeeaa48787e to your computer and use it in GitHub Desktop.
Save Jany-M/2abdb626cf094a36b578eaeeaa48787e to your computer and use it in GitHub Desktop.
[WP] Get an image for Open Graph
<?php
// Get an image for Open Graph - Uses WP Imager https://github.com/Jany-M/WP-Imager
if(is_singular()) {
global $post;
if(function_exists('wp_imager')) {
$img = wp_imager(1200, 630, 1, '', '', '', true );
} elseif(has_post_thumbnail($post->ID)) {
$img = get_the_post_thumbnail_url( $post->ID, array(1200,630));
}
} else {
$img = get_stylesheet_directory_uri().'/img/placeholder.jpg';
}
?>
<meta property="og:image" content="<?php echo $img; ?>" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment