Add Social Sharing Buttons to Posts in WordPress


I was working on a WordPress theme for a client and he requested me to add couple of social sharing buttons on his website. So I started browsing through the social sharing plugins for WordPress and found that most were either too complicated or bloated for the simple feature my client needed. I didn’t want to use a bloated plugin to weight down his WordPress website in any way since my client only need a couple of buttons at the end of the content to share article on popular social networks.

So I started digging and it turned out that it is actually really easy to add sharing options to your posts without having to load any scripts. Twitter, Facebook, Google+ and LinkedIn all provide a way to call their sharing functionality by linking to a URL and pass some parameters like the text or URL of the item to be shared. With most of these social networks, you only need to supply article URL and they fetch article title, introduction text and article image automatically.

So I created this simple WordPress snippet for him, which you can also paste in your single.php to add social sharing buttons from Twitter, Facebook, Google+ and LinkedIn.

<div class="social-sharing-links">
    <a href="<?php echo esc_html( get_the_title() ); ?>&url=<?php echo esc_url( get_the_permalink() ); ?>&via=WPCodeSnippet" class="twitter" target="_blank">Share on Twitter</a>
    <a href="<?php echo esc_url( get_the_permalink() ); ?>" class="facebook" target="_blank">Share on Facebook</a>
    <a href="<?php echo esc_url( get_the_permalink() ); ?>" class="gplus" target="_blank" >Share on Google+</a>
    <a href="<?php echo esc_url( get_the_permalink() ); ?>&title=<?php echo esc_html( get_the_title() ); ?>&" class="linkedin" target="_blank" >Share on LinkedIn</a>

If you want to style your social sharing buttons then you can use following CSS or you can define your own CSS styles for these buttons in your theme’s style.css.

/* CSS styles for social sharing buttons */ {
    margin-bottom: 2em;
} a {
    display: inline-block;
    color: #FFF;
    font-size: 13px;
    font-weight: bold;
    margin-right: 1em;
    padding: 8px 10px;
    background-color: rgba(0, 0, 0, 0.3);
    -webkit-border-radius: 3px;
    -moz-border-radius: 3px;
    border-radius: 3px;
    text-decoration: none;
} a:hover {
    background-color: rgba(0, 0, 0, 0.5);
} a.facebook {
    background: rgba(59, 89, 152, 0.7);
} a.facebook:hover {
    background: rgba(59, 89, 152, 1);
} a.twitter {
    background: rgba(0, 172, 237, 0.7);
} a.twitter:hover {
    background: rgba(0, 172, 237, 1);
} a.gplus {
    background: rgba(221, 75, 57, 0.7);
} a.gplus:hover {
    background: rgba(221, 75, 57, 1);
} a.linkedin {
    background: rgba(0, 123, 182, 0.7);
} a.linkedin:hover {
    background: rgba(0, 123, 182, 1);


Leave a Reply

Random Post Selection ::

Recent Posts

Unlock Unlimited Downloads