RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.38% (6.14%*) • Home Loan Fixed: 5.44% (6.26%*) • Fixed: 5.44% (6.26%*) • Variable: 5.38% (6.14%*) • Investment IO: 5.69% (6.52%*) • Investment PI: 5.49% (5.98%*)

Alternative Ways of Displaying Tweets

Alternative Ways of Displaying Tweets

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.

Note: Examples have been removed from this page temporarily. Code remains functional.

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:

Example No Longer Available

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.

1
<?php 
2
/*
3
 Alternative Ways of Displaying Tweets
4
 http://www.beliefmedia.com/displaying-tweets
5
*/
6
 
7
function beliefmedia_oembed_tweet($atts) {
8
 
9
  $atts = shortcode_atts(array(
10
    'url' => ''
11
  ), $atts);
12
 
13
 $transient = 'beliefmedia_otweet_' . md5(serialize($atts));
14
 $cached =  beliefmedia_get_transient_data($transient);
15
 
16
 if ($cached !== false ) {
17
  return $cached;
18
 
19
  } else {
20
 
21
    $url = 'https://publish.twitter.com/oembed?url=' . urlencode($atts['url']);
22
    $data = @file_get_contents($url);
23
    if ($data !== false) $data = json_decode($data, true);
24
      else return false;
25
 
26
    $html = $data['html'];
27
 
28
    /* Extract language, text dir, and tweet */
29
    preg_match('#\<blockquote\sclass\="twitter\-tweet"\>\<p\slang\="(en|ar|bn|cs|da|de|el|es|fa|fi|fil|fr|he|hi|hu|id|it|ja|ko|msa|nl|no|pl|pt|ro|ru|sv|th|tr|uk|ur|vi|zh-cn|zh-tw)"\sdir\="(ltr|rtl)"\>(.*)\</blockquote\>#', $html, $results);
30
 
31
    $tweet_language = $results['1'];
32
    $tweet_direction = $results['2'];
33
    $tweet_text = $results['3'];
34
    $status_url = $data['url'];
35
    $author_name = $data['author_name'];
36
    $author_url = $data['author_url'];
37
 
38
    /* Get username */
39
    $user_url_array = explode('/', $author_url);
40
    $username = end($user_url_array);
41
 
42
    /* Bulid a textbox */
43
    $return = '<div style="width: 90%; margin: auto; border: #000000 1px solid">
44
<div style="background-color: #000000; padding: 2px 30px 2px 30px ; font-weight: bold; color: #ffffff;">&plus;  ' . $author_name . ', (@' . $username . ')</div>
45
<div style="padding: 8px 30px 10px 30px; color: #000000; padding: 15px 30px 18px 65px; background: #16B9ED url(http://www.beliefmedia.com/wp-images/site/textbox/twitter-40.png) no-repeat 10px 50%; font-size: 0.9em;">' . $tweet_text . '</div>
46
</div>';
47
 
48
    /* Cache result */
49
    beliefmedia_set_transient($transient, $return);
50
 
51
    /* Return result */
52
    return $return;
53
 }
54
}
55
add_shortcode('bmtweet', 'beliefmedia_oembed_tweet');

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.

1
<?php 
2
/*
3
 Alternative Ways of Displaying Tweets
4
 http://www.beliefmedia.com/displaying-tweets
5
*/
6
 
7
function beliefmedia_oembed_tweet($url) {
8
 
9
 $transient = 'beliefmedia_otweet_' . md5($url);
10
 $cached =  beliefmedia_get_transient_data($transient);
11
 
12
 if ($cached !== false ) {
13
  return $cached;
14
 
15
  } else {
16
 
17
    $url = 'https://publish.twitter.com/oembed?url=' . urlencode($url);
18
    $data = @file_get_contents($url);
19
    if ($data !== false) $data = json_decode($data, true);
20
      else return false;
21
 
22
    $html = $data['html'];
23
 
24
    /* Extract language, text dir, and tweet */
25
    preg_match('#\<blockquote\sclass\="twitter\-tweet"\>\<p\slang\="(en|ar|bn|cs|da|de|el|es|fa|fi|fil|fr|he|hi|hu|id|it|ja|ko|msa|nl|no|pl|pt|ro|ru|sv|th|tr|uk|ur|vi|zh-cn|zh-tw)"\sdir\="(ltr|rtl)"\>(.*)\</blockquote\>#', $html, $results);
26
 
27
    $tweet_language = $results['1'];
28
    $tweet_direction = $results['2'];
29
    $tweet_text = $results['3'];
30
    $status_url = $data['url'];
31
    $author_name = $data['author_name'];
32
    $author_url = $data['author_url'];
33
 
34
    /* Get username */
35
    $user_url_array = explode('/', $author_url);
36
    $username = end($user_url_array);
37
 
38
    /* Bulid a textbox */
39
    $return = '<div style="width: 90%; margin: auto; border: #000000 1px solid">
40
<div style="background-color: #000000; padding: 2px 30px 2px 30px ; font-weight: bold; color: #ffffff;">&plus;  ' . $author_name . ', (@' . $username . ')</div>
41
<div style="padding: 8px 30px 10px 30px; color: #000000; padding: 15px 30px 18px 65px; background: #16B9ED url(http://www.beliefmedia.com/wp-images/site/textbox/twitter-40.png) no-repeat 10px 50%; font-size: 0.9em;">' . $tweet_text . '</div>
42
</div>';
43
 
44
    /* Cache result */
45
    beliefmedia_set_transient($transient, $return);
46
 
47
    /* Return result */
48
    return $return;
49
 }
50
}
51
 
52
/* Usage */
53
$url = 'https://twitter.com/martykhoury/status/586630176898854913';
54

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 (WP Shortcode)
Description: Embed formatted tweets with WordPress shortcode or PHP. Example code only.
  Download • Version 0.2, 1.1K, zip, Category: WordPress Shortcodes
PHP Code & Snippets, (1.1K)    

■ ■ ■

 
Download our complimentary 650-page guide on marketing for mortgage brokers. We'll show you exactly how we generate billions in volume for our clients.
Finance Guide, Cropped Top and Bottom
  Timezone: 1 · [ CHANGE ]

RELATED READING

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment