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

http://www.facebook.com/sharer.php?u=the_link&t=the_title

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:

http://csi.nfshost.com/goodies/
http://www.webfruits.it/dblog/articolo.asp?articolo=62 (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

<a href="http://www.facebook.com/sharer.php?u=<?php the_permalink();?>&t=<?php the_title(); ?>" target="blank">Share on Facebook</a>

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

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (16 votes, average: 4.31 out of 5)
Loading ... Loading ...
If you found this post useful, please consider a small donation.
» WordPress themes are designs for WordPress - one of the most popular blogging software nowadays.
You will be pleasantly surprised by WordPress Themes provided by Template Monster. All of them are of professional design and high quality.

10 Responses

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

  2. Monkios says:

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

  3. Devon says:

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

  4. [...] Create a “Share on Facebook” link in your WordPress blog [...]

  5. Roger Mitchell says:

    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?

  6. Bachir says:

    Looks like you like WpRecipes…

  7. Hi Sir, thanks for this great tut. It was very helpful. Integrated it in my site http://www.whatsthelatest.net.

    GOD bless and more power.

  8. Says says:

    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: http://www.phresh.cc/fb_screen.jpg

  9. Says says:

    I figured it out. Thanks.

  10. Akosiniko says:

    i was also able to put a “share on facebook” and “retweet button” for my worpress.com blog.
    html codes because java script don’t work.
    Check this guys :)
    Thanks
    http://akosiniko.wordpress.com/2010/05/15/shareonfacebookandretweetbuttons/

Leave a Reply

flash games company