Top Ad unit 728 × 90

Display Feedburner Subscriber Count as Text


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.
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: http://www.wpbeginner.com.
Enter the same URL in the Callback URL field as well. After filling the form hit theCreate 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.
01function getTwitterFollowers($screenName 'wpbeginner')
02{
03    // some variables
04    $consumerKey 'YOUR_CONSUMER_KEY';
05    $consumerSecret 'YOUR_CONSUMER_SECRET';
06    $token = get_option('cfTwitterToken');
07  
08    // get follower count from cache
09    $numberOfFollowers = get_transient('cfTwitterFollowers');
10  
11    // cache version does not exist or expired
12    if (false === $numberOfFollowers) {
13        // getting new auth bearer only if we don't have one
14        if(!$token) {
15            // preparing credentials
16            $credentials $consumerKey ':' .$consumerSecret;
17            $toSend base64_encode($credentials);
18  
19            // http post arguments
20            $args array(
21                'method' => 'POST',
22                'httpversion' => '1.1',
23                'blocking' => true,
24                'headers' => array(
25                    'Authorization' => 'Basic ' $toSend,
26                    'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8'
27                ),
28                'body' => array'grant_type' =>'client_credentials' )
29            );
30  
31            add_filter('https_ssl_verify''__return_false');
32            $response = wp_remote_post('https://api.twitter.com/oauth2/token'$args);
33  
34            $keys = json_decode(wp_remote_retrieve_body($response));
35  
36            if($keys) {
37                // saving token to wp_options table
38                update_option('cfTwitterToken'$keys->access_token);
39                $token $keys->access_token;
40            }
41        }
42        // we have bearer token wether we obtained it from API or from options
43        $args array(
44            'httpversion' => '1.1',
45            'blocking' => true,
46            'headers' => array(
47                'Authorization' => "Bearer $token"
48            )
49        );
50  
51        add_filter('https_ssl_verify''__return_false');
52        $api_url ="https://api.twitter.com/1.1/users/show.json?screen_name=$screenName";
53        $response = wp_remote_get($api_url$args);
54  
55        if (!is_wp_error($response)) {
56            $followers = json_decode(wp_remote_retrieve_body($response));
57            $numberOfFollowers $followers->followers_count;
58        else {
59            // get old value and break
60            $numberOfFollowers = get_option('cfNumberOfFollowers');
61            // uncomment below to debug
62            //die($response->get_error_message());
63        }
64  
65        // cache for an hour
66        set_transient('cfTwitterFollowers'$numberOfFollowers, 1*60*60);
67        update_option('cfNumberOfFollowers',$numberOfFollowers);
68    }
69  
70    return $numberOfFollowers;
71}
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.
1<?php
2echo getTwitterFollowers('your_screen_name');
3 ?>
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.
Display Feedburner Subscriber Count as Text Reviewed by Unknown on 9:14 PM Rating: 5

No comments:

All Rights Reserved by Boca Raton Real Estate © 2014 - 2015
Powered By Blogger, Designed by Sweetheme

Contact Form

Name

Email *

Message *

Powered by Blogger.