Howto: Integrate Facebook Open Graph With WordPress |
Howto: Integrate Facebook Open Graph With WordPress Posted: 10 May 2011 07:37 AM PDT Facebook Open Graph protocol allows you to share your blog content not only with your readers, but their Facebook friends as well. The best part is – whenever someone liked your content(s), it will be published on their Facebook profile. But that’s not all, Open Graph allows you to explore more interesting ways to interact and engage with your readers. Ultimately – if this is done right – it builds up your brand and increases your site’s traffic. In today’s post, we are going to look into how to integrate Facebook Open Graph with a self-hosted WordPress in a detailed step-by-step guide. It will require editing your existing WordPress theme and creating a Facebook application (if you don’t have one). Ready? Let’s fire up the browser and your favourite code editor. Full guide after jump.
Step 1: Create a facebook AppWe’ll need an Application ID and to get that, you’ll need to create a Facebook App. If you already have a one, move on to step 2. Creating an application is easy, here’s what you do:
That’s all! You can always come back later to fill up the rest of the information. Step 2: Replace <HTML> TagOpen up your theme’s header file (header.php) in your favorite editor. Always keep a backup copy just in case anything goes wrong. Look for this following line of code, or one that starts with <html xmlns="http://www.w3.org/… <html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>> Replace it with: <html xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://ogp.me/ns#" xmlns:fb="https://www.facebook.com/2008/fbml"> Keep header.php open, we are going to need it for the 3rd step. Step 3: Insert OG <Meta> tagsPaste the following code right after <?php if (have_posts()):while(have_posts()):the_post(); endwhile; endif;?> <!-- the default values --> <meta property="fb:app_id" content="your_fb_app_id" /> <meta property="fb:admins" content="your_fb_admin_id" /> <!-- if page is content page --> <?php if (is_single()) { ?> <meta property="og:url" content="<?php the_permalink() ?>"/> <meta property="og:title" content="<?php single_post_title(''); ?>" /> <meta property="og:description" content="<?php echo strip_tags(get_the_excerpt($post->ID)); ?>" /> <meta property="og:type" content="article" /> <meta property="og:image" content="<?php if (function_exists('wp_get_attachment_thumb_url')) {echo wp_get_attachment_thumb_url(get_post_thumbnail_id($post->ID)); }?>" /> <!-- if page is others --> <?php } else { ?> <meta property="og:site_name" content="<?php bloginfo('name'); ?>" /> <meta property="og:description" content="<?php bloginfo('description'); ?>" /> <meta property="og:type" content="website" /> <meta property="og:image" content="logo.jpg" /> <?php } ?> Here are some of the the values you’ll need to alter:
Step 3a – When "wp_get_attachment_thumb_url" FailWhen <meta property="og:image" content="" /> A simple workaround will be to replace Line 12 with the following code: <meta property="og:image" content="<?php if (function_exists('catch_that_image')) {echo catch_that_image(); }?>" /> Next, open up functions.php and insert the following code: function catch_that_image() { global $post, $posts; $first_img = ''; ob_start(); ob_end_clean(); $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches); $first_img = $matches [1] [0]; if(empty($first_img){ //Defines a default image $first_img = "/images/default.jpg"; } return $first_img; } This replacement code attempts to use a function call Step 4: Insert Facebook Javascript SDKThe following Javascript gives you to access all of the features of the Graph API and Dialogs. It also allows you to integrate Facebook social plugins like the Like button, Facepile, Recommendations, etc with ease. Place it in header.php, right after <div id="fb-root"></div> <script> window.fbAsyncInit = function() { FB.init({appId: 'your_fb_app_id', status: true, cookie: true, xfbml: true}); }; (function() { var e = document.createElement('script'); e.async = true; e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js'; document.getElementById('fb-root').appendChild(e); }()); </script> Replace your_fb_app_id in Line 4 with Application ID from Step 1 earlier. Step 5: Let’s test it!We are done integrating Facebook Open Graph to the WordPress blog. Let’s give it a couple of test to make sure we’ve done things correctly. Test 1: View source codeTake a look at the source codes of one of the blog post, you should have something like this: Check the properties and its values, make sure they are correct. Test 2: Install a Like BoxIf you haven’t install a Facebook Like Button, then it’s probably time to get one. Place the following code anywhere (preferably before content or after content) inside single.php: <fb:like href="<?php the_permalink() ?>" layout="button_count" show_faces="false" width="450" action="like" colorscheme="light"></fb:like> Next, get a friend to Like it. You should see something similar appearing in his Facebook profile: |
You are subscribed to email updates from hongkiat.com To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Google Inc., 20 West Kinzie, Chicago IL USA 60610 |
0 comments:
Post a Comment