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  8 Responses to “How to Get Twitter User Data (Part 1)”
  1. 1
  2. 2

    I pay a visit every day some web pages and sites to read articles, except this website
    offers feature based articles.

    my webpage; brave frontier cheat

    • 2.1
      misty says:

      Well personally sounds ridiculous.  Why is some one else’s page any of your business? Sounds like you put your nose in where it does not belong. But ya know something,  I’m positive that you have done it before.  Which is wrong and makes a stalker and the authorities should be contracted.  

  3. 3

    Hi it’s me, I am also visiting this site daily, this website is genuinely fastidious and the visitors are in fact sharing pleasant thoughts.

    Here is my website :: extreme weight loss diets

  4. 4

    Indices as the Dow 100, Nasdaq 100, FTSE 100, CAC 40, Dax.
    First is to know exactly what type of option is being traded.
    A small down tick or up tick in the market could make you lose everything.

  5. 5
    Raquel says:

    It’s genuinely very complicated in this active life to listen news
    on TV, so I oly use web for that purpose, and obtain the most up-to-date news.

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.