Skip to content

Instantly share code, notes, and snippets.

@petertwise
Created May 20, 2017 06:15
Show Gist options
  • Save petertwise/bae6f8ffe5b955d07dc8b84744e6ba9b to your computer and use it in GitHub Desktop.
Save petertwise/bae6f8ffe5b955d07dc8b84744e6ba9b to your computer and use it in GitHub Desktop.
PHP social sharer function for WordPress w/ FontAwesome
<?php
function squarecandy_get_share_icons($url, $text = '', $twitterhandle = '', $subject = 'I thought you might enjoy this...' ){
// encode url and text parts
$encodedurl = urlencode($url);
$encodedtext = urlencode($text);
$rawencodedtext = rawurldecode($text);
// calculate the allowable tweet length... 140 - ( elipsis and two spaces [5 char] + the url [always counts as 23] + @twittername [calculate length] )
$tweetlength = 140 - ( 5 + 23 + strlen($twitterhandle) );
// build the tweet text
$twittertext = substr( $text, 0, $tweetlength ) . '... ' . $twitterhandle;
// encode tweet text
$twittertext = urlencode($twittertext);
// facebook
// remember to set the facebook og meta tags on the page itself.
// sharer.php only takes the url now; the rest comes from meta data
$output = '<a class="share-facebook share-link" href="https://www.facebook.com/sharer/sharer.php?u=';
$output .= $encodedurl;
$output .= '"><span class="screen-reader-text">share on facebook</span><span class="icon-facebook"></span></a>';
// twitter
$output .= '<a class="share-twitter share-link" href="https://twitter.com/share';
$output .= '?text=' . $twittertext;
$output .= '&amp;url=' . $encodedurl;
$output .= '"><span class="screen-reader-text">tweet this</span><span class="icon-twitter"></span></a>';
// email
$output .= '<a class="share-email share-link" href="mailto:?';
// $output .= 'subject=' . $rawencodedtext . '&amp;';
$output .= 'subject=' . rawurldecode($subject) . '&amp;';
$output .= 'body=%0A' . $rawencodedtext . '%0A' . $encodedurl;
$output .= '"><span class="screen-reader-text">share by email</span><span class="icon-mail4"></span></a>';
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment