Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
WordPress Plugins
View all Guides

How to Display Twitter Followers Count as Text in WordPress

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
How to Display Twitter Followers Count as Text in WordPress

The easiest way to display Twitter followers is by using the official Twitter follow button. But what if you don’t want to slow your site down by loading twitter’s script? Or what if you are making something very custom and need to display twitter follower count as text instead of a button. Well then you will like this tutorial. In this article, we will show you how to display your twitter follower count as text on your WordPress site.

Wondering how we are going to do this? Well, first we will create a Twitter App, so we can properly use the Twitter API v1.1 to pull the followers count. We will cache it to optimize performance, and then we will display it on the site. Ready to get started? Let’s go.

First thing you need to do is to create a Twitter App for the site where you want to display the followers count. Go to Twitter Developers website and sign in with your Twitter account. After signing in create a new application.

Creating a new Twitter app

On the next screen provide a name for your app this could be anything, ideally the title of your website. Provide a description for your app, this could be the same description as your blog or anything you want. In the website field enter the URL of your WordPress site, For example:

Enter the same URL in the Callback URL field as well. After filling the form hit the Create your Twitter application button at the bottom of the page.

This will create a new Twitter app for you to use. On the next page, click on Create my access token button. This will show you a notification that your authorization token has been created.

On your Twitter App’s page, we will only need the Consumer Key and Consumer Secret for the next step.

Copy the following code and paste it in your theme’s functions.php file or a site specific plugin. Replace Consumer Key and Consumer Secret variables with your consumer key and secret.

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('', $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 = "$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
        // cache for an hour
        set_transient('cfTwitterFollowers', $numberOfFollowers, 1*60*60);
        update_option('cfNumberOfFollowers', $numberOfFollowers);
    return $numberOfFollowers;

Now add this line of code in your theme template where you want to display your twitter followers count. This could be in the sidebar.php, header.php, or basically anywhere you like.

echo getTwitterFollowers('your_screen_name');

That’s it. You are done. We hope that this article helped you show Twitter followers as text in WordPress. There are many other things that you can do to integrate twitter with your WordPress site. For example, you can add twitter cards, or display recent tweets in WordPress. To get more such useful tips consider following @wpbeginner on Twitter.

Source: Zvonko Biskup

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »


  1. venky says:

    Hi used the same code in my site..but its not showing the follower counter of the twitter pages

    pls help me out ..

  2. Noraly says:

    update: after having one last look I saw I hadn’t activated the access token. So now it does show up, only way at the bottom of my sidebar. How do I move it up to a more logical place? Preferably within the text widget at the top, so I can include it with all my other social media links. Thank you!

  3. Noraly says:

    Hi all, hope you are still monitoring comments, since it’s an older article. I have copied the code in functions.php, replaced the key and secret (left the ‘ ‘ intact, was I meant to do that?). Then I copied the other bit in sidebar.php. Replaced the yourscreenname with my twittername. This doesn’t make it show up in the sidebar though. Should I do something with a text widget in the sidebar, where I want it to show up? Just putting the last line of code in a sidebarwidget doesn’t seem to be the trick. Would appreciate your help. Thanks!

  4. WPBeginner Staff says:

    Yes with a few changes you can use it with any PHP application.

  5. Jitendra says:

    Can i use it in other PHP application? I mean any other application which is not WP.

  6. arun says:

    It is not working for me.
    I have added that code into sidebar template , then i replaced consumer key and secret key with screen name. Still it is not working
    This is my page url

    • WPBeginner Support says:

      Arun it seems like you resolved the issue, when we visited your webpage it was showing the correct twitter follower count in plain text.

  7. Nic Granleese says:


    Can you tell me if this code works for multiple twitter users.
    I’m trying to make a table with different users on a site with their respective twitter follow count.
    When I tried it seems to display only one twitter user’s count, which I assume is because user one get’s cached, and then the second, third, and n users just display the same result.


    • WPBeginner Support says:

      Yes you got that right.

      • Thomas says:

        I’ve got the same problem.
        When I ask for the follower count of three different accounts and display it on a page, it displays the same number three times. The number it displays is the exact follower count of the first account.
        Do you know how to fix this? :/
        Thanks in advance.


  8. Julian Lara says:

    Its possible to get a comma in the number like 140,029. Because actually show like 140029.

  9. rayuribe says:

    works great!
    It is possible add the result of this script to this other >

    and show the total? (facebook_fans+twitter_fans=total_fans)

    • jahirul says:

      Would you please share the code of follower count of yours? the function and the activation code, please.

  10. Nazar says:

    This doesn’t work for me.

    I’ve replaced $consumerKey and $consumerSecret as well as made the Access level to “Read and write” but nothing is happening :|

  11. irfan says:

    Hey hi,
    Such a great post.
    I have ask a question for you I use twiiter user link ( when i use this link show followers its that possible?
    Any one,
    Thank Advance

  12. Alvin says:


    we get this error

    Fatal error: Call to undefined function get_option() in line 17

    line 17 is this

    $token = get_option(‘cfTwitterToken’);

  13. Matt says:


    Dreamweaver tells me this line is invalid:

    $api_url = “$screenName“;

    So I’ve updated it to:

    $api_url = ‘$screenName’;

    But the twitter count just says 0 (I have over 1,200).

    Any suggestions?

  14. Malcom Miles says:

    Wrapped this tutorial along with the “WordPress Site Specific Plugin” tutorial and worked like a charm.

    Many thanks! :3

  15. Tyler says:

    I’ve tried doing this atleast 10 times and can’t get it to work. Is it up-to-date?

  16. Zulhilmi Zainudin says:

    How about to display Facebook Fans & Google+ Followers in text?

  17. Chandra says:

    Thanks for this code. I used this in my site but after sometime, I tested with an addition of follower but that count is not being updated. It still shows old count. Is something missing ? Thanks.

  18. regnar says:

    A little demo would be great.

    • Editorial Staff says:

      It will show the count as text. For example: if you have 100 followers, then it will output 100. Not sure what you expected in the demo.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.