Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about Facebook and WordPress.

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

Share on Facebook

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

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.