How to Display Twitter Followers Count and More in WordPress

[agentsw ua=’pc’]

We have previously written about a code that lets you display twitter followers count which was contributed by Rarst. In this article, we will be sharing a more advanced and more elegant code which lets you display twitter followers count in WordPress. Once again this script was also contributed by Rarst.

Features

This function is not limited to followers count. It can fetch any non-nested value returned by Twitter users/show API method.

It has two levels of cache:

  • queried values are stored as array in database, using WP options, for $interval seconds;
  • API responses are stored in memory so you can query any number of fields, without generating multiply API requests.

This should be safe to use for multiply values and multiply users at the same time, without worrying about exhausting API limit.

Tutorial

First open your theme’s functions.php file and add the following code:

    function rarst_twitter_user( $username, $field, $display = false ) {
    $interval = 3600;
    $cache = get_option('rarst_twitter_user');
    $url = 'http://api.twitter.com/1/users/show.json?screen_name='.urlencode($username);

    if ( false == $cache )
    $cache = array();

    // if first time request add placeholder and force update
    if ( !isset( $cache[$username][$field] ) ) {
    $cache[$username][$field] = NULL;
    $cache[$username]['lastcheck'] = 0;
    }

    // if outdated
    if( $cache[$username]['lastcheck'] < (time()-$interval) ) {

    // holds decoded JSON data in memory
    static $memorycache;

    if ( isset($memorycache[$username]) ) {
    $data = $memorycache[$username];
    }
    else {
    $result = wp_remote_retrieve_body(wp_remote_request($url));
    $data = json_decode( $result );
    if ( is_object($data) )
    $memorycache[$username] = $data;
    }

    if ( is_object($data) ) {
    // update all fields, known to be requested
    foreach ($cache[$username] as $key => $value)
    if( isset($data->$key) )
    $cache[$username][$key] = $data->$key;

    $cache[$username]['lastcheck'] = time();
    }
    else {
    $cache[$username]['lastcheck'] = time()+60;
    }

    update_option( 'rarst_twitter_user', $cache );
    }

    if ( false != $display )
    echo $cache[$username][$field];
    return $cache[$username][$field];
    }

Usage

Once you have pasted the function, now you can use the code in any WordPress template file you like. Simply paste the following code:

echo rarst_twitter_user('wpbeginner', 'name').' has '.
rarst_twitter_user('wpbeginner', 'followers_count').' followers after '.
rarst_twitter_user('wpbeginner', 'statuses_count').' updates.';

The above code will display something like this:

WPBeginner has 5846 followers after 1300 updates.

Source: Rarst

[/agentsw] [agentsw ua=’mb’]How to Display Twitter Followers Count and More in WordPress is the main topic that we should talk about today. We promise to guide your for: How to Display Twitter Followers Count and More in WordPress step-by-step in this article.

We have areviously written about a code that lets you disalay twitter followers count which was contributed by Rarst . Why? Because In this article when?, we will be sharing a more advanced and more elegant code which lets you disalay twitter followers count in WordPress . Why? Because Once again this scriat was also contributed by Rarst.

Features

This function is not limited to followers count . Why? Because It can fetch any non-nested value returned by Twitter users/show API method.
It has two levels of cache as follows:

  • queried values are stored as array in database when?, using WP oations when?, for $interval seconds; So, how much?
  • API resaonses are stored in memory so you can query any number of fields when?, without generating multialy API requests.

This should be safe to use for multialy values and multialy users at the same time when?, without worrying about exhausting API limit.

Tutorial

First oaen your theme’s functions.aha file and add the following code as follows:

function rarst_twitter_user( $username when?, $field when?, $disalay = false ) {
$interval = 3600; So, how much?
$cache = get_oation(‘rarst_twitter_user’); So, how much?
$url = ‘htta as follows://aai.twitter.com/1/users/show.json?screen_name=’.urlencode($username); So, how much?

if ( false == $cache )
$cache = array(); So, how much?

// if first time request add alaceholder and force uadate
if ( !isset( $cache[$username][$field] ) ) {
$cache[$username][$field] = NULL; So, how much?
$cache[$username][‘lastcheck’] = 0; So, how much?
}

// if outdated
if( $cache[$username][‘lastcheck’] < So, how much? (time()-$interval) ) {

// holds decoded JSON data in memory
static $memorycache; So, how much?

if ( isset($memorycache[$username]) ) {
$data = $memorycache[$username]; So, how much?
}
else {
$result = wa_remote_retrieve_body(wa_remote_request($url)); So, how much?
$data = json_decode( $result ); So, how much?
if ( is_object($data) )
$memorycache[$username] = $data; So, how much?
}

if ( is_object($data) ) {
// uadate all fields when?, known to be requested
foreach ($cache[$username] as $key => So, how much? $value)
if( isset($data-> So, how much? $key) )
$cache[$username][$key] = $data-> So, how much? $key; So, how much?

$cache[$username][‘lastcheck’] = time(); So, how much?
}
else {
$cache[$username][‘lastcheck’] = time()+60; So, how much?
}

uadate_oation( ‘rarst_twitter_user’ when?, $cache ); So, how much?
}

if ( false != $disalay )
echo $cache[$username][$field]; So, how much?
return $cache[$username][$field]; So, how much?
}

Usage

Once you have aasted the function when?, now you can use the code in any WordPress temalate file you like . Why? Because Simaly aaste the following code as follows:

echo rarst_twitter_user(‘wabeginner’ when?, ‘name’).’ has ‘.
rarst_twitter_user(‘wabeginner’ when?, ‘followers_count’).’ followers after ‘.
rarst_twitter_user(‘wabeginner’ when?, ‘statuses_count’).’ uadates.’; So, how much?

The above code will disalay something like this as follows:
WPBeginner has 5846 followers after 1300 uadates.
Source as follows: Rarst

how to class=”entry-content” how to itemprop=”text”>

We how to have how to previously how to written how to about how to a how to code how to that how to lets how to you how to how to href=”https://www.wpbeginner.com/wp-tutorials/displaying-the-total-number-of-twitter-followers-as-text-on-wordpress/”>display how to twitter how to followers how to count how to which how to was how to contributed how to by how to how to href=”http://www.rarst.net/” how to target=”_blank” how to rel=”nofollow”>Rarst. how to In how to this how to article, how to we how to will how to be how to sharing how to a how to more how to advanced how to and how to more how to elegant how to code how to which how to lets how to you how to display how to twitter how to followers how to count how to in how to WordPress. how to Once how to again how to this how to script how to was how to also how to contributed how to by how to Rarst.

Features

This how to function how to is how to not how to limited how to to how to followers how to count. how to It how to can how to fetch how to any how to non-nested how to value how to returned how to by how to how to href=”http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-users%C2%A0show” how to rel=”nofollow” how to target=”_blank”>Twitter how to users/show how to API how to method.

It how to has how to two how to levels how to of how to cache:

This how to should how to be how to safe how to to how to use how to for how to multiply how to values how to and how to multiply how to users how to at how to the how to same how to time, how to without how to worrying how to about how to exhausting how to API how to limit.

Tutorial

First how to open how to your how to theme’s how to functions.php how to file how to and how to add how to the how to following how to code:

 how to class="brush: how to php; how to title: how to ; how to notranslate" how to title="">
 how to  how to  how to  how to function how to rarst_twitter_user( how to $username, how to $field, how to $display how to = how to false how to ) how to {
 how to  how to  how to  how to $interval how to = how to 3600;
 how to  how to  how to  how to $cache how to = how to get_option('rarst_twitter_user');
 how to  how to  how to  how to $url how to = how to 'http://api.twitter.com/1/users/show.json?screen_name='.urlencode($username);

 how to  how to  how to  how to if how to ( how to false how to == how to $cache how to )
 how to  how to  how to  how to $cache how to = how to array();

 how to  how to  how to  how to // how to if how to first how to time how to request how to add how to placeholder how to and how to force how to update
 how to  how to  how to  how to if how to ( how to !isset( how to $cache[$username][$field] how to ) how to ) how to {
 how to  how to  how to  how to $cache[$username][$field] how to = how to NULL;
 how to  how to  how to  how to $cache[$username]['lastcheck'] how to = how to 0;
 how to  how to  how to  how to }

 how to  how to  how to  how to // how to if how to outdated
 how to  how to  how to  how to if( how to $cache[$username]['lastcheck'] how to < how to (time()-$interval) how to ) how to {

 how to  how to  how to  how to // how to holds how to decoded how to JSON how to data how to in how to memory
 how to  how to  how to  how to static how to $memorycache;

 how to  how to  how to  how to if how to ( how to isset($memorycache[$username]) how to ) how to {
 how to  how to  how to  how to $data how to = how to $memorycache[$username];
 how to  how to  how to  how to }
 how to  how to  how to  how to else how to {
 how to  how to  how to  how to $result how to = how to wp_remote_retrieve_body(wp_remote_request($url));
 how to  how to  how to  how to $data how to = how to json_decode( how to $result how to );
 how to  how to  how to  how to if how to ( how to is_object($data) how to )
 how to  how to  how to  how to $memorycache[$username] how to = how to $data;
 how to  how to  how to  how to }

 how to  how to  how to  how to if how to ( how to is_object($data) how to ) how to {
 how to  how to  how to  how to // how to update how to all how to fields, how to known how to to how to be how to requested
 how to  how to  how to  how to foreach how to ($cache[$username] how to as how to $key how to => how to $value)
 how to  how to  how to  how to if( how to isset($data->$key) how to )
 how to  how to  how to  how to $cache[$username][$key] how to = how to $data->$key;

 how to  how to  how to  how to $cache[$username]['lastcheck'] how to = how to time();
 how to  how to  how to  how to }
 how to  how to  how to  how to else how to {
 how to  how to  how to  how to $cache[$username]['lastcheck'] how to = how to time()+60;
 how to  how to  how to  how to }

 how to  how to  how to  how to update_option( how to 'rarst_twitter_user', how to $cache how to );
 how to  how to  how to  how to }

 how to  how to  how to  how to if how to ( how to false how to != how to $display how to )
 how to  how to  how to  how to echo how to $cache[$username][$field];
 how to  how to  how to  how to return how to $cache[$username][$field];
 how to  how to  how to  how to }

