Skip to content

Instantly share code, notes, and snippets.

@vivek-kumar-poddar
Last active April 27, 2017 00:09
Show Gist options
  • Save vivek-kumar-poddar/2d6fffb041b240690eae38fd145d6cda to your computer and use it in GitHub Desktop.
Save vivek-kumar-poddar/2d6fffb041b240690eae38fd145d6cda to your computer and use it in GitHub Desktop.
This code snippets allows users to put social media buttons on their posts. This is code snippet for java-script free social media buttons. https://wpvkp.com/add-social-media-sharing-buttons-to-wordpress-without-plugin/
<?php
// Function to handle the thumbnail request
function get_the_post_thumbnail_src($img)
{
return (preg_match('~\bsrc="([^"]++)"~', $img, $matches)) ? $matches[1] : '';
}
function wpvkp_social_buttons($content) {
global $post;
if(is_singular() || is_home()){
// Get current page URL
$sb_url = urlencode(get_permalink());
// Get current page title
$sb_title = str_replace( ' ', '%20', get_the_title());
// Get Post Thumbnail for pinterest
$sb_thumb = get_the_post_thumbnail_src(get_the_post_thumbnail());
// Construct sharing URL without using any script
$twitterURL = 'https://twitter.com/intent/tweet?text='.$sb_title.'&amp;url='.$sb_url.'&amp;via=wpvkp';
$facebookURL = 'https://www.facebook.com/sharer/sharer.php?u='.$sb_url;
// Based on popular demand added Pinterest too
$pinterestURL = 'https://pinterest.com/pin/create/button/?url='.$sb_url.'&amp;media='.$sb_thumb[0].'&amp;description='.$sb_title;
$gplusURL ='https://plus.google.com/share?url='.$sb_title.'';
// Add sharing button at the end of page/page content
$content .= '<div class="sb-social">';
$content .= '<a href="'. $twitterURL .'" target="_blank"><i class="sb-icon font-twitter"></i></a>';
$content .= '<a href="'.$facebookURL.'" target="_blank"><i class="sb-icon font-facebook"></i></a>';
$content .= '<a href="'.$pinterestURL.'" target="_blank"><i class="sb-icon font-pinterest"></i></a>';
$content .= '<a href="'.$gplusURL.'"><i class="sb-icon font-gplus"></i></a>';
$content .= '</div>';
return $content;
}else{
// if not a post/page then don't include sharing button
return $content;
}
};
// Enable the_content if you want to automatically show social buttons below your post.
// add_filter( 'the_content', 'wpvkp_social_buttons');
// This will create a wordpress shortcode [social].
// Please it in any widget and social buttons appear their.
// You will need to enabled shortcode execution in widgets.
add_shortcode('social','wpvkp_social_buttons');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment