Home Blog Retrieve Twitter Favorites (Likes) and Display in WordPress with Shortcode

Retrieve Twitter Favorites (Likes) and Display in WordPress with Shortcode

Twitter Likes

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.

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.

The Result

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. The result:

DeveloperSteve (@DeveloperSteve) - @davidfreeman_au @martykhoury Singapore too 😁
Posted: May 26, 2018, 5:22 am

David Freeman (@davidfreeman_au) - @martykhoury Hey @martykhoury , that is the plan! We will see how we go with this one in Melb and then… https://t.co/Jtz9sw8YNv
URL: https://twitter.c...000245601781465090
Posted: May 26, 2018, 5:21 am

Will Horton (@winglets747) - Nice new website and booking engine from @JINAIR_LJ. A few touches to be more customer-centric: replace "PKG" and "… https://t.co/TSafiy5n8d
URL: https://twitter.c...979162020724465664
Posted: March 29, 2018, 1:03 am

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 $consumerkey, $consumersecret, $accesstoken, and $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 autoload.php file).

The Code

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.

Shortcode Attributes

The following shortcode attributes are available.


The twitteruser is the user for which to retrieve likes.


Specifies the number of records to retrieve. Must be less than or equal to 200. Specify the default in your shortcode function.


The cache is 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.

PHP Function

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/list endpoint 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.


Twitteroauth can be downloaded from Github , or via the source menu on the library website .

Title: Display Twitter Favorites in WordPress or with PHP
Description: Retrieve Twitter Favorites (Likes) and Display in WordPress with Shortcode, or with PHP.
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/19oH


Please enter your comment!
Please enter your name here