What's WP
Our everything is about WordPress - best choice of WordPress users!
How to Showcase Twitter Follower Statistics in WordPress

How to Showcase Twitter Follower Statistics in WordPress

In order to showcase the website popularity within the Twitter community, many webmasters have displayed the Twitter follower statistics in their WordPress sites. Generally, people are more likely to become a loyal reader for the site featuring a large Twitter follower base. Therefore, if you are active and popular among Twitter users, you can show the follower count to win more potential visitors.

In the following, we’d like to present the detailed steps helping you display the count with ease. Note that the process is easy to handle, and does not cost too much time and energy.

Add a Follow Button with Count

One of the easiest ways to achieve this goal is making use of Twitter follower button that allows the website visitors to follow your Twitter account easily and directly from the webpages. You can do this with the help of some user-friendly WordPress plugins, but we suggest you using Javascript for further configuration with the data attributes.

We have already come out the needed Javascript code in the following. The only thing you need to do is to copy and paste the lines of code into the place you want.

<a href="https://twitter.com/whatswp" class="twitter-follow-button" data-show-count="false" data-lang="en">Follow @whatswp</a>

<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>

Don’t paste the code completely the same as the example. In the first line of code, you need to add the URL of your own Twitter profile, as well as the right Twitter name after the symbol of @.

According to this default Javascript code of Twitter follow button, you can find that the follower count is not displayed with the follow button generally. However, you can make a change using the data-show-count attribute. It is very simply. You only need to replace the word false after “data-show-count=” into the word true.

If you have already integrated the Tweet button with your website, then you only need to add the first line of code. See the right example in the following.

<a href="https://twitter.com/whatswp" class="twitter-follow-button" data-show-count="true" data-lang="en">Follow @whatswp</a>

Now, you can open up your theme directory to paste the code into header.php file or sidebar.php file. In addition, you can also create a new widget with the code into it. To do this, you can go to WordPress dashboard < Appearance < Widget. Then, drag a new text widget to the sidebar area, and add the code into the blank space. add new widget

Follower Statistics in Plain Text

This method is a little bit technical. Firstly, you need to get your follower count from Twitter API by creating the Twitter app. After logging into this Twitter app page and sign in with your account, you can click the Create New App button.

In the next screen, you need to fill out the application page with the application name, application description, website URL, and callback URL. Here, you can use your website name as the application name, and use the website URL as the callback URL. After agreeing with the app rules, you can click on Create Your Twitter Application button.

application page

In the next page, you can get the Consumer Key and Consumer Secret needed for the next step by clicking Create my access token button.

Now, simply open the functions.php file of your theme directory, and paste the following lines of code into it.

function getTwitterFollowers($screenName = 'whatswp')
{
require_once('Cache.php');
require_once('TwitterAPIExchange.php');
$settings = array(
    $consumerKey = 'YOUR_CONSUMER_KEY',
    $consumerSecret = 'YOUR_CONSUMER_SECRET',
$oauth_access_token = 'YOUR_OAUTH_ACCESS_TOKEN',
$oauth_access_token_secret = '"YOUR_OAUTH_ACCESS_TOKEN_SECRET"'
);
$cache = new Cache();
    // get follower count from cache
    $numberOfFollowers = get_transient('cfTwitterFollowers');
    // cache version does not exist or expired
    if (false === $numberOfFollowers) {
        // forming data for request
        $apiUrl = "https://api.twitter.com/1.1/users/show.json";
        $requestMethod = 'GET';
        $getField = '?screen_name=' . $screenName;
 
        $twitter = new TwitterAPIExchange($settings);
        $response = $twitter->setGetfield($getField)
             ->buildOauth($apiUrl, $requestMethod)
             ->performRequest();
 
        $followers = json_decode($response);
        $numberOfFollowers = $followers->followers_count;
         // cache for an hour
        set_transient('cfTwitterFollowers', $numberOfFollowers, 1*60*60);
        update_option('cfNumberOfFollowers', $numberOfFollowers);
    }
    return $numberOfFollowers;
}

Then, open the sidebar.php or header.php, and add the following code into it.

require_once(getTwitterFollowers.php);
echo getTwitterFollowers(whatswp);

Author

Susan Rose is a freelance writer who has a deep knowledge about WordPress. She loves everything related to website building since the freshman year at Christopher Newport University, and loves to share her experience with people all over all the world. This girl is popular among our readers as she is outgoing and energetic. In the daily life, Susan loves detective novels as it can train her logical thinking and creativity.

Close

Want to Learn More About WordPress?

Get Our Free Themes, Plugins, Support, SEO Tips, and Other Tutorials!

Email *