Sunday, November 29, 2020
Home Blogging How to Display Twitter Followers Count as Text in WordPress

How to Display Twitter Followers Count as Text in WordPress

The best approach to display Twitter followers is through the use of the official Twitter observe button. However what for those who don’t wish to sluggish your website down by loading twitter’s script? Or what if you’re making one thing very customized and have to display twitter follower rely as textual content as a substitute of a button. Nicely then you’ll like this tutorial. On this article, we are going to present you find out how to display your twitter follower rely as textual content in your WordPress website.

Questioning how we’re going to do that? Nicely, first we are going to create a Twitter App, so we are able to correctly use the Twitter API v1.1 to drag the followers rely. We are going to cache it to optimize efficiency, after which we are going to display it on the location. Able to get began? Let’s go.

Very first thing that you must do is to create a Twitter App for the location the place you wish to display the followers rely. Go to Twitter Builders web site and check in together with your Twitter account. After signing in create a brand new utility.

On the subsequent display present a reputation on your app this might be something, ideally the title of your web site. Present an outline on your app, this might be the identical description as your weblog or something you need. Within the web site subject enter the URL of your WordPress website, For instance: https://www.gyanhigyann.com.

Enter the identical URL within the Callback URL subject as properly. After filling the shape hit the Create your Twitter utility button on the backside of the web page.

This can create a brand new Twitter app so that you can use. On the subsequent web page, click on on Create my entry token button. This can present you a notification that your authorization token has been created.

In your Twitter App’s web page, we are going to solely want the Shopper Key and Shopper Secret for the subsequent step.

Copy the next code and paste it in your theme’s features.php file or a website particular plugin. Substitute Shopper Key and Shopper Secret variables together with your client key and secret.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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;
}

Now add this line of code in your theme template the place you wish to display your twitter followers rely. This might be within the sidebar.php, header.php, or mainly anyplace you want.

1
2
3
<?php
echo getTwitterFollowers('your_screen_name');
 ?>

That’s it. You might be performed. We hope that this text helped you present Twitter followers as textual content in WordPress. There are lots of different issues that you are able to do to combine twitter together with your WordPress website. For instance, you’ll be able to add twitter playing cards, or display current tweets in WordPress. To get extra such helpful ideas take into account following @GyannHi on Twitter.

You May Also Like : How to Exclude Pages from WordPress Search Results

If you liked this article, make sure to hit the bell for getting our latest updates and share this article with your friends in every social media platforms.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

How to Start Dropshipping Business in India and Make Money Through it in 2020

If you're planning on beginning a dropshipping business in India, the time is correct now. Ecommerce or shopping for stuff on the Internet is...

An Internship Guide about DRDO

Defense Research Development Organization (DRDO) provides an internship to graduates and submit graduate college students. If you're on the lookout for a profession in DRDO...

How to Become a Pilot: Eligibility, Training, Study & Salary

Every little one dreamt of flying. Some wished to fly as passengers whereas others wished to grow to be pilot, taking plane hovering into...

10 Best Online Money Transfer Apps & e-Wallets in India

There was a time when sending cash from one place to a different concerned standing in lengthy financial institution/publish workplace queues, patiently awaiting your...

Recent Comments