Skip to content

Instantly share code, notes, and snippets.

@ramshengale
Last active July 15, 2021 12:46
Show Gist options
  • Save ramshengale/9106086aacf0858ed9f2a6216a4de1a7 to your computer and use it in GitHub Desktop.
Save ramshengale/9106086aacf0858ed9f2a6216a4de1a7 to your computer and use it in GitHub Desktop.
<?php
/**
* Social share function. Use with
* add_action to inject on frontend
*/
function fs_social_share_icons()
{
$post_id = get_the_ID();
$shareURL = rawurlencode(get_permalink($post_id));
$shareTitle = str_replace(' ', '%20', get_the_title($post_id));
$shareThumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post_id), 'full');
// Email Vars.
$mailSubject = $shareTitle;
$mailBody = $shareTitle . '<br />' . $shareURL;
// Construct sharing URL without using any script.
$twitterURL = add_query_arg([
'text' => $shareTitle,
'url' => $shareURL,
], 'https://twitter.com/intent/tweet');
$facebookURL = add_query_arg([
'u' => $shareURL,
], 'https://www.facebook.com/sharer/sharer.php');
$googleURL = add_query_arg([
'url' => $shareURL,
], 'https://plus.google.com/share');
$linkedInURL = add_query_arg([
'mini' => 'true',
'url' => $shareURL,
'title' => $shareTitle,
], 'https://www.linkedin.com/shareArticle');
$whatsappURL = add_query_arg([
'text' => "{$shareTitle} {$shareURL}",
], 'whatsapp://send');
$pinterestURL = add_query_arg([
'url' => $shareURL,
'media' => $shareThumbnail[0],
'description' => $shareTitle,
], 'https://pinterest.com/pin/create/button/');
?>
<div class="social-share-box">
<a
class="social-share-box-link social-share-email"
href="mailto:?subject=<?php echo esc_attr($mailSubject); ?>&amp;body=<?php echo esc_attr($mailBody); ?>"
>
<span class="fa-stack fa-sm icon-envelope">
<i class="fa fa-square fa-stack-2x"></i>
<i class="fa fa-envelope fa-stack-1x" title="Share on Gmail"></i>
</span>
</a>
<a
class="social-share-box-link social-share-facebook"
href="<?php echo esc_url($facebookURL); ?>"
target="_blank"
>
<span class="fa-stack fa-sm icon-facebook">
<i class="fa fa-square fa-stack-2x"></i>
<i class="fa fa-facebook fa-stack-1x" title="Share on facebook"></i>
</span>
</a>
<a
class="social-share-box-link social-share-twitter"
href="<?php echo esc_url($twitterURL); ?>"
target="_blank"
>
<span class="fa-stack fa-sm icon-twitter">
<i class="fa fa-square fa-stack-2x"></i>
<i class="fa fa-twitter fa-stack-1x" title="Share on Twitter"></i>
</span>
</a>
<a
class="social-share-box-link social-share-google-plus"
href="<?php echo esc_url($googleURL); ?>"
target="_blank"
>
<span class="fa-stack fa-sm icon-gplus">
<i class="fa fa-square fa-stack-2x"></i>
<i class="fa fa-google-plus fa-stack-1x" title="Share on google plus"></i>
</span>
</a>
<a
class="social-share-box-link social-share-linkedin"
href="<?php echo esc_url($linkedInURL); ?>"
target="_blank"
>
<span class="fa-stack fa-sm icon-linkedin">
<i class="fa fa-square fa-stack-2x"></i>
<i class="fa fa-linkedin fa-stack-1x" title="Share on LinkedIn"></i>
</span>
</a>
<a
class="social-share-box-link social-share-whatsapp"
href="<?php echo esc_url($whatsappURL); ?>"
target="_blank"
>
<span class="fa-stack fa-sm icon-whatsapp">
<i class="fa fa-square fa-stack-2x"></i>
<i class="fa fa-whatsapp fa-stack-1x" title="Share on Whatsapp"></i>
</span>
</a>
<a
class="social-share-box-link social-share-pinterest"
href="<?php echo esc_url($pinterestURL); ?>"
target="_blank"
>
<span class="fa-stack fa-sm icon-pinterest">
<i class="fa fa-square fa-stack-2x"></i>
<i class="fa fa-pinterest fa-stack-1x" title="Share on pinterest"></i>
</span>
</a>
</div>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment