Skip to content

Instantly share code, notes, and snippets.

@lordspace
Created April 15, 2015 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lordspace/d3b5f4e4803fc5efe926 to your computer and use it in GitHub Desktop.
Save lordspace/d3b5f4e4803fc5efe926 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Orbisius Facebook Share Image Preview Fix
Plugin URI: http://orbisius.com
Description: Makes sure that post's featured image is 1200x630 so facebook can use it as preview.
Version: 1.0.0
Author: Svetoslav Marinov (Slavi)
Author URI: http://club.orbisius.com/products/
*/
add_action( 'wp', 'orbisius_og_tags_init', 100 );
/**
*/
function orbisius_og_tags_init() {
add_action( 'wp_head', 'orbisius_og_output_tags' );
}
/**
* Outputs a header instruction
* @see http://davidwalsh.name/demo/facebook-metas.php
* @see http://stackoverflow.com/questions/11616697/how-to-use-og-meta-tag-for-facebook-share
*/
function orbisius_og_output_tags() {
global $post;
$id = get_the_ID();
/*if ( ! defined( 'orbisius_APP_POST_TYPE' )
|| 'post' != get_post_type( $id ) ) {
return;
}*/
/*
[0] => url
[1] => width
[2] => height
[3] => boolean: true if $url is a resized image, false if it is the original
or if no image is available.
*/
// Make facebook happy.
// @see http://webgilde.com/en/wrong-image-facebook/
$img_src_rec = wp_get_attachment_image_src(
get_post_thumbnail_id( $id, array( 1200, 630 ) )
);
if ( empty( $img_src_rec ) ) {
return ;
}
$img_src = $img_src_rec[0];
/*
<!-- for Facebook -->
<meta property="og:title" content="" />
<meta property="og:type" content="article" />
<meta property="og:image" content="<?php echo $img_src; ?>" />
<meta property="og:url" content="" />
<meta property="og:description" content="" />
<!-- for Twitter -->
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="" />
<meta name="twitter:description" content="" />
<meta name="twitter:image" content="<?php echo $img_src; ?>" />
*/
?>
<!-- Make Facebook happy: 1200x630-->
<meta property="og:image" content="<?php echo $img_src; ?>" />
<!-- /Make Facebook happy: 1200x630-->
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment