X

How to Get Twitter Follower Count in WordPress

Snippets by IsItWP

Are you looking for a way to get Twitter follower count and display it in WordPress? While there’s probably a plugin for this, we have created a quick code snippet that you can use to get Twitter follower count in WordPress.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

function update_twitter_count() {
 
$name = 'problogdesign';
$url = 'http://api.twitter.com/1/users/show.xml?screen_name='. $name;
 
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
 
$xml = new SimpleXMLElement($data);
$count = $xml->followers_count;
 
$count = (float) $count;
$count = number_format($count);
 
add_option('twitter_followers');
update_option('twitter_followers', $count);
}

function twitter_count() {
	echo get_option('tb_twitter_followers');
}

if (!wp_next_scheduled('your_hourly_hook')) {
	wp_schedule_event(time(), 'hourly', 'your_hourly_hook');
}
add_action('your_hourly_hook', 'update_twitter_count');

Make sure to change the Twitter handle in the above code. Add this code to a template file of your theme where you want to display the Twitter count.

<?php 
           twitter_count(); 
?>

This code snippet is a courtesy of Michael Martin.

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 24 best WordPress education themes and how to secure your WordPress forms with password protection.

Comments   Leave a Reply

  1. Kelvin Alfonso July 27, 2014 at 1:51 am

    Making this a shortcode would be awesome as well.

  2. Tweets that mention Wordpress Get Twitter follower count – wpsnipp.com Wordpress code snippets for your blog -- Topsy.com November 4, 2010 at 7:32 pm

    […] This post was mentioned on Twitter by WPSNIPP, WPSNIPP. WPSNIPP said: #wordpress Get Twitter follower count http://bit.ly/aIM8v1 #tips #help #blog […]

Add a Comment Cancel reply

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!