Beginner's Guide for WordPress / Start your WordPress Blog in minutes

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

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit – a collection of WordPress related products and resources that every professional should have!

Reader Interactions

27 CommentsLeave a Reply

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

    pls help me out ..

  2. 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. 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. 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

  5. Hi,

    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.


      • 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.


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

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

  7. 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 :|

  8. Hello,

    we get this error

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

    line 17 is this

    $token = get_option(‘cfTwitterToken’);

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

    Many thanks! :3

  10. 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.

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.