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]
.
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.
Considerations
- An example response of the
$data
array that we cache is as follows:1Array2(3[userid] => 652494464[followers] => 81175[following] => 69506[created] => Thu Aug 13 02:43:01 +0000 20097[name] => Belief Media8[screenname] => BeliefMedia9[location] => Australia.10[description] => BeliefMedia is Sydney's fastest growing media & marketing company. Simply put, we make businesses grow... bigly 🙂11[count] => 53812[profileurl] => http://pbs.twimg.com/profile_images/753827278014984192/5BS5q-Ga_normal.jpg13[lasttweet] => Simple Responsive Pricing Tables With #WordPress Shortcode https://t.co/RgW0O2wX7u - @BeliefMedia14) - 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.
Download
Title: Retrieve Twitter Data With WordPress Shortcode or PHP (Shortcode)
Description: Retrieve Your Twitter Follow Count (and Other Data) in Plain Text With WordPress Shortcode or PHP. Data is cached.
Download • Version 0.2, 1.7K, zip, Category: WordPress Shortcodes
PHP Code & Snippets, (1.6K)