Posted: May 12, 2015

This small snippet will grab your twitter follower count and display it on your site as html text. As this outputs is plain old html you will be able to add whatever classes you need to it and make it pretty with css.

Install

Simply open your functions.php file for you active theme and copy and paste the following code. Save the file and make sure it uploads back to your server. You will also have to create a twitter app which can be done at the twitter developer site.

The Code


function getTwitterFollowers($screenName = 'screenName')
{
    // some variables
    $consumerKey = '';
    $consumerSecret = '';
    $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;
}

Required Changes

There are a few needed changes for the above code to function properly.

  1. You need to change the screenName variable to your twitter screen name.
  2. You need to get your twitter consumer key and consumer secret code and populate the consumerKey and consumerSecret variables.

Displaying The Count Number

To display the twitter follower count simply add the code below into the template file you wish for it to display in e.g. sidebar.php or header.php.


echo getTwitterFollowers('screenName');

Just change the screenName to your twitter screen name and your done.

  • Was this Helpful ?
  • Yes   No

Do You Like What You See?