Setting up custom facebook and twitter buttons using php

The canonical tag has been a god send for us helping to get pages indexed and clean up site structures. In many sites we use the same php variable for our share buttons.

This is a great way to setup custom twitter and facebook share buttons for your pages. We always use an external template file and then include it from the pages but you can hard code this into a single web page if required.

small facebook like and twitter retweet buttons

The images are currently set to images/facebookpic.png and images/twitterpic.pngĀ  You could set it to if you’re feeling lazy or google image search facebook button for examples. You could get pretty creative with this if you are aiming to give your site a social/seo boost.

// declare the variables from a seperate page and include(‘mytemplate.php’) or put it all in one page.
$title = ‘My title’;
$canonical = ‘’;

//output the tags required in <head> obviously meta descriptions etc would be added too.
echo ‘<title>’.$title.'</title>
<link rel=”canonical” href=”‘.$canonical.'” />’;

// Buttons below for <body> section. Note there is no follow me function added to the twitter code, this can be added if required.

$fblink = “”.$canonical.”&amp;t=”.urlencode($title).”&amp;src=sp”;
echo(‘<a title=”Share on Facebook” href=”‘.$fblink.'” target=”_blank”><img src=”images/facebookpic.png” alt=”facebook like” border=”0″ /></a>’);
$twitlink = “;count=horizontal&amp;original_referer=”.$canonical.”&amp;text=”.urlencode($title).”&amp;url=”.$canonical;
echo(‘ <a title=”Share on Twitter” href=”‘.$twitlink.'” target=”_blank”><img src=”images/twitterpic.png” alt=”twitter retweet” border=”0″ /></a> ‘);



You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *