Skip to content

Instantly share code, notes, and snippets.

@madila
Created April 19, 2018 11:13
Show Gist options
  • Save madila/33f5e4250f19be15b6c6890f4c1a4090 to your computer and use it in GitHub Desktop.
Save madila/33f5e4250f19be15b6c6890f4c1a4090 to your computer and use it in GitHub Desktop.
OFC Social Sharing Buttons
<?php
/*
Drop In Function for your theme.
Usage in the loop: ofc_social_sharing_buttons(get_the_ID());
*/
function ofc_social_sharing_buttons($id = null) {
global $post;
$id = ($id) ? $id : $post->ID;
// Get current page URL
$ofcURL = urlencode(get_permalink($id));
// Get current page title
$ofcTitle = str_replace( ' ', '%20', get_the_title($id));
// Get Post Thumbnail for pinterest
$ofcThumbnail = wp_get_attachment_image_src( get_post_thumbnail_id($id), 'full' );
// Construct sharing URL without using any script
$twitterURL = 'https://twitter.com/intent/tweet?text='.$ofcTitle.'&amp;url='.$ofcURL.'&amp';
//$twitterURL .= ';via=ofc';
$facebookURL = 'https://www.facebook.com/sharer/sharer.php?u='.$ofcURL;
//$googleURL = 'https://plus.google.com/share?url='.$ofcURL;
//$bufferURL = 'https://bufferapp.com/add?url='.$ofcURL.'&amp;text='.$ofcTitle;
$whatsappURL = 'whatsapp://send?text='.$ofcTitle . ' ' . $ofcURL;
//$linkedInURL = 'https://www.linkedin.com/shareArticle?mini=true&url='.$ofcURL.'&amp;title='.$ofcTitle;
// Based on popular demand added Pinterest too
$pinterestURL = 'https://pinterest.com/pin/create/button/?url='.$ofcURL.'&amp;media='.$ofcThumbnail[0].'&amp;description='.$ofcTitle;
// Add sharing button at the end of page/page content
$content .= '<div class="ofc-social">';
$content .= '<h5>Share On</h5>';
$content .= '<a class="btn btn-border ofc-social-link ofc-twitter mr-2 mb-1" href="'. $twitterURL .'" target="_blank"><i class="fa fa-twitter mr-1"></i> Twitter</a>';
$content .= '<a class="btn btn-border ofc-social-link ofc-facebook mr-2 mb-1" href="'.$facebookURL.'" target="_blank"><i class="fa fa-facebook mr-1"></i> Facebook</a>';
$content .= '<a class="btn btn-border ofc-social-link ofc-whatsapp d-inline-block d-sm-none mr-2 mb-1" href="'.$whatsappURL.'" target="_blank"><i class="fa fa-whatsapp mr-1"></i> WhatsApp</a>';
//$content .= '<a class="ofc-link ofc-googleplus" href="'.$googleURL.'" target="_blank">Google+</a>';
//$content .= '<a class="ofc-link ofc-buffer" href="'.$bufferURL.'" target="_blank">Buffer</a>';
//$content .= '<a class="ofc-link ofc-linkedin" href="'.$linkedInURL.'" target="_blank">LinkedIn</a>';
$content .= '<a class="btn btn-border ofc-social-link ofc-pinterest mb-1" href="'.$pinterestURL.'" data-pin-custom="true" target="_blank"><i class="fa fa-pinterest mr-1"></i> Pin It</a>';
$content .= '</div>';
echo $content;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment