Home / functions.php / Get Twitter follower count
  • How to Get Twitter follower count

    get-twitter-follower-count

    Adding the first section of code to your functions.php & don’t forget to change $name = ‘problogdesign’; to your twitter user name. Then add the second section of code to your template where you wish to display your twitter follower count. Special thanks to Michael Martin of problogdesign.com.

    ( click code to copy )

    wordpress snippet : PHP

    <>
    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');
    
    ( click code to copy )

    wordpress snippet : PHP

    <>
    <?php
               twitter_count();
    ?>
    

    ( WordPress codex functions, hooks, in this snippet. )

    schedule_event, init, add_action, add_option, get_option, update_option, _n, _e, _ex, wp_next_scheduled, wp_schedule_event, wp,