How to Add Social Media Sharing Buttons to WordPress Without a Plugin

How to add social sharing buttons to WordPress without a plugin

Social sharing buttons let visitors post your content to Facebook, Twitter, LinkedIn and other networks. There are many ways to add them without a plugin; here are two. The first uses plain links and loads no third-party scripts at all; the second uses the ShareThis service and gives you share analytics.

Problem: How do you add social media sharing buttons to WordPress without a plugin?

Why not the official widgets

The simplest way is to paste each network’s official widget: the Facebook SDK, Twitter’s widgets.js, LinkedIn’s in.js. It is very simple, but it has two drawbacks. You can’t change the buttons’ styles, and every network adds its own script, so the number of scripts grows with the number of networks and slows the page down. The scripts also track visitors, which under GDPR needs their consent.

Option 1: plain share links

Each major network has a share URL you can link to directly. No JavaScript is required:

function get_share_links( $post_id = null ) {
    $post_id = $post_id ?: get_the_ID();
    $url     = rawurlencode( get_permalink( $post_id ) );
    $title   = rawurlencode( html_entity_decode( get_the_title( $post_id ), ENT_QUOTES, 'UTF-8' ) );

    return array(
        'facebook' => 'https://www.facebook.com/sharer/sharer.php?u=' . $url,
        'twitter'  => 'https://twitter.com/intent/tweet?url=' . $url . '&text=' . $title,
        'linkedin' => 'https://www.linkedin.com/sharing/share-offsite/?url=' . $url,
        'whatsapp' => 'https://wa.me/?text=' . $title . '%20' . $url,
    );
}

// In a template:
foreach ( get_share_links() as $network => $href ) {
    printf(
        '<a href="%s" target="_blank" rel="noopener noreferrer" class="share-btn share-btn--%s">%s</a>',
        esc_url( $href ),
        esc_attr( $network ),
        esc_html( ucfirst( $network ) )
    );
}

get_the_title() returns HTML entities (for example &#8217; for an apostrophe), so the title is decoded before encoding it for the URL. You style the links with your own CSS like any other element.

Option 2: ShareThis custom buttons

Register at sharethis.com and enqueue their script on single posts:

add_action( 'wp_enqueue_scripts', 'helloadmin_sharethis' );
function helloadmin_sharethis() {
    if ( is_single() ) {
        wp_enqueue_script(
            'sharethis',
            'https://platform-api.sharethis.com/js/sharethis.js#product=custom-share-buttons',
            array(),
            null,
            true
        );
    }
}

Then add elements with the st-custom-button class and a data-network attribute. The script turns them into share buttons, and you style them however you like:

<div class="st-custom-button" data-network="facebook"
     data-url="<?php echo esc_url( get_permalink() ); ?>"
     data-title="<?php echo esc_attr( get_the_title() ); ?>">Facebook</div>
<div class="st-custom-button" data-network="twitter"
     data-url="<?php echo esc_url( get_permalink() ); ?>"
     data-title="<?php echo esc_attr( get_the_title() ); ?>">Twitter</div>
<div class="st-custom-button" data-network="linkedin"
     data-url="<?php echo esc_url( get_permalink() ); ?>"
     data-title="<?php echo esc_attr( get_the_title() ); ?>">LinkedIn</div>

Advantages: you control the button styles, and one script serves any number of networks. ShareThis also collects share statistics. The cost is one third-party script on the page.

NOTE: Option 1 is faster, more private and works without JavaScript. Choose Option 2 if you need share analytics.