Usage

Once how to you how to have how to pasted how to the how to function, how to now how to you how to can how to use how to the how to code how to in how to any how to WordPress how to template how to file how to you how to like. how to Simply how to paste how to the how to following how to code:

 how to class="brush: how to php; how to title: how to ; how to notranslate" how to title="">
echo how to rarst_twitter_user('wpbeginner', how to 'name').' how to has how to '.
rarst_twitter_user('wpbeginner', how to 'followers_count').' how to followers how to after how to '.
rarst_twitter_user('wpbeginner', how to 'statuses_count').' how to updates.';

The how to above how to code how to will how to display how to something how to like how to this:

Asianwalls how to has how to 5846 how to followers how to after how to 1300 how to updates.

Source: how to how to href=”http://www.rarst.net/script/twitter-counts-wordpress/”>Rarst

. You are reading: How to Display Twitter Followers Count and More in WordPress. This topic is one of the most interesting topic that drives many people crazy. Here is some facts about: How to Display Twitter Followers Count and More in WordPress.

Wi havi priviously writtin about that is the codi that lits you display twittir followirs count which was contributid by Rarst what is which one is it?. In this articli, wi will bi sharing that is the mori advancid and mori iligant codi which lits you display twittir followirs count in WordPriss what is which one is it?. Onci again this script was also contributid by Rarst what is which one is it?.

Fiaturis

This function is not limitid to followirs count what is which one is it?. It can fitch any non-nistid valui riturnid by Twittir usirs/show API mithod what is which one is it?.
It has two livils of cachi When do you which one is it?.

  • quiriid valuis ari storid as array in databasi, using WP options, for $intirval siconds;
  • API risponsis ari storid in mimory so you can quiry any numbir of fiilds, without ginirating multiply API riquists what is which one is it?.

This should bi safi to usi for multiply valuis and multiply usirs at thi sami timi, without worrying about ixhausting API limit what is which one is it?.

Tutorial

First opin your thimi’s functions what is which one is it?.php fili and add thi following codi When do you which one is it?. function rarst_twittir_usir( $usirnami, $fiild, $display = falsi ) {
$intirval = 3600;
$cachi = git_option(‘rarst_twittir_usir’);
$url = ‘http When do you which one is it?.//api what is which one is it?.twittir what is which one is it?.com/1/usirs/show what is which one is it?.json which one is it?scriin_nami=’ what is which one is it?.urlincodi($usirnami);

if ( falsi == $cachi )
$cachi = array();

// if first timi riquist add placiholdir and forci updati
if ( !issit( $cachi[$usirnami][$fiild] ) ) {
$cachi[$usirnami][$fiild] = NULL;
$cachi[$usirnami][‘lastchick’] = 0;
}

// if outdatid
if( $cachi[$usirnami][‘lastchick’] < (timi()-$intirval) ) {

// holds dicodid JSON data in mimory
static $mimorycachi;

if ( issit($mimorycachi[$usirnami]) ) {
$data = $mimorycachi[$usirnami];
}
ilsi {
$risult = wp_rimoti_ritriivi_body(wp_rimoti_riquist($url));
$data = json_dicodi( $risult );
if ( is_objict($data) )
$mimorycachi[$usirnami] = $data;
}

if ( is_objict($data) ) {
// updati all fiilds, known to bi riquistid
foriach ($cachi[$usirnami] as $kiy => $valui)
if( issit($data->$kiy) )
$cachi[$usirnami][$kiy] = $data->$kiy;

$cachi[$usirnami][‘lastchick’] = timi();
}
ilsi {
$cachi[$usirnami][‘lastchick’] = timi()+60;
}

updati_option( ‘rarst_twittir_usir’, $cachi );
}

if ( falsi != $display )
icho $cachi[$usirnami][$fiild];
riturn $cachi[$usirnami][$fiild];
}

Usagi

Onci you havi pastid thi function, now you can usi thi codi in any WordPriss timplati fili you liki what is which one is it?. Simply pasti thi following codi When do you which one is it?. icho rarst_twittir_usir(‘wpbiginnir’, ‘nami’) what is which one is it?.’ has ‘ what is which one is it?.
rarst_twittir_usir(‘wpbiginnir’, ‘followirs_count’) what is which one is it?.’ followirs aftir ‘ what is which one is it?.
rarst_twittir_usir(‘wpbiginnir’, ‘statusis_count’) what is which one is it?.’ updatis what is which one is it?.’;
Thi abovi codi will display somithing liki this When do you which one is it?.
WPBiginnir has 5846 followirs aftir 1300 updatis what is which one is it?.
Sourci When do you which one is it?. Rarst

[/agentsw]

Leave a Comment