22Mar/11Off
‘Share’ links generator
I don't know about you, but I spend far too much of my time creating those blasted 'Share' links. Well, I did, until I wrote this pair of handy functions.
They will create share links for Facebook, twitter, delicious and StumbleUpon on the fly. Additionally you can pass any URL to create a share link to, and a title if you like.
You can create more definitions very easily by adding to the $types
array. Use the terms #URL#
and #TITLE#
for the URL and title, or #UURL#
and #UTITLE#
for the urlencode()
versions.
function share($type=null,$url=null,$title=null){ // I store various page data in an $args array. // For this function, I'm be using the page title. // If you don't have a page title, it'll be ignored. global $args; // Type definitions $types=array( 'fb'=>'http://www.facebook.com/sharer.php?u=#UURL#&t=#UTITLE#', 'tw'=>'http://twitter.com/home?status=#UTITLE# #UURL#', 'dl'=>'http://www.delicious.com/save?url=#UURL#&title=#UTITLE#', 'sl'=>'http://www.stumbleupon.com/submit?url=#UURL#&title=#UTITLE#' ); // Sanity checks if(!$type)return 'NO TYPE REQUESTED'; if(!array_key_exists($type,$types))return 'UNDEFINED TYPE REQUESTED'; // Build the share link if(!$url)$url=thisurl(); $title=(!$title&&$args['page']['title'])?$args['page']['title']:$title; $find=array('#URL#','#TITLE#','#UURL#','#UTITLE#'); $replace=array($url,$title,urlencode($url),urlencode($title)); return str_replace($find,$replace,$types[$type]); } function thisurl(){ // Return the full page URL $host=strtolower(array_shift(explode('/',$_SERVER['SERVER_PROTOCOL']))).'://'; $uri=$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; return($host.$uri); } // Usage: echo share('fb'); echo share('tw'); echo share('dl'); echo share('su');
Enjoy this article?
Tagged as: delicious, Facebook, share links, StumbleUpon, twitter
Comments Off
"Anything anyone says is either obvious or wrong."
Grandpa Simspon
Grandpa Simspon