Skip to content

Instantly share code, notes, and snippets.

@fernandiez
Created February 23, 2018 08:10
Show Gist options
  • Save fernandiez/9767e5b03496e84a80e8f216b21dd96d to your computer and use it in GitHub Desktop.
Save fernandiez/9767e5b03496e84a80e8f216b21dd96d to your computer and use it in GitHub Desktop.
Social Share Button Snippet simpleshare_socialbuttons function
function simpleshare_socialbuttons($content) {
global $post;
if(is_single() || is_page()){
// Get current page URL
$simpleshareURL = urlencode(get_permalink());
// Get current page title
$simpleshareTitle = str_replace( ' ', '%20', get_the_title());
// Get Post Thumbnail
$simpleshareThumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
// Construct sharing URL
$twitterURL = 'https://twitter.com/intent/tweet?text='.$simpleshareTitle.'&url='.$simpleshareURL.'&via=simpleshare';
$facebookURL = 'https://www.facebook.com/sharer/sharer.php?u='.$simpleshareURL;
$googleURL = 'https://plus.google.com/share?url='.$simpleshareURL;
$linkedInURL = 'https://www.linkedin.com/shareArticle?mini=true&url='.$simpleshareURL.'&title='.$simpleshareTitle;
// Add sharing button at the end of post/page content
$content .= '<!-- simpleshare.com social sharing. Get your copy here: http://simpleshare.me/1VIxAsz -->';
$content .= '<div class="simpleshare-social">';
$content .= '<h5>SHARE ON</h5> <a class="simpleshare-link simpleshare-twitter" href="'. $twitterURL .'" target="_blank">Twitter</a>';
$content .= '<a class="simpleshare-link simpleshare-facebook" href="'.$facebookURL.'" target="_blank">Facebook</a>';
$content .= '<a class="simpleshare-link simpleshare-googleplus" href="'.$googleURL.'" target="_blank">Google+</a>';
$content .= '<a class="simpleshare-link simpleshare-linkedin" href="'.$linkedInURL.'" target="_blank">LinkedIn</a>';
$content .= '</div>';
return $content;
}else{
// if not a post/page then don't include sharing button
return $content;
}
};
add_filter( 'the_content', 'simpleshare_socialbuttons');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment