Displaying Twitter Followers Count in Plain Text

Over the last couple of days, I was keep thinking of adding metro style social box on the sidebar of my site and show the counters like Facebook Like count, Twitter followers count stuff like that. Now, I came across bunch of “plugin”s and big tutorials available out there to get my job done. But I decided to write a simple function to retrieve the Followers or Like counts manually than following a complex methods available out there.

After playing around with my thoughts for couple of minutes, this is the code that came up with. Amazingly it worked without any issue.

function ib_twitter_followers_count() {
   $url = 'https://twitter.com/YOUR_ID';
   $content = wp_remote_retrieve_body(wp_remote_get($url));
      if( is_wp_error($content)) { 
         return 0; 
      } else {
         preg_match('%followers_count&quot;:\K\d %', $content, $matches);
   echo $matches[0]; } 

Simply copy the code from above and paste it on your theme’s functions.php file. Now, in order to display the count number in plain text, simply change YOUR_ID from the second line and update the page. Now, you can call this function anywhere on your template files as:

<?php ib_twitter_followers_count(); ?>

Or you can use the following method:

if(function_exists('ib_twitter_follower_count')) {
   ib_twitter_follower_count(); }

I would highly recommend you to use the second method.

Code Explanations

The simple methodology behind this function was to retrieve the body content of an https request and then search for s very specific pattern of string and grab the content right after the possible match from the content.

At the very beginning of the code, I declared $url variable and set the value (public URL of your twitter account). Then I used two WordPress function to retrieve data through an https request. I used wp_remote_get function to make an https request through passing the value from the $url variable. I retrieve the body content of this https request using wp_remote_retrieve_body function.

Now, before retrieving the data (followers count) from the https response, I used is_wp_error functions with IF…ELSE statement to process possible errors. In case if there is any error it would return nothing else it would go for executing preg_match function and finally retrieve the content right after the possible string pattern match.

This is a very simple yet an efficient method of retrieving your twitter followers count in plain text. I hope you found this post to be useful for you. Stay tuned for more.

Note: This method is a very simple one and may not work if Twitter decides to change their profile page’s codes upside down. However, you can always get smart with your codes if thing doesn’t work. Let me know if it works for you as well or not from the comment section.

Today In History



Leave a Reply

Note: Convet HTML, PHP, JavaScripts from Postable, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.