Skip to content

Instantly share code, notes, and snippets.

@DavidEngland
Created June 14, 2017 15:46
Show Gist options
  • Select an option

  • Save DavidEngland/6bce74750ff71ece4544966090dd2683 to your computer and use it in GitHub Desktop.

Select an option

Save DavidEngland/6bce74750ff71ece4544966090dd2683 to your computer and use it in GitHub Desktop.
General function for sharing webpage on social medias. Pass name of the social media you want to share webpage on, or if you want to share a specific page from webpage then pass the url as 2nd argument. See examples for clarification.
<?php
echo "<h3>Share current page</h3>";
echo '<a href="" class="" ' . socialLink( 'facebook' ) . '>Share on Facebook</a><br/>';
echo '<a href="" class="" ' . socialLink( 'twitter' ) . '>Share on Twitter</a><br/>';
echo '<a href="" class="" ' . socialLink( 'google' ) . '>Share on Google</a><br/>';
echo "<h3>Share specific url</h3>";
echo '<a href="" class="" ' . socialLink( 'facebook', 'www.worthers.com' ) . '>Share on Facebook</a><br/>';
echo '<a href="" class="" ' . socialLink( 'twitter' , 'www.worthers.com' ) . '>Share on Twitter</a><br/>';
echo '<a href="" class="" ' . socialLink( 'google' , 'www.worthers.com' ) . '>Share on Google</a><br/>';
<?php
function socialLink( $media, $url = false )
{
switch( $media ) {
case 'facebook':
return 'onclick="window.open(\'https://www.facebook.com/sharer/sharer.php?u=\'+' . ( $url != '' ? "encodeURIComponent('{$url}')" : 'encodeURIComponent(location.href)') . ', \'facebook-share-dialog\', \'width=626,height=436\'); return false;"';
case 'twitter':
return 'onclick="window.open(\'http://twitter.com/share?url=\'+' . ( $url != '' ? "encodeURIComponent('{$url}')" : 'encodeURIComponent(location.href)') . ', \'\',\'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600\'); return false;"';
case 'google':
return 'onclick="javascript:window.open(\'https://plus.google.com/share?url=\'+' . ($url != '' ? "encodeURIComponent('{$url}')" : 'encodeURIComponent(location.href)') . ', \'\',\'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600\'); return false;"';
default:
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment