This code created by Chad Von, a WordPress and Content Management Systems and eCommerce for Business Websites. I live up north in Coeur d’Alene, Idaho. This code is to make your WordPress Post have better view when shared on Facebook. This code need to be added on your header.php to make it work.
This code is added new metadata for Facebook. This metadata only available on single and page. Let’s get started. Open your header.php and re write this code.
<?php
$thumb = get_post_meta($post->ID,'_thumbnail_id',false);
$thumb = wp_get_attachment_image_src($thumb[0], false);
$thumb = $thumb[0];
$default_img = get_bloginfo('stylesheet_directory').'/images/default_icon.jpg';
?>
<?php if(is_single() || is_page()) { ?>
<meta property="og:type" content="article" />
<meta property="og:title" content="<?php single_post_title(''); ?>" />
<meta property="og:description" content="<?php
$out_excerpt = str_replace(array("rn", "r", "n"), "", get_the_excerpt());
echo apply_filters('the_excerpt_rss', $out_excerpt);
?>" />
<meta property="og:url" content="<?php the_permalink(); ?>"/>
<meta property="og:image" content="<?php if ( $thumb[0] == null ) { echo $default_img; } else { echo $thumb; } ?>" />
<?php } else { ?>
<meta property="og:type" content="article" />
<meta property="og:title" content="<?php bloginfo('name'); ?>" />
<meta property="og:url" content="<?php bloginfo('url'); ?>"/>
<meta property="og:description" content="<?php bloginfo('description'); ?>" />
<meta property="og:image" content="<?php if ( $thumb[0] == null ) { echo $default_img; } else { echo $thumb; } ?>" />
<?php } ?>
Please notes, you need put this code inside <head> and before </head> to make it works
You check more details on Chad Von blogs.



That’s awesome! I never thought to use an if statement with og meta before, that’s really interesting thanks for a top tip
You are welcome
Pingback: Dacă vă dispare editorul vizual la upgrade-ul la Wordpress 3.1
Pingback: Tweak You WordPress Post for better Facebook Share | Jhez3R
The description part it’s a tricky one
You may want to delete or at least comment it out. Other that that … awesomeness.