1. Code
  2. WordPress
  3. Plugin Development

Building a Social Profile Widget: Get Your Follower Count

Scroll to top
Read Time: 6 min

A feature that's becoming more common with websites is the social media profile widget. The widget consists of icons and text that link to a number of social network profiles to which the website is associated. Some of these widgets also show the number of followers on each of their respective social network.

There are a lot of free and premium social profile widget plugins available for WordPress that one can use in linking to his/her personal or website social network's profiles.

In this two-part series, I will show how to get the count of Facebook page Likes, Twitter and Google+ Followers, and how to build a social profile WordPress widget.

We'll be focusing on Facebook, Twitter, and Google+ because they are three of the biggest social networks.

Facebook Likes Count

The Facebook Graph API can be used in a variety of ways to communicate and access Facebook data such as retrieving profile information of users, posting new stories, upload photos, retrieve posts, and a number of other tasks right inside an application.

Using the Graph API, we can programmatically retrieve the number of Likes of a Facebook page. For example, to retrieve the Date founded, About, Name and Likes count of Tuts+ Facebook page, point your browser to to reveal JSON data containing all the information about the page.

Note we are only interested in the page's number of Likes (which is the value of the object property "likes") with respect to the social profile widget we will be building in the second part of this article.

Below is a PHP function that returns the number of Likes of a Facebook page.
This function accepts a Facebook page's username as its argument and returns the number of Likes.

function facebook_count( $username ) {
    $facebook_count = file_get_contents( ''.$username );
    return json_decode( $facebook_count )->likes;

Say we want to get the get the page Likes count of Tuts+ Facebook page, pass the username tutsplus to the function like this:

<?php echo facebook_count( 'tutsplus' ); ?>

Google+ Follower Count

Before you can use Google's API to retrieve information about a Google+ profile or page, you need to obtain a developer API key. Follow the steps outlined below to generate your own API key.

  1. Login to Google Developers Console using your Google's account, click on the Create Project button and fill the form to create a project.
    Google Developer ConsoleGoogle Developer ConsoleGoogle Developer Console
  2. Click the newly created project to go to its dashboard, navigate to the API sub-menu under APIs & auth and activate the Google+ API.
    Google APIGoogle APIGoogle API
  3. To get the API key, navigate to the Credentials page, click the Create new Key and proceed to creating a Server key
    Create a new keyCreate a new keyCreate a new key
  4. Your API key should now be displayed as depicted in the image below.
Public API AccessPublic API AccessPublic API Access

The PHP function below retrieves and return the follower count of a Google+ profile or page.

function googleplus_count( $user, $apikey ) {
    $google = file_get_contents( '' . $user . '?key=' . $apikey );
    return json_decode( $google )->circledByCount;

To use the function, pass a Google+ profile username (with the leading + symbol) or an ID and your API key to the function.

For example, my Google+ user name is +agbonghamacollins and my ID is 116181276412462774298.

Passing either my Google+ username or ID together with my API key to the function as argument will return the number of people following me on G+.

<?php echo googleplus_count( '116181276412462774298', 'AIzaSyC-CXn43NPTUldT4rMAp6hAqzsZfafAMEE' ); ?>
<?php echo googleplus_count( '+agbonghamacollins', 'AIzaSyC-CXn43NPTUldT4rMAp6hAqzsZfafAMEE' ); ?>

Twitter Followers Count

Twitter uses OAuth which is an open protocol to allow secure authorization in a simple and standard method that allows users to approve application to act on their behalf without sharing their password.

To successfully send requests to the Twitter API, you need to create an application with OAuth authorization since unauthorized requests aren't allowed.

To create a Twitter application, you need to login to the Twitter developer dashboard using your Twitter account. The point of creating an application is to give yourself (and Twitter) a set of keys. These are:

  • The consumer key
  • The consumer secret
  • The access token
  • The access token secret

These keys will come in handy when we are to query the API for followers count.

Follow the steps below to create a Twitter application and to generate the keys.

  • Login to Twitter developer account using your Twitter account and navigate to the Application Management console.
  • Click on the Create New App button to initiate the Twitter application creation.
  • Fill the form and click the submit button to create the application.
  • Click on the application, navigate to the Permissions tab and change the Access level to Read and Write.
    If you want to make any decent use of this API, you'll need to change your settings to Read & Write if you're doing anything other than standard data retrieval using GET requests.
    Twitter Demo AppTwitter Demo AppTwitter Demo App

To get your application consumer key and secret, navigate to the API Keys tab.
The API key and API secret is the consumer key and consumer secret, respectively.

Twitter Demo App SettingsTwitter Demo App SettingsTwitter Demo App Settings

To get the application access token and access token secrets, still in the API Keys tab, scroll downward and click on the Create my access token button to create the access tokens.

Access TokensAccess TokensAccess Tokens

Refresh the page and your application Access tokens will be shown to you.

Your Access TokenYour Access TokenYour Access Token

We now have the consumer key and consumer secret as well as the access token and secret keys. Next, we need to programmatically retrieve our number of Twitter followers.

I found a very useful Simple PHP Wrapper for Twitter API that make sending request and receiving response from the API painless.

To use the Twitter PHP wrapper, download and install it via Composer simply by adding the following require statement to your composer.json file:

    "require": {
        "j7mbo/twitter-api-php": "dev-master"

Run $ php composer.phar install to download the library and generate the vendor/autoload.php autoloader file.

Include the vendor/autoload.php or alternatively, download the PHP wrapper in zip archive, extract and include the TwitterAPIExchange.php file which contain the wrapper class.

The function below will retrieve the count of Twitter followers of a user. To use the function, set your Twitter application's consumer key, consumer secret, access token, and access token secret.

function twitter_count( $user ) {
    require_once 'vendor/autoload.php';
	/** Set access tokens here - see: **/
	$settings = array(
	    'oauth_access_token'        => "YOUR_OAUTH_ACCESS_TOKEN",
	    'oauth_access_token_secret' => "YOUR_OAUTH_ACCESS_TOKEN_SECRET",
	    'consumer_key'              => "YOUR_CONSUMER_KEY",
	    'consumer_secret'           => "YOUR_CONSUMER_SECRET"
	$url = '';
	$getfield = '?screen_name=' . $user;
	$requestMethod = 'GET';
	$twitter = new TwitterAPIExchange( $settings );
	$follow_count = $twitter->setGetfield( $getfield )
	                ->buildOauth( $url, $requestMethod )
	$get_count = json_decode( $follow_count, true );
	return $get_count['followers_count'];

To use function, pass the twitter handle or username to the function as its argument.

<?php echo twitter_count( 'tech4sky' ); ?>


In this first part of the series, we've created three PHP functions that handle the retrieval of Facebook page likes, Google+ fans, and Twitter followers.

In the second and last part of this series, we will be building the Social Profile WordPress widget that links to a Facebook, twitter and Google+ profiles and also display the followers count as depicted in the image below.

Be sure to check out the next part of the series for further implementation details. In the meantime, I welcome any and all feedback in the comments.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.