How to Display Twitter Followers Count and More in WordPress

Interactive Online > Blog  > CMS  > WordPress  > How to Display Twitter Followers Count and More in WordPress

How to Display Twitter Followers Count and More in WordPress

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.

How to Display Twitter Followers Count and More in WordPress is a post from: WPBeginner which is not allowed to be copied on other sites.

Related posts:

  1. Displaying the Total Number of Twitter Followers as Text on WordPress
  2. Display Feedburner Subscriber Count as Text in WordPress
  3. How to Display Author’s Twitter and Facebook on the Profile Page


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

How to Display Twitter Followers Count and More in WordPress is a post from: WPBeginner which is not allowed to be copied on other sites.

Related posts:

  1. Displaying the Total Number of Twitter Followers as Text on WordPress
  2. Display Feedburner Subscriber Count as Text in WordPress
  3. How to Display Author’s Twitter and Facebook on the Profile Page

(read full article on source site)
Free Website for Your Dental Practice
See why our dental practice websites exceed the features & value of your typcial dentist website.
  • No cookie-cutter websites
  • No duplicate content
  • Custom built with a unique look and feel
  • Unique content written by professional writers
  • Set yourself apart from other dentists
  • The key to your online success!
* we never share your e-mail with third parties.