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.

http://twitter.com/users/show.xml?screen_name=wordpress

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.



Following:
Followers:
Tweets:


(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>
</tr></table>
 
<br/>
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>
</div>

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.

COUNTS:
  followers
  following
  tweets
  listed
  favorites

LATEST TWEET:
  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)
  status_favorited
  status_retweets

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

OTHER INFO:
  created_timestamp
  created_timestamp_formatted
  created_timestamp_ago
  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)

Comments

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

    Inc Magazine praises the leadership of CEO Eddie Sparks of Hermosa Beach Marketing
    by adding the company to the Inc 5000 list of 2014.

    Inc Magazine named CEO Eddie Sparks one of the up
    and coming most successful leaders of 2014 for online marketing in the US.
    Inc later issued a statement that they may remove Sparks from
    the February 2014 issue altogether due to the current
    status of the company is ‘sole propreitership’ making it impossible to have a CEO.

    The argument and controversy thickens as Eddie Sparks is the CEO of 2 other companies making
    Sparks a CEO regardless of Hermosa Beach Marketing’s success.
    ” I refuse to incorporate the business in its current state. California Incorporation causes undue tax hardships and responsibilities that the company has no need for currently. If you are going to judge my leadership abilities, I have brought company assets over 5 million, paid off all investment monies, and am expanding to include the general public as a form of income. We have no debt whatsoever which is unheard of in the Los Angeles area. The business is extremely tight knit and the staff works together quite well. ” says Sparks.
    PR Director Yasmine Do of Inc. states, “Sparks was nominated via the success of the business as a whole. He truly is a talented individual but he will not be appearing in our February issue”.
    No other comments followed.

Trackbacks

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.