and like us on FB

Since version 2.9 of WordPress, the platform has supported a technology known as oEmbed that permits the inclusion of rich media in content via a single URL. From WordPress.org, they describe the feature-rich functionality of oEmbed by saying that it is “… a protocol for site A (such as your blog) to ask site B (such as YouTube) for the HTML needed to embed content (such as a video) from site B.” It’s a means of rendering content I’m not been particularly fond of because there’s no easy way of controlling how the data is rendered or cached.

Since WordPress version 3.4 , WordPress has supported Twitter embeds into the core of the platform. What this means is that each twitter link on its own line will render as an embedded tweet.

The simplicity of oEmbed, and features such as the follow and retweet button, means that it’s an ideal solution for the majority of people. For others, however, they’d rather not load the extra 120kb of JavaScript for each occurrence of a Tweet, or they just want to maintain control over message formatting. This article seeks to present an alternative shortcode that makes use of the oEmbed response to render a simple textbox tweet. In reality, our shortcode is kind of useless because it doesn’t do anything that can’t be done without a simple copy-and-paste. The code provides an example of how oEmbed might be used as an alternative to the expected usage. All of the articles we publish on our blog are intended to simply provide our clients with ideas… and this post is no different.

The Result

The shortcode of [bmtweet url="https://twitter.com/martykhoury/status/586630176898854913"] returns the following:

+ Marty Khoury, (@martykhoury)
Coder’s wife asks him to pick up a loaf of bread and, if they have eggs, get a dozen. Programmer comes home with a dozen loaves of bread.

— Marty Khoury (@martykhoury) April 10, 2015

The textbox is one that isn’t entirely unlike those on this page, although it’s just an example and can be formatted to your liking.

WordPress Shortcode

Again, the code is just an example. We’ve used Simple Cache to cache results (may be downloaded as a plugin).

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

Used outside of WordPress the following (almost identical) function may be used. Again, Simple Cache is required.

Considerations

  • Making a request to the API yields far more information and permits an almost identical style to that of the Twitter oEmbed response. This article is forthcoming.

Download

Title: Alternative Ways of Displaying Tweets
Description: Embed formatted tweets with WordPress shortcode or PHP. Example code only.
Download: Shortcode (V0.2) | PHP Code (V0.2) | Plugin Page

Shortt URL for this post: http://shor.tt/2iGT