Home Blog Retrieve Your Twitter Follow Count (and Other Data) in Plain Text With...

Retrieve Your Twitter Follow Count (and Other Data) in Plain Text With WordPress Shortcode or PHP


This article provides the necessary code to query Twitter for profile data relevant to your (and other) accounts. Once we retrieve the data from Twitter we cache the resulting array with Simple Cache to avoid repeated requests to the API. While we cache the most relevant user data, it’s the follower count that we’ve found users are most interested in using. That said, the function is handy for retrieving your latest tweet (another post is forthcoming that shows you how to build a sidebar widget).

The function requires you install Abraham’s twitteroauth . We have an old video here that’ll show you how to set up your Twitter application .

The Result

The function works by finding and replacing relevant keywords in your text with the appropriate response from our cached Twitter array. For this reason we’re required to use opening and closing shortcode tags. The most common response required is the follower count, and this may be returned with [twitterdata]%%followers%%[/twitterdata]. The result:

Error. Try again soon.


If you were to include your last tweet, you might use something like the following (we’ve wrapped it in a textbox).

+ Follow BeliefMedia on Twitter

Error. Try again soon.

Shortcode used was as follows: [twitterdata]%%lasttweet%%.<br>&bull; Join %%followers%% others and follow us on <a href="http://www.twitter.com/BeliefMedia" target="_blank">Twitter</a>.[/twitterdata]. We then wrapped our shortcode in a textbox that is available here.

Since we’re retrieving the Twitter array from a cached source, we don’t make another request to the API despite returning a different result.

Common Functions

The common functions are required for use with both the PHP application and WordPress shortcode. Its purpose is to query Twitter and cache the resulting array of profile information. If you’re using the function from within WordPress, you won’t need to include Simple Cache; instead, install the plugin.

If you wanted to test the function and view the resulting array, use the following:

Ensure you add the pre HTML tags (removed for formatting).

We use a function to linkify links, usernames, and hashtags. The function is required and available here.

WordPress Shortcode

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin.

If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.

PHP Function

The PHP function is intended to be used outside of WordPress. It includes fewer options than the shortcode; the assumption is that you’ll curate the result to your liking.

Data returned via the PHP function is cached via Simple Cache.


  • An example response of the $data array that we cache is as follows:
  • When registering your Twitter application, be sure to select Read/Write as the application privilege. You will be required to have a verified phone number before this option becomes available.


Title: Retrieve Twitter Data With WordPress Shortcode or PHP
Description: Retrieve Your Twitter Follow Count (and Other Data) in Plain Text With WordPress Shortcode or PHP. Data is cached.
Download: Shortcode (V0.2) | PHP Code (V0.2) | Plugin Page

■ ■ ■

Interested in WordPress and Web Design? Subscribe to our list and we'll send you subscriber-only tools and plugins

Short URL for this post: http://shor.tt/Jo8


Please enter your comment!
Please enter your name here