How to Display Social Share Counts in WordPress as Text


In the first part of this series, I showed you how to programmatically retrieve the share count of various social media networks from their respective APIs using PHP.

In this final part, we will develop a WordPress plugin that displays the number of Facebook likes, Twitter posts, and social share count of a specific URL.


To display the share count of a social network in a WordPress post or page, embed the shortcode of the social network as defined by the plugin. Let’s get started with the plugin development.

Plugin Development

First, create a folder where all of the plugin files will be stored. Let’s name it designmodo-social-count.

Include the plugin header in the plugin PHP file. Without the header, WordPress will not recognize the plugin.


Plugin Name: Designmodo Social Media Count
Plugin URI:
Description: Display social media count.
Version: 1.0
Author: Agbonghama Collins
Author URI:

Create the PHP class and initialize the required methods at the magic __construct.

class Designmodo_Social_Count {

	function __construct() {

		add_shortcode( 'facebook-share', array( $this, 'facebook_share' ) );

		add_shortcode( 'facebook-page-like', array( $this, 'facebook_page_like' ) );

		add_shortcode( 'pinterest-count', array( $this, 'pinterest_count' ) );

		add_shortcode( 'tweet-count', array( $this, 'tweet_count' ) );

		add_shortcode( 'google-plus', array( $this, 'google_plus_count' ) );

		add_shortcode( 'linkedin-share', array( $this, 'linkedin_share' ) );

		add_shortcode( 'stumbledupon', array( $this, 'stumbledupon_share' ) );

In the code above, we defined the shortcode (first function argument) for the various social networks and their respective callback function (second function argument) via the add_shortcode function.

Before we start writing code for the various shortcode callback functions, we’ll create two helper class methods that accept an API call as its parameter and return the JSON output back to the callback function for processing.

Below are the two helper functions: get_response_body and post_response_body

	function get_response_body( $url, $type = '' ) {

		$response = wp_remote_get( $url );
		$body     = wp_remote_retrieve_body( $response );

		// if api call is pinterest, make the response pure json
		if ( $type == 'pinterest' ) {
			$body = preg_replace( '/^receiveCount\((.*)\)$/', '\\1', $body );

		return json_decode( $body );
	function post_response_body( $url ) {

		$query = '
		    "method": "pos.plusones.get",
		    "id": "p",
		    "params": {"nolog": true, "id": "' . $url . '", "source": "widget", "userId": "@viewer", "groupId": "@self"},
		    "jsonrpc": "2.0",
		    "key": "p",
		    "apiVersion": "v1"

		$response = wp_remote_post(
				'headers' => array( 'Content-type' => 'application/json' ),
				'body'    => $query

		return json_decode( wp_remote_retrieve_body( $response ), true );

The initial will handle GET request for the entire social network we are majoring except that of Google+ (a POST request), which will be handled by the latter.

When the shortcode for Facebook likes and share count is active [facebook-share url=""], the callback function facebook_share is called.

function facebook_share( $atts ) {
		$url      = $atts['url'];
		$api_call = '' . $url;

		return $this->get_response_body( $api_call )->shares . ' Facebook Likes & Shares';


The facebook_page_like() below is the callback function for the Facebook page like shortcode.

function facebook_page_like( $atts ) {
		$username = $atts['username'];
		$api_call = '' . $username;

		return $this->get_response_body( $api_call )->likes . ' Facebook Page Like';

The function pinterest_count is the callback function for the Pinterest share shortcode.

function pinterest_count( $atts ) {

		$url      = $atts['url'];
		$api_call = '' . $url;

		return $this->get_response_body( $api_call, 'pinterest' )->count . ' Pinterest Pins';

When the shortcode for the Twitter tweet count is active, tweet_count callback function is called.

function tweet_count( $atts ) {

		$url      = $atts['url'];
		$api_call = '' . $url;

		return $this->get_response_body( $api_call )->count . ' Tweets';


The callback function for LinkedIn share is linkedin_share.

function linkedin_share( $atts ) {
		$url      = $atts['url'];
		$api_call = '' . $url . '&format=json';
		$count    = $this->get_response_body( $api_call )->count;

		return $count . ' LinkedIn Shares';

stumbledupon_share is the callback function for StumbleUpon.

function stumbledupon_share( $atts ) {
		$url      = $atts['url'];
		$api_call = '' . $url;
		$count    = $this->get_response_body( $api_call )->result->views;

		return $count . ' Stumbles';

google_plus_count() is the callback function for the Google+ shortcode.

function google_plus_count( $atts ) {
		$url   = $atts['url'];
		$count = $this->post_response_body( $url )[0]['result']['metadata']['globalCounts']['count'];

		return $count . ' Google Plus';

Below is the singleton method that will instantiate the class to make it active.

static function get_instance() {
		static $instance = false;

		if ( ! $instance ) {
			$instance = new self;


Finally, we will make a call to the singleton method get_instance() to instantiate the class.


Voila! We are done coding our social share count plugin.

How to Use the Plugin

Using the URL as our example:

To get the number of Facebook likes and shares for the URL, use the shortcode [facebook-share url=""]

Pinterest share count [pinterest-count url=""]

Tweet count [tweet-count url=""]

Google PlusOnes [google-plus url=""]

LinkedIn shares [linkedin-share url=""]

StumbleUpon stumbles [stumbledupon url=""]

To display the number of likes a Facebook page has, use this shortcode [facebook-page-like username="designmodo"] where designmodo is the page’s username.

Designmodo Social Share Shortcodes

Say you added all the shortcodes above in a WordPress post or page, you will see the social share count display as depicted in the image below.

Social share count in Action


To further understand how the plugin was built and how you can implement it in your WordPress site, download the plugin. Have a question, contribution or suggestion for code improvement? Let us know in the comments.


  1. Andrea Oct 22, 12:29 am

    Hi, thank you for this great tutorial post!
    I was wondering if you have a smart way to make a full social count using the sum of every social “like” of every service.

    Thank you

  2. NS Jan 2, 5:00 pm

    Kudos! Very quick and easy to understand tutorial.

    I actually came here looking for ‘follower count text’ instead of ‘share count’. Are you able to create a plugin or tutorial for this please?

    • NS Jan 2, 5:01 pm

      Forgot to add, I need to display the’ follower count text’ for multiple social media accounts.

  3. Nico Apr 19, 10:56 pm

    Hi, sorry to bother but if I try to activate this plugin you get an error
    Parse error: syntax error, unexpected ‘[‘ in line 113.

    Best regards

  4. zoomingjapan May 25, 9:15 pm

    I want to put this code into my single.php and thus I cannot add a static URL.
    I need to call the current blog post’s URL with something like “get_permalink”, but I don’t get how to do it.

    Is it possible at all?

    Thank you.

  5. Alex Feb 6, 9:47 pm

    Any updates for this plugin?
    Counr are not visible for some social sites

Leave a Reply

* Minimum length: 20 characters