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 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
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.
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.
Copy and paste the WordPress function into your theme's
functions.php file or, if you sensibly have one installed, your custom functions plugin.
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
$dataarray that we cache is as follows:
- When registering your Twitter application, be sure to select
Read/Writeas the application privilege. You will be required to have a verified phone number before this option becomes available.