How to Get Twitter User Data (Part 1)

by on June 11, 2011

Twitter is THE microblogging social network. And, we're fortunate to have a vast array of twitter-related plugins for WordPress. But, I haven't found one yet that just grabs the user data for any account and makes it available as an easy shortcode implementation for a blog.

That's exactly what we'll develop today. Plus, we'll offer several cool functions that you can utilize in plugins and other WordPress functions. We'll instantly grab more than two dozen stats for any user, cache them temporarily in the database, and then make them available as shortcode which can be presented in any post, page, or text widget. These include: followers count, following count, tweet count, and the latest status message. Moreover, our collection of functions that support this shortcode will format numbers and correctly create links in the status message. Further, any of it can be presented in your own arrangement preference.

You can skip to Part 2 here.

And, you can also grab the entire collection of functions here.

Let's dive in…

An Overview of our Twitter Shortcode

Twitter provides a very clean and up-to-date XML file for every user. We'll use the WordPress Twitter account for our example.

Click the following link to view their latest XML file. You may have to “view the source” to see it.

You can replace “wordpress” at the end of the URL with any valid user's screen name to view that data as well. As you look at the file, you'll note all sorts of information: user id, name, screen name, location, and so on.

What we do is extract the content of that file (for any user), parse it into a PHP array, format the data, and store it temporarily in the WP database. By caching the data for a little while, we avoid being banned as an apparent denial-of-service attack … in the event we have lots of views of our data presentation in a short period of time. It's also faster to read from the database on your own site than to read the content from a file on another server.

Here's an quick usage example (live). The really cool aspect is that you can arrange any piece of data any way you want and apply your own style preferences.


(posted from )

Using Our Shortcode

Our shortcode name is “wcs_twitter_user_data”. But, that's a bit long, so we also enabled an abbreviated version “wcs_tud”.

Here are the (optional) attributes and their default values:

user: 'wordpress'
data: 'status' (that's the latest tweet)
time_format: 'F j, Y (g:i a)' (PHP time/date format)
conv_lang_name: 'true' (convert 'en' to 'English', for example)
cache_hours: '1'
force_update: 'false' ('true' bypasses the cache — ONLY for testing)

The time_format attribute sets the display format for the two timestamp_formatted data values. You can review these standard PHP format characters here.

Here's an example usage to get the latest WordPress tweet:

[wcs_tud user=wordpress data=status]

And, this one gets their tweet count:

[wcs_tud user=wordpress data=tweets]

Here's the actual HTML used on this page (above) to display the large usage example:

Code: HTML

<div style="width:500px; margin-left:25px; padding:8px; background:#FFFFE0; border:1px solid #A0A0A0;">
<table cellpadding="0" cellspacing="0" style="border-bottom:1px solid #A0A0A0; width:100%;"><tr>
    <td valign="middle" width="64">[wcs_tud user=WordPress data=image]</td>
    <td valign="top" align="left"><span style="font-size:20px;">[wcs_tud user=WordPress data=link]</span><br/>[wcs_tud user=WordPress data=website_link]</td>
Following: [wcs_tud user=WordPress data=following]
Followers: [wcs_tud user=WordPress data=followers]
Tweets: [wcs_tud user=WordPress data=tweets]
[wcs_tud user=WordPress data=status]
<br/><span style="font-size:12px;">(posted [wcs_tud user=wordpress data=status_timestamp_ago] from [wcs_tud user=wordpress data=status_source])</span>

And, here are the various values that you can use with the data attribute. The status values (latest tweet info) will be empty if tweet_privacy  is 'true'. As a matter of fact, all of the true/false values are lower-case strings — not boolean values.


  status (latest tweet)
  status_id (latest tweet id #)
  status_timestamp (raw timestamp)
  status_timestamp_formatted (formatted timestamp)
  status_timestamp_ago (time difference “… ago”)
  status_source (for example: Twitter, TweetDeck, or CoTweet)

  id (user id #)
  name (display name)
  screen_name (up to 15 characters: lowercase, numbers, and/or underscore)
  link (HTML link code to their Twitter page)
  website_link (HTML link code to their website)
  image (HTML img display tag)

  time_zone (example: 'Mountain Time (US & Canada)')
  geo_enabled ('true' or 'false')
  tweet_media ('true' or 'false')
  tweet_privacy ('true' or 'false')
  language_iso_code (examples: 'en', 'es', and 'fre')
  language (ISO code conversion, examples: 'English', 'Spanish', and 'French')

You can get the source for this shortcode in the next segment of this article:
How to Get Twitter User Data (Part 2)

Share This Article: “How to Get Twitter User Data (Part 1)”

(Also Available: Press CTRL+D to Bookmark this Page)


Share Your Thoughts  4 Responses to “How to Get Twitter User Data (Part 1)”
  1. 1
  2. 2
    hd porno says:

    What’s up mates, its enormous article about cultureand completely explained, keep it up all the


Check out what others are saying about this post...
  1. [...] can also grab the entire collection of functions here.You can view the first article in this series here.  Print PDF Share This Article: “How to Get Twitter User Data (Part 2)”(Also [...]

  2. [...] has written a fantastic post about getting Twitter data to work well with WordPress, including a A Shortcode To Get Twitter User Data. Today, it released a two-part series called How to Get Twitter User Data I and How to Get [...]

Share Your Thoughts

(Some editor features are restricted unless you're logged in.)

(When replying to a specific comment, your browser may require Shift+Enter instead of just Enter.)

(get a gravatar)

Notify me of followup comments via e-mail. You can also subscribe without commenting.