Adding Facebook Like

This post has been marked as old. The code might no longer work. Comments have been disabled as this is no longer maintained.

You can get the facebook code from Facebook directly. Instead of manually adding the open graph tags to the header, use WordPress SEO or MFields Open Graph Meta Tags to add it for you.

Adding the Facebook Like button to your website is pretty easy when you’re running a standard template – you drop some code in the header.php and single.php files. But if you’re running Genesis or Thesis, you’ll have to  use functions to add it.

For both of these themes there are plugins that let you add PHP code through the WordPress interface ( Thesis OpenHook and Genesis Simple Hooks). I would highly suggest not using these plugins, because if you type something wrong you might break your website and not be able to log back in to fix it.

For both of these themes I’ll be sticking the Facebook Like code right above the post content on the individual posts. If you would like it placed elsewhere, refer to a visual guide (Thesis Visual Hook Reference or Genesis Visual Hook Reference).

Facebook Like in Thesis

Paste this code in your custom_functions.php file:

The first part checks to see if it’s a single post (so not the homepage, category page…) and if it is it sticks the facebook code right before the post content.

The second part sticks some information that Facebook wants in the <head> of the site. If it’s a single post (which it should always be since that’s only where the like code shows up) then it uses the title of the page. If it’s not a single post (if you changed the is_single() in the first code block to something else) it will show the site name followed by the site description. I then specify the image to use as the logo (location: /custom/images/logo.jpg). If you want to use a different image make sure to change this.

Facebook Like in Genesis

In your child theme, place this code in functions.php:

The first part checks to see if it’s a single post (so not the homepage, category page…) and if it is it sticks the facebook code right before the post content.

The second part sticks some information that Facebook wants in the <head> of the site. If it’s a single post (which it should always be since that’s only where the like code shows up) then it uses the title of the page. If it’s not a single post (if you changed the is_single() in the first code block to something else) it will show the site name followed by the site description. I then specify the image to use as the logo (location: /images/logo.jpg). If you want to use a different image make sure to change this.

chat28 Comments

  1. says

    Thanks for the tutorial, Bill! I will certainly be adding the Facebook Like button to one of my Thesis sites today! Best part of buying thesis is the community of devs like yourself who post awesome tutorials for the average blogger like myself to leverage and spice up my sites.

  2. says

    Bill we just started using Thesis and are hevey social media users. we have started using Dig Dig for this. is there a better way or any reason Dig Dig might cause similar issues like the above plugins?

    • Bill Erickson says

      I’ve never used Dig Dig, but there’s nothing wrong with using a plugin to do it. I personally try to reduce the number of plugins used to the bare minimum, so I try to find ways to do things without the plugins (like this tutorial).

      The issues I mentioned above are specifically for those plugins – they don’t apply to others. Thesis OpenHook let’s you write code within the WordPress interface, and if you write the wrong code then you break your site.

  3. says

    Hey Bill,

    I added the code to the custom.functions.php but it’s not working. I just upgraded to 1.8. Does this work for the new version too?
    Thanks.

  4. says

    Thanks Bill,

    I got it to work mostly … but no matter what post I click on it grabs the Site name and Site description only.

    I have this setup on a site im working on behind maintenance mode so I can’t give you a link.

    Also when it grabs site name and description there is no space between “site””description”

    it is thesis 1.8

    • Bill Erickson says

      That’s strange. You might experiment by changing the property=”og:title”. Something like: if(is_single() … “test1″ … }else { … “test2″ };

      It sounds like it’s pulling the title from the }else{ condition, but if you’re on a single page it should pull it from the is_single() condition. Are you using any SEO plugins that rewrite site titles?

      • says

        Nope… no SEO plugins
        I’ve got
        Contact Form 7
        Reveal ID’s
        Stream Video Player
        Widget Logic
        Wishlist Member
        WP-DB Manager
        WP-email
        wp-prettyphoto

        Its weird
        I’ve disabled all plugins and same result
        I’ve taken out all custom_functions (except above) and same result
        I’ve change the WP title and desc to “blank” and same result

        I put the above code on a fresh install and another older install and works just fine

        stumped

  5. says

    Hi, Bill,

    Excellent tutorial. I have one question. In the picture at the top of the tutorial, the “example” implementation of this tutorial, the “like” button is after the posting date. In my attempt at this tutorial, the like button is between the Title and the Meta section. How can I get it inline with the meta like the example picture? Before or after? Using Genesis…

    Thanks in advance… Don.

    • Bill Erickson says

      Ah, you caught that! :)

      I simplified the tutorial to focus on the code for Facebook. In the screenshot, I used the following for the “In Single Post” part:

      add_filter('genesis_post_info', 'post_info_filter');
      function post_info_filter($post_info) {
      $facebook = '<iframe src="http://www.facebook.com/plugins/like.php?href=<?php the_permalink() ?>&amp;layout=button_count&amp;show_faces=true&amp;width=250&amp;height=
      20px&amp;action=like&amp;font=trebuchet+ms&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:250px; height:20px; padding-left: 10px;"></iframe>';
      if(is_single())
          $post_info = $post_info.$facebook;
          return $post_info;
      }

    • Bill Erickson says

      You’re running News, which is a child theme of Genesis. Find the functions.php file in the News theme folder, and paste the code above marked “Facebook Like in Genesis”

  6. says

    I’m using genesis framework, and news theme also, works great! One question though, I get this:
    Warren likes Site Redesign on PLR Internet Marketing.

    (you can see the title tags on my facebook page which I’m sure isn’t supposed to happen.)

    • Bill Erickson says

      That’s exactly how it’s supposed to happen. It uses whatever is in the doctitle as the name of the page. If you want this to be something different, change the code in the function facebook_like_head()

  7. says

    Thanks for this great tutorial. I want to not display the button on selective posts and pages. How would I do that.

    I mean

    if (is_post(4,12,13)) or (is_page(2,245))
    {do nothing}
    else
    {show button}

    How would I do that?

    • Bill Erickson says

      Refer to the conditional tags page to see how to do that: http://codex.wordpress.org/Conditional_Tags

      Instead of putting your code in the else, you can test for NOT by using !

      So if you want it to not show up on the about or contact pages, do

      if( !is_page( 'about' ) && !is_page( 'contact' ) ) {
      do_something();
      }

      That can be read “if this is not the about page and it’s not the contact page, do the do_something() function.”

      For posts, use is_single() instead of is_post()

  8. John says

    Got following error
    Parse error: syntax error, unexpected ‘<' in /home/content/27/7477727/html/wp-content/themes/church/functions.php on line 81

    using Genesis, church child theme. Please help. Thanks in advance.

    • Bill Erickson says

      I’ll need to see the full code of your functions.php file to see what you did wrong, but my guess is you included a

      • says

        in fact it does give a “Parse error, unexpected “<"…." I am using Genesis Framework 1.8.2 and Décor child theme. I can restore the site by removing your code from function.php.