and like us on FB

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:

Heather Monthie, PhD (@drmonthie) - Thank you! @FlyingMusicians @CountryTechbcs @InfoSecHotSpot @martykhoury @teslaliving Happy to connect!
Posted: July 3, 2017, 5:53 pm

Flight (@flightorg) - The #FlyDubai #Flight981 radar data. https://t.co/PCglvMy9Wn
Posted: March 19, 2016, 4:42 pm

Flight (@flightorg) - Posted a small #FlyDubai #Flight981 gallery to our FB page: https://t.co/MjiriKOL4A - awful images. https://t.co/QXYqfKlqhs
URL: http://www.facebook.com/flightorg
Posted: March 19, 2016, 4:05 pm

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.

user

The twitteruser is the user for which to retrieve likes.

num

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

cache

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.

Considerations

  • 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.

Download

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

Shortt URL for this post: http://shor.tt/123S