Generate javascript share buttons with PHP
- Wendy
- December 6, 2017
- 1,193
Here is self-explanatory PHP function that will help you to generate Social Sharing links easily.
function share($to, $title, $url) { $url = urlencode($url); $title = urlencode($title); switch ($to) { case 'facebook': return " onclick=\"javascript:window.open(this.href,'', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;\" href=\"http://www.facebook.com/share.php?u=$url&title=$title\" "; break; case 'twitter': return " onclick=\"javascript:window.open(this.href,'', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;\" href=\"http://twitter.com/home?status=$title+$url\" "; break; case 'google+': return " onclick=\"javascript:window.open(this.href,'', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;\" href=\"https://plus.google.com/share?url=$url\" "; break; case 'tumblr': return " onclick=\"javascript:window.open(this.href,'', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;\" href=\"http://www.tumblr.com/share?v=3&u=$url&t=$title\" "; break; case 'pintrest': return " onclick=\"javascript:window.open(this.href,'', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;\" href=\"http://pinterest.com/pin/create/bookmarklet/?url=$url&is_video=false&description=$title\" "; break; case 'linkedin': return " onclick=\"javascript:window.open(this.href,'', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;\" href=\"http://www.linkedin.com/shareArticle?mini=true&url=$url&title=$title\" "; break; case 'reddit': return " onclick=\"javascript:window.open(this.href,'', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;\" href=\"http://www.reddit.com/submit?url=$url&title=$title\" "; break; case 'stumbleupon': return " onclick=\"javascript:window.open(this.href,'', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;\" href=\"http://www.stumbleupon.com/submit?url=$url&title=$title\" "; break; default: return ""; break; } }
To use it, just call the the function with appropriate params
echo share("facebook", "Page Title", "http://page.com/link.html");