Home / functions.php / Cached twitter follower count with get_transient, wp_remote_get
  • How to Cached twitter follower count with get_transient, wp_remote_get

    cached-twitter-follower-count

    Add the first section of code to the functions.php of your wordpress theme. This code will cache your Twitter follower number for 1 hour and display the count within your template in any location you place get_follower_count().

    ( click code to copy )

    wordpress snippet : PHP

    <>
    function get_follower_Count(){
        $count = get_transient('follower_count');
    if ($count !== false) return $count;
        $count = 0;
        $data = wp_remote_get('http://api.twitter.com/1/users/show.json?screen_name=YOURNAME');
    if (!is_wp_error($data)) {
        $value = json_decode($data['body'],true);
        $count = $value['followers_count'];
    }
    set_transient('follower_count', $count, 60*60); // 1 hour cache
    return $count;
    }
    
    ( click code to copy )

    wordpress snippet : PHP

    <>
    <?
    get_follower_count()
    ?>
    

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

    wp_remote_get, _n, _e, is_wp_error, wp,