Create a “Share on Facebook” link in your WordPress blog

With its over 200 millions registered users (read 200 Million Strong) Facebook can bring a lot of traffic to your blog, so in my opinion it’s very important to add a “Share on Facebook” button to your blog.

Facebook Sharer

If you noticed, Facebook as a sharer url allowing you to share a link.

I am going to use this feature to create the button.

sharer.php needs two variables to share your page: u for the link and t for the title, so we need to create a link like

WordPress functions

The function that returns URL for the permalink to the post currently being processed is the_permalink(). You can read more information about it at the official page.

The function to get the title is (guess what?) the_title. More information at the official page.

Some icons

Just in case you are looking for some interesting Facebook icons, I am giving you a couple of links: (this one is in italian, just go on download button at the end of the page).

If you know more links about free Facebook icons, just let me know and I’ll add them here.

The code

Time to write some code now… in your wp-content/themes/ folder of your WP installation, locate the folder of your theme and check for single.php file.

Warning: not all themes have a single.php file… if you can’t find it, then probably the right file to edit is index.php… anyway it could be the right file if you see the_content() function.

The code I suggest you to add, after the_content, is

And you’ll have your Facebook link like the one I added on my blog.

  • Very nice. Thanks, I’ve added the same to my blog.

  • Don’t you have a link that does exactly that just below “The geek has spoken” ?

  • This is GREAT for getting hits for your website. Thanks for the guide!

  • Pingback: 10 WordPress 2.7 Hacks That I Have Added This Month | Blogging | So You Want To Teach?()

  • Roger Mitchell

    This seems to be exactly what I want, but as a new user I am unable to find the wp-content/themes/ folder and therefore can’t find the single.php file.
    I go to appearance, themes and it shows my current theme at the top. Do I then go to edit CSS? Can you guide me on how to find the folder and file I need to change?

  • Bachir

    Looks like you like WpRecipes…

  • Hi Sir, thanks for this great tut. It was very helpful. Integrated it in my site

    GOD bless and more power.

  • Thanks for the code. I was able to place it in my wordpress theme but there’s one thing that I’m having a problem with.

    the link works fine but unlike on your site, The title of the story does not show up, neither does the sentence or two that follows the title. Instead all I get the site link and the site name.

    Here’s a screen shot of what I’m getting:

  • I figured it out. Thanks.

  • i was also able to put a “share on facebook” and “retweet button” for my blog.
    html codes because java script don’t work.
    Check this guys :)

  • Very helpful. Exactly what I was looking for. Thank you!

  • Ted

    Unfortunatelly, this won’t validate:

    Whitespace in query component. Use %20 in place of spaces.

    How to use %20 for titles with spaces in between?

  • Having a facebook share button has become a necessary thing for blogs. Instead using of plugins we can use this code.
    Thank you for sharing this simple tips.

    • Kyx


  • Pingback: Willie » Create a “Facebook Share” button or link()

  • Justin

    Shanks. Using this code in a custom site I’m building.

  • thanks for sharing nice dude

  • I want to add this functionality to my static iframe tab in my facebook fan page.How i can do that?

  • mike

    thanks for this tutorial. i was able to make the link work. but i think i missed the part where the fb icon is supposed to show up. instead only the “Share on Facebook” text is showing :( hope you can explain this part…

  • its not work facebook new tos

  • Have you ever considered about adding a little bit more than just
    your articles? I mean, what you say is fundamental
    and everything. However just imagine if you added some great
    visuals or video clips to give your posts more, “pop”!
    Your content is excellent but with images and videos, this blog
    could definitely be one of the very best in its field.
    Terrific blog!

  • Jamie

    Even though this is a few years old it seems to work properly for me. Thanks, this is a very easy way to add a share button.

  • great job man

  • simple….and its work (y)

  • My programmer is trying to convince me to
    move to .net from PHP. I have always disliked the idea because of the expenses.
    But he’s tryiong none the less. I’ve been using Movable-type on various websites
    for about a year and am nervous about switching to another platform.
    I have heard fantastic things about Is
    there a way I can transfer all my wordpress posts into it?

    Any help would be really appreciated!

  • The Facebook Share Dialog no longer supports custom URL parameters, such as “t” for Title as you have indicated in this article. You have to implement Open Graph tags in order to customize what is shared. I wrote more about this here: