Skip to content

Instantly share code, notes, and snippets.

@devonwaldon
Created November 22, 2016 19:26
Show Gist options
  • Save devonwaldon/fc6631f3e89e18f9d32720709b3df9e7 to your computer and use it in GitHub Desktop.
Save devonwaldon/fc6631f3e89e18f9d32720709b3df9e7 to your computer and use it in GitHub Desktop.
PHP function to generate social share URLs
/**
* Generate Social Media Buttons
*/
function get_social_share_url( $network, $post_id_to_share, $username=NULL ) {
$url = '';
$url_to_share = get_permalink($post_id_to_share);
$title_to_share = pl_get_the_title($post_id_to_share);
$summary_to_share = pl_get_the_excerpt($post_id_to_share);
switch ($network) {
case 'facebook':
$url .= 'http://www.facebook.com/sharer/sharer.php?';
$url .= 's=100&';
$url .= 'p[url]='.urlencode($url_to_share).'&';
$url .= 'p[title]='.urlencode($title_to_share).'&';
$url .= 'p[summary]='.urlencode($summary_to_share).'&';
$url .= 'u='.urlencode($url_to_share).'&';
$url .= 't='.urlencode($title_to_share);
break;
case 'twitter':
$url .= 'https://twitter.com/intent/tweet?';
$url .= 'url='.urlencode($url_to_share).'&';
$url .= 'text='.urlencode($title_to_share).'&';
if (isset($username)) {
$url .= 'via='.$username;
}
break;
case 'linkedin':
$url .= 'http://www.linkedin.com/shareArticle?';
$url .= 'mini=true&';
$url .= 'summary='.urlencode($summary_to_share).'&';
$url .= 'title='.urlencode($title_to_share).'&';
$url .= 'url='.urlencode($url_to_share).'&';
if (isset($username)) {
$url .= 'source='.$username;
}
break;
case 'pinterest':
$url .= 'https://www.pinterest.com/pin/create/button/?';
$url .= 'url='.urlencode($url_to_share).'&';
$url .= 'description='.urlencode($summary_to_share).'&';
break;
default:
error_log('Please supply a valid network name for function "social_share_url".');
return false;
break;
}
return $url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment