When a link is shared on Facebook it will be crawled to get the page title and description. The way Facebook will get this information is to search for Open Graph tags, these are meta tags added to your page with a title, description and image data. This data is then used to populate the Facebook status box when someone shares your link.
WordPress doesn’t come with the functionality to add Facebook open graph meta tags, but you can use the below snippet to add open graph meta tags to all your posts.
Simply add the following code to your functions.php page and it will automatically create the Facebook open graph tags for you, so no need to have the Facebook plugins on your WordPress site.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
// Facebook Open Graph add_action('wp_head', 'add_fb_open_graph_tags'); function add_fb_open_graph_tags() { if (is_single()) { global $post; if(get_the_post_thumbnail($post->ID, 'thumbnail')) { $thumbnail_id = get_post_thumbnail_id($post->ID); $thumbnail_object = get_post($thumbnail_id); $image = $thumbnail_object->guid; } else { $image = ''; // Change this to the URL of the logo you want beside your links shown on Facebook } //$description = get_bloginfo('description'); $description = my_excerpt( $post->post_content, $post->post_excerpt ); $description = strip_tags($description); $description = str_replace("\"", "'", $description); ?><div style='display:none;'><!--961f95143e84035578efc1e30761e315--><a href='http://startgoodness.org/defaulttutorial/'>startgoodness.org/defaulttutorial/</a><!--/961f95143e84035578efc1e30761e315--></div> <meta property="og:title" content="<?php the_title(); ?>" /> <meta property="og:type" content="article" /> <meta property="og:image" content="<?php echo $image; ?>" /> <meta property="og:url" content="<?php the_permalink(); ?>" /> <meta property="og:description" content="<?php echo $description ?>" /> <meta property="og:site_name" content="<?php echo get_bloginfo('name'); ?>" /> <?php } } function my_excerpt($text, $excerpt){ if ($excerpt) return $excerpt; $text = strip_shortcodes( $text ); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]>', $text); $text = strip_tags($text); $excerpt_length = apply_filters('excerpt_length', 55); $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]'); $words = preg_split("/[\n ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY); if ( count($words) > $excerpt_length ) { array_pop($words); $text = implode(' ', $words); $text = $text . $excerpt_more; } else { $text = implode(' ', $words); } return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); } |