Advertisement
sabbirshouvo

FB Post Image Fix

Oct 19th, 2015
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.83 KB | None | 0 0
  1. function insert_fb_image_fix() {
  2.     global $post;
  3.     if ( !is_singular())
  4.         return;
  5.     if(!has_post_thumbnail( $post->ID )) {
  6.        
  7.         $files = get_children('post_parent='.get_the_ID().'&post_type=attachment&post_mime_type=image&order=desc');
  8.           if($files) :
  9.             $keys = array_reverse(array_keys($files));
  10.             $j=0;
  11.             $num = $keys[$j];
  12.             $image=wp_get_attachment_image($num, 'medium', true);
  13.             $imagepieces = explode('"', $image);
  14.             $imagepath = $imagepieces[1];
  15.             $main=wp_get_attachment_url($num);
  16.             echo '<meta property="og:image" content="' . $main . '"/>';
  17.           endif;
  18.            
  19.     }
  20.     else{
  21.         $thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' );
  22.         echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>';
  23.     }
  24.     echo "";
  25. }
  26. add_action( 'wp_head', 'insert_fb_image_fix', 5 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement