Advertisement
me2arafat

How to Avoid No Thumbnail Issue While Sharing Post on Facebo

Dec 12th, 2015
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. function insert_image_src_rel_in_head() {
  2. global $post;
  3. if ( !is_singular()) //if it is not a post or a page
  4. return;
  5. if(!has_post_thumbnail( $post->ID )) { //the post does not have featured image, use a default image
  6. $default_image="http://example.com/image.jpg"; //replace this with a default image on your server or an image in your media library
  7. echo '<meta property="og:image" content="' . $default_image . '"/>';
  8. }
  9. else{
  10. $thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
  11. echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>';
  12. }
  13. echo "
  14. ";
  15. }
  16. add_action( 'wp_head', 'insert_image_src_rel_in_head', 5 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement