Add Social Sharing Buttons to Posts in WordPress

How to Add Social Sharing Buttons to Posts in WordPress

in Social Media
Google+ LinkedIn

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);

Share the love

If you like this snippet, share it with others!

3 Replies on "Add Social Sharing Buttons to Posts in WordPress"

  1. Steven

    Hey Jenson,
    thank you for this script. It works really fine!
    Do you know if there is a simple way to add the post-image in the share-action? This would be very useful for sites that focus on visual content which might want to share on pinterest..
    Big ups, Steven

  2. Liam

    Great tutorial! Just what i looking for!

  3. Ben Anderson

    Hello. I pasted the code into the singlular.php file in my child theme at the bottom. How do I make it actually display on the website now? I’m using OceanWp and Elementor.

Leave a Reply

Your email address will not be published. Required fields are marked *