Display Twitter Followers Count as Text in WordPress
The easiest way to Display Twitter Followers Count as Text in WordPress 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 do it.
Create Twitter App
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: https://pluginthemehub.com
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.
Create Access Token to Display Twitter Followers Count as Text in WordPress
On the next page, click onΒ Create my access tokenΒ button. This will show you a notification that your authorization token has been created.
Consumer Key and Consumer Secret
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 = ‘pluginthemehub’)
{
Β Β Β Β // 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 where you want to display your twitter followers count. This could be in the sidebar.php, header.php, or basically anywhere you like.
<?php
echo getTwitterFollowers(‘your_screen_name’);
Β ?>
Thatβs it. You are done.
We hope that this article (Display Twitter Followers Count as Text in WordPress) 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.Β
NOTE: Twitter has changed it’s name to X. Here, you go the link- Twitter (X)