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.
Making this a shortcode would be awesome as well.
[…] This post was mentioned on Twitter by WPSNIPP, WPSNIPP. WPSNIPP said: #wordpress Get Twitter follower count http://bit.ly/aIM8v1 #tips #help #blog […]