Tag Archives: share

Add a share/like button to a PHP web page

There are a few things to consider when adding share and like buttons to a php web page.

Firstly, you must ensure that you have your Open Graph tags in place within the <head> section of the site.

<meta property="og:title" content="Page Title" />
<meta property="og:type" content="type (see facebook for types)" />
<meta property="og:image" content="url of image for page" />
<meta property="og:site_name" content="Overall site name" />

If you have developed your own CMS, you should dynamically fill the title tag with the same title as the page itself.

Once these are set up you then need to consider what type of button do you want?  Do you want a share button, which allows your guests to post the page to your timeline, to a friends timeline or to your pages or groups. Or do you simply want a like button?

Personally, even though facebook says the share button is deprecated I still use it.  it is used within facebook all the time so I can’t see why they would stop it being used and it is far more powerful in my opinion.

So the next step is to create the code for the buttons.  One thing you will need before proceeding is the url of the page.  If you need to generated this dynamically use the following code (or similar):

$pageurl = urlencode("http://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]);

This will get you the url of the page in the following format: http://server-name/pagename and urlencode with encode the slashes and any other parameters for safe use in the button code.

To generated a share button we use the sharer.php url and appen our page url as follows

<a href="http://www.facebook.com/sharer/sharer.php?u=<?php echo $pageurl;?>">
<img border="0" src="images/facebook-share.jpg" />
</a>

In this instance you will notice that it is simply a standard image link, so you must create an image and store this somewhere on your sever.

This is the one I created if you need one that looks a bit like the ‘like’ button:  facebook-share

To create a like button use the following code:

<iframe src="http://www.facebook.com/plugins/like.php?href=
<?php echo $pageurl;?>&amp;layout=button_count&amp;show_faces=false&amp;
width=100&amp;action=like&amp;font&amp;colorscheme=light&amp;height=21" 
scrolling="no" frameborder="0" style="border:none; overflow:hidden; 
width:100px; height:21px;" allowTransparency="true"></iframe>

Notice the pageurl included and play around with the other parameters.  These are just defaults that I use.