How to Display Twitter Followers Count as Text in WordPress

The perfect means to show Twitter fans is through the use of the legitimate Twitter apply button. But what should you don’t need to gradual your website online down through loading twitter’s script? Or what if you’re making one thing very customized and wish to show twitter follower depend as textual content as a substitute of a button. Well then you’ll like this instructional. In this text, we will be able to display you ways to show your twitter follower depend as textual content for your WordPress website online.

Wondering how we’re going to do that? Well, first we will be able to create a Twitter App, so we will be able to correctly use the Twitter API v1.1 to pull the fans depend. We will cache it to optimize efficiency, after which we will be able to show it at the website online. Ready to get began? Let’s cross.

First factor you wish to have to do is to create a Twitter App for the website online the place you need to show the fans depend. Go to Twitter Developers website online and signal in together with your Twitter account. After signing in create a brand new software.

On the following display screen supply a reputation in your app this may well be the rest, preferably the identify of your website online. Provide an outline in your app, this may well be the similar description as your weblog or the rest you need. In the website online box input the URL of your WordPress website online, For instance: https://www.wpbeginner.com.

Enter the similar URL in the Callback URL box as neatly. After filling the shape hit the Create your Twitter software button on the backside of the web page.

This will create a brand new Twitter app for you to use. On the following web page, click on on Create my get entry to token button. This will display you a notification that your authorization token has been created.

On your Twitter App’s web page, we will be able to simplest want the Consumer Key and Consumer Secret for the next move.

Copy the next code and paste it in your theme’s purposes.php record or a website online explicit plugin. Replace Consumer Key and Consumer Secret variables together with your shopper key and secret.


serve as getTwitterFollowers($screenName="wpbeginner")
{
    // some variables
    $shopperKey = 'YOUR_CONSUMER_KEY';
    $shopperSecret="YOUR_CONSUMER_SECRET";
    $token = get_option('cfTwitterToken');
 
    // get follower depend from cache
    $numberOfFollowers = get_transient('cfTwitterFollowers');
 
    // cache model does no longer exist or expired
    if (false === $numberOfFollowers) {
        // getting new auth bearer provided that we do not have one
        if(!$token) {
            // making ready credentials
            $credentials = $shopperKey . ':' . $shopperSecret;
            $toSend = base64_encode($credentials);
 
            // http submit arguments
            $args = array(
                'manner' => 'POST',
                'httpversion' => '1.1',
                'blocking off' => true,
                'headers' => array(
                    'Authorization' => 'Basic ' . $toSend,
                    'Content-Type' => 'software/x-www-form-urlencoded;charset=UTF-8'
                ),
                'frame' => array( 'grant_type' => 'client_credentials' )
            );
 
            add_filter('https_ssl_verify', '__return_false');
            $reaction = wp_remote_post('https://api.twitter.com/oauth2/token', $args);
 
            $keys = json_decode(wp_remote_retrieve_body($reaction));
 
            if($keys) {
                // saving token to wp_options desk
                update_option('cfTwitterToken', $keys->access_token);
                $token = $keys->access_token;
            }
        }
        // we have now bearer token wether we acquired it from API or from choices
        $args = array(
            'httpversion' => '1.1',
            'blocking off' => true,
            'headers' => array(
                'Authorization' => "Bearer $token"
            )
        );
 
        add_filter('https_ssl_verify', '__return_false');
        $api_url = "https://api.twitter.com/1.1/customers/display.json?screen_name=$screenName";
        $reaction = wp_remote_get($api_url, $args);
 
        if (!is_wp_error($reaction)) {
            $fans = json_decode(wp_remote_retrieve_body($reaction));
            $numberOfFollowers = $followers->followers_count;
        } else {
            // get previous price and wreck
            $numberOfFollowers = get_option('cfNumberOfFollowers');
            // uncomment underneath to debug
            //die($response->get_error_message());
        }
 
        // cache for an hour
        set_transient('cfTwitterFollowers', $numberOfFollowers, 1*60*60);
        update_option('cfNumberOfFollowers', $numberOfFollowers);
    }
 
    go back $numberOfFollowers;
}

Now upload this line of code in your theme template the place you need to show your twitter fans depend. This may well be in the sidebar.php, header.php, or mainly anyplace you prefer.


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

That’s it. You are achieved. We hope that this text helped you display Twitter fans as textual content in WordPress. There are many different issues that you’ll be able to do to combine twitter together with your WordPress website online. For instance, you’ll be able to upload twitter playing cards, or show contemporary tweets in WordPress. To get extra such helpful pointers imagine following @wpbeginner on Twitter.

Source: Zvonko Biskup

Recent Articles

The Best Pinterest Alternatives to Use if You are Bored of Pinterest

Pinterest is a loved social media network among the lover of aesthetics, mostly females tbh. Now, however more and more people are...

Ways you can add links to your Instagram post in 2021

Instagram has many cool features which need no introduction. However, there are a number of features that...

Finance App You should Have on Your iPhone and Android

We live in the age of digitization. When everything is digital, it is no surprise that money has joined the club too....

5 Video Editing Softwares to Try Your Hand at in 2021

Video is the king when it comes to social media. With reels dominating Instagram and Facebook and...

Charge Your Laptop Without a Charger in 5 Ways

Forgetting your laptop charger is a pretty common thing. We all have been there and all of...

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here