At the time of originally writing this article (March, 2016), Twitter had just renamed their Twitter favorites to "likes" (expressed by way of a little heart signal) in a puerile attempt at remaining relevant in a world dominated by Facebook. The change likely comes from the fact that the word 'favorite' isn't generally culturally-compatible because of the implied variations in translation, while a heart signal is a globally accepted symbol that transcends language. The change was just another to the platform in the months preceding to maintain relevance.
Note: While the code provided is functional, we've removed the examples as the website itself no longer requires Twitter support.
While the word "like" generally translates to "enjoy" in common language, it is inconsistent with the picture representation; likes (the heart) and favorites are very different things. I usually favorite tweets as a bookmark - not necessarily because I like them... and 'hearting' a tweet sends a very different message. If you look at my favorites you'll see tweets that I've often saved because I didn't like them. Changing what favorites are will change how they're used, and potentially alters all existing favorites as well.
Sometimes, however, it's nice to keep a list of your
favorites 'likes' on your WordPress website as a resource for others, and they're a convenient way to bookmark tweets for your own reading. The WordPress shortcode on this page will render your favorites in a very basic manner.
For the purpose of this article I'll render just three favorites. To see a more comprehensive list, see
this example page (50 results). To render the three likes below, I've used the shortcode of
[twitterlikes user="MartyKhoury" num="3"]. I could have omitted my username because I've listed it as a default in my code.
To display the likes of another use, substitute your own username for theirs. A maximum of 200 favorites can be returned in the one request. More details below.
Registering a Twitter Application
You must have a Twitter application registered. In doing so you'll create a
$accesstokensecret that are all required in the shortcode function. You should refer to this video on setting up the necessary application on Twitter.
We're using Abraham Williams' TwitterOAuth library to assist with making the request. The library must be included at the top of your custom functions file (you'll need to reference the path to the
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 first function will convert the returned Twitter time into a more sensible format. Adjust as necessary by reference to PHP's date() function.
The second function converts text hashtags, username mentions, and URLs, into clickable links.
The last function takes a URL and snips it into two halves. We do this simply to preserve space. Alter the 35 to a maximum URL length.
The following shortcode attributes are available.
twitteruseris the user for which to retrieve likes.
cacheis the amount of time to cache the data locally before making another request. Twitter rate limits the number of requests to a maximum to 15 per 15 minute window which is more than enough for personal use. Since favorites don't change that often, and the request takes a little bit of time, we suggest caching data for 12 hours. Alter this in the shortcode function (or your post shortcode) if required.
Used outside of WordPress, the following may be used.
Usage is as follows:
A request for favorites is limited to your own account.
- Liking articles, from Twitter .
- More information on the
GET favorites/listendpoint can be found here . There are a number of other attributes that could be applied but we'll reserve that functionality for our naked PHP function. The purpose of the shortcode was to make accessing data easy.