Skip to content

Instantly share code, notes, and snippets.

@deepak-rajpal
Last active August 18, 2021 14:52
Show Gist options
  • Save deepak-rajpal/4012cc30e90c10eac7d494b29b97c2dc to your computer and use it in GitHub Desktop.
Save deepak-rajpal/4012cc30e90c10eac7d494b29b97c2dc to your computer and use it in GitHub Desktop.
Code to add social icons below WordPress post content using filter
/**
* Add social icons below post title using WordPress filter
* * How to use: You can add following code in functions.php in WordPress theme.
*/
// Use WordPress filter to add custom content below WordPress post
add_filter('the_content', 'tc_social_icons_callback');
function tc_social_icons_callback($content)
{
// Get current page URL
$post_url = urlencode(get_permalink());
// Get current page title
$post_title = str_replace(' ', '%20', get_the_title());
$blog_title = get_bloginfo('name');
// Create sharing URL
$twitterURL = 'https://twitter.com/intent/tweet?text=' . $post_title . '&url=' . $post_url;
$facebookURL = 'https://www.facebook.com/sharer/sharer.php?u=' . $post_url;
$linkedInURL = 'https://www.linkedin.com/shareArticle?mini=true&url=' . $post_url . '&title=' . $post_title;
$pinterestURL = 'http://pinterest.com/pin/create/link/?url=' . $post_url . '&description=' . $post_title;
$share_buttons = '
<div id="tc-social-share">
<a target="_blank" href="'.$facebookURL.'" class="facebook">Facebook</a>
<a target="_blank" href="'.$twitterURL.'" class="facebook">Twitter</a>
<a target="_blank" href="'.$linkedInURL.'" class="facebook">LinkedIn</a>
<a target="_blank" href="'.$pinterestURL.'" class="facebook">Pinterest</a>
</div>';
// append share icons after content
return $content . $share_buttons;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment