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.
You will be pleasantly surprised by WordPress Themes provided by Template Monster. All of them are of professional design and high quality.















(33 votes, average: 4.39 out of 5)









This post has 18 comments
Viktor Hesselbom
Very nice. Thanks, I’ve added the same to my blog.
Monkios
Don’t you have a link that does exactly that just below “The geek has spoken” ?
Devon
This is GREAT for getting hits for your website. Thanks for the guide!
10 WordPress 2.7 Hacks That I Have Added This Month | Blogging | So You Want To Teach?
[...] Create a “Share on Facebook” link in your WordPress blog [...]
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…
zplits | what's the latest?
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.
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
Says
I figured it out. Thanks.
Akosiniko
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/
Larry Levenson
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?
Facebook Ad Power
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
Yeah..
Willie » Create a “Facebook Share” button or link
[...] References:: http://www.emanueleferonato.com/2009/04/17/create-a-share-on-facebook-lin-in-your-wordpress-blog/ [...]
Justin
Shanks. Using this code in a custom site I’m building.
Yasir
thanks for sharing nice dude
kostas
I want to add this functionality to my static iframe tab in my facebook fan page.How i can do that?
THANKS