Add Twitter Followers Count In WordPress

16 August, 20130 Comments
2 Flares Twitter 1 Facebook 1 Google+ 0 Pin It Share 0 StumbleUpon 0 2 Flares ×

twitter follower count

You can easily display the number of twitter followers in your wordpress website by just using the offical Twitter Follower Button.

But if you dont want to use this(as it sometimes slows your website) then there is something really interesting for you.

You can add the twitter follower count as text in your wordpress website.

You may also like How To Add Sitemap To Footer Of WordPress Blog

So lets begin

For this, you would first need to create a twitter application so that we can use the Twitter API v1.1.

How To Create  The Twitter App :

  • Firstly, Click Here to go to the Twitter Account Page.
  • Then sign in with you Twitter Account.
  • Then create a new application and fill in the required details.
  • Then click on Create My Access Token button.
  • Then you would get a notification stating that your authorization token has been created.
  • Now you would need only Consumer key and Consumer secret.

Now copy the following code in the functions.php file

function getTwitterFollowers($screenName = 'wpbeginner')
{
    // some variables
    $consumerKey = 'YOUR_CONSUMER_KEY';
    $consumerSecret = 'YOUR_CONSUMER_SECRET';
    $token = get_option('cfTwitterToken');

    // get follower count from cache
    $numberOfFollowers = get_transient('cfTwitterFollowers');

    // cache version does not exist or expired
    if (false === $numberOfFollowers) {
        // getting new auth bearer only if we don't have one
        if(!$token) {
            // preparing credentials
            $credentials = $consumerKey . ':' . $consumerSecret;
            $toSend = base64_encode($credentials);

            // http post arguments
            $args = array(
                'method' => 'POST',
                'httpversion' => '1.1',
                'blocking' => true,
                'headers' => array(
                    'Authorization' => 'Basic ' . $toSend,
                    'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8'
                ),
                'body' => array( 'grant_type' => 'client_credentials' )
            );

            add_filter('https_ssl_verify', '__return_false');
            $response = wp_remote_post('https://api.twitter.com/oauth2/token', $args);

            $keys = json_decode(wp_remote_retrieve_body($response));

            if($keys) {
                // saving token to wp_options table
                update_option('cfTwitterToken', $keys->access_token);
                $token = $keys->access_token;
            }
        }
        // we have bearer token wether we obtained it from API or from options
        $args = array(
            'httpversion' => '1.1',
            'blocking' => true,
            'headers' => array(
                'Authorization' => "Bearer $token"
            )
        );

        add_filter('https_ssl_verify', '__return_false');
        $api_url = "https://api.twitter.com/1.1/users/show.json?screen_name=$screenName";
        $response = wp_remote_get($api_url, $args);

        if (!is_wp_error($response)) {
            $followers = json_decode(wp_remote_retrieve_body($response));
            $numberOfFollowers = $followers->followers_count;
        } else {
            // get old value and break
            $numberOfFollowers = get_option('cfNumberOfFollowers');
            // uncomment below to debug
            //die($response->get_error_message());
        }

        // cache for an hour
        set_transient('cfTwitterFollowers', $numberOfFollowers, 1*60*60);
        update_option('cfNumberOfFollowers', $numberOfFollowers);
    }

    return $numberOfFollowers;
}</pre>
<span style="font-family: Consolas, Monaco, monospace; font-size: 12px; line-height: 18px;">

In the above code, replace Consumer Key and Consumer Secret with your own Consumer key and Secret.

Then add the following code where you want the twitter count to be displayed on your website.

 <?php echo getTwitterFollowers('your_screen_name');?> 

Thanks to Zvonko Biškup

Hope You liked the post :)

Thus you can now add twitter followers count to your wordpress website.

Filed in: WordPressWordPress Tutorials
Tagged with:

About the Author ()

I am a Computer engineer from New Delhi with interest in blogging and gaming. I am an aspiring full-time blogger and the founder of Konnecting U , where i blog about the latest tech updates along with info on gadgets, android and other "how to" topics. If you like the articles on my blog, then do subscribe to its RSS Feed . You can also connect with me on Twitter or Facebook

Leave a Reply

Back to Top