{"id":1398,"date":"2010-05-07T23:21:52","date_gmt":"2010-05-07T21:21:52","guid":{"rendered":"http:\/\/planetozh.com\/blog\/?p=1398"},"modified":"2010-05-08T01:06:57","modified_gmt":"2010-05-07T23:06:57","slug":"how-to-embed-a-tweet-in-wordpress-a-complete-oembed-tutorial","status":"publish","type":"post","link":"https:\/\/planetozh.com\/blog\/2010\/05\/how-to-embed-a-tweet-in-wordpress-a-complete-oembed-tutorial\/","title":{"rendered":"How to Embed a Tweet in WordPress: a Complete oEmbed Tutorial"},"content":{"rendered":"<p>A few days ago, Twitter published a <a href=\"http:\/\/media.twitter.com\/blackbird-pie\/\">new tool<\/a> that lets you embed a tweet on your site, simplifying the old school way: take a screenshot, crop the picture, upload it, embed it.<\/p>\n<p>The problem is: this Twitter tool is way too lame. Basically you need to cut and paste a lengthy code snippet full of HTML and CSS. How ugly. Compared to the way you can embed a Youtube video in WordPress (just pasting a video URL, on its own line) this is seriously cumbersome.<\/p>\n<p>Hey, wouldn&#39;t it be cool if you can embed a tweet just as easily? You would just paste its URL and let WordPress transforms it into a nicely presented tweet with links and everything? OK, let&#39;s do a plugin for this. And guess what, it&#39;s going to be fairly easy, using WordPress&#39; oEmbed API implementation.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/planetozh.com\/blog\/wp-content\/uploads\/2010\/05\/embed-tweet.jpg\" alt=\"\" title=\"embed-tweet\" width=\"528\" height=\"257\" class=\"aligncenter size-full wp-image-1400\" srcset=\"https:\/\/planetozh.com\/blog\/wp-content\/uploads\/2010\/05\/embed-tweet.jpg 528w, https:\/\/planetozh.com\/blog\/wp-content\/uploads\/2010\/05\/embed-tweet-300x146.jpg 300w\" sizes=\"auto, (max-width: 528px) 100vw, 528px\" \/><br \/>\n<!--more--><\/p>\n<h2>What is oEmbed<\/h2>\n<p>When you paste a Youtube video URL, raw (not hyperlinked) and on its own line, WordPress transforms it into an embedded video. Why? Basically, there&#39;s something in WordPress that says &quot;every time you see a URL like <tt>http:\/\/www.youtube.com\/watch?v=*<\/tt>, ask <tt>http:\/\/www.youtube.com\/oembed<\/tt> what to do with it&quot;.<\/p>\n<p>In short, <a href=\"http:\/\/www.oembed.com\/\">oEmbed<\/a> is a simple API that lets you define a URL pattern (used by a <em>consumer<\/em>) and the URL of a script (the <em>provider<\/em>) that will generate a clean HTLM out of it.<\/p>\n<p>Twitter does not support (yet) oEmbed, so we will have to handle this on our own: transform a WordPress blog into a oEmbed provider.<\/p>\n<h2>Register your blog as an oEmbed provider<\/h2>\n<p>First, let&#39;s register our blog as an oEmbed provider, to replace all URLs like <tt>http:\/\/twitter.com\/ozh\/status\/13156561<\/tt> into a cool piece of HTML. If you can breathe and walk at the same time, you&#39;re qualified to do this: all we need is a single function call. The function to be used here is <tt>wp_oembed_add_provider( $pattern, $url_of_service)<\/tt>.<\/p>\n<div id=\"ig-sh-1\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #666666;font-style: italic\">\/\/ Return the internal URL that will handle the oEmbed requests (ie yourblog.com\/blog\/?ozh_tweet=1)<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">function<\/span> ozh_tweetembed_internal_oembed_url<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #b1b100\">return<\/span> trailingslashit<span style=\"color: #009900\">&#040;<\/span> home_url<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #339933\">.<\/span> <span style=\"color: #0000ff\">'?ozh_tweet=1'<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #666666;font-style: italic\">\/\/ Register this internal URL as an oEmbed provider for twitter URLs<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">function<\/span> ozh_tweetembed_add_oembed_provider<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #666666;font-style: italic\">\/\/ http:\/\/twitter.com\/ozh\/statuses\/1234567<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; wp_oembed_add_provider<span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #0000ff\">'http:\/\/twitter.com\/*\/status\/*'<\/span><span style=\"color: #339933\">,<\/span> ozh_tweetembed_internal_oembed_url<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">add_action<span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #0000ff\">'init'<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">'ozh_tweetembed_add_oembed_provider'<\/span> <span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>OK, so far, we&#39;re telling WP that everytime something looks like <tt>http:\/\/twitter.com\/*\/status\/*<\/tt>, it will have to ask <tt>yourblog.com\/blog\/?ozh_tweet=1<\/tt> what to do with it.<\/p>\n<h2>Catch URLs that will contain ?ozh_tweet=1<\/h2>\n<p>Since we&#39;re now expecting WP to do something particular when called with <tt>?ozh_tweet=1<\/tt>, we have to hijack this URL:<\/p>\n<div id=\"ig-sh-2\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #666666;font-style: italic\">\/\/ Register 'ozh_tweet' as a query var so we can check if ?ozh_tweet=1<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">function<\/span> ozh_tweetembed_add_tweet_query_var<span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #000088\">$query_vars<\/span> <span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #000088\">$query_vars<\/span><span style=\"color: #009900\">&#091;<\/span><span style=\"color: #009900\">&#093;<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #0000ff\">'ozh_tweet'<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #b1b100\">return<\/span> <span style=\"color: #000088\">$query_vars<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">add_filter<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'query_vars'<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">'ozh_tweetembed_add_tweet_query_var'<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #666666;font-style: italic\">\/\/ As early as possible, check for query var 'ozh_tweet' and maybe shortcut everything<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">function<\/span> ozh_tweetembed_get_tweet_query_var<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #000088\">$tweet<\/span> <span style=\"color: #339933\">=<\/span> get_query_var<span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #0000ff\">'ozh_tweet'<\/span> <span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #b1b100\">if<\/span><span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #000088\">$tweet<\/span> <span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #666666;font-style: italic\">\/\/ Make something with the oEmbed request<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; ozh_tweetembed_handle_oembed_request<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #666666;font-style: italic\">\/\/ Now die, we don't want a blog page<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #990000\">die<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">add_action<span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #0000ff\">'template_redirect'<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">'ozh_tweetembed_get_tweet_query_var'<\/span> <span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>So, at this point, every time WP sees a line matching <tt>http:\/\/twitter.com\/*\/status\/*<\/tt>, it will ask <tt>yourblog.com\/blog\/?ozh_tweet=1&url=the_tweet_url&some_other_var=...<\/tt> what to do with it. So, what to do with it?<\/p>\n<h2>Handling the oEmbed request itself<\/h2>\n<p>Now for the fun part: the function that handles the oEmbed request and return the expected formatted output.<\/p>\n<p>Basically, we&#39;re going to:<\/p>\n<ol>\n<li>Guess the tweet id<\/li>\n<li>Poll the Twitter API for information about this tweet, get a JSON response<\/li>\n<li>Parse the JSON response, make something pretty with this it<\/li>\n<li>Return that pretty thing into a JSON response<\/li>\n<\/ol>\n<p>Vamos, muchachos:<\/p>\n<div id=\"ig-sh-3\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #666666;font-style: italic\">\/\/ Handle the oEmbed requests<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #666666;font-style: italic\">\/\/ The oEmbed request is something like:<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #666666;font-style: italic\">\/\/ yourblog.com\/blog\/?ozh_tweet=1&amp;url=http:\/\/twitter.com\/ozh\/statuses\/123456&amp;...<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #666666;font-style: italic\">\/\/<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">function<\/span> ozh_tweetembed_handle_oembed_request<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #000088\">$url<\/span> <span style=\"color: #339933\">=<\/span> esc_url<span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #000088\">$_GET<\/span><span style=\"color: #009900\">&#091;<\/span><span style=\"color: #0000ff\">'url'<\/span><span style=\"color: #009900\">&#093;<\/span> <span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #b1b100\">if<\/span><span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #339933\">!<\/span><span style=\"color: #000088\">$url<\/span> <span style=\"color: #009900\">&#041;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #990000\">die<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'URL missing'<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #990000\">preg_match<\/span><span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #0000ff\">'!http:\/\/twitter.com\/([^\/]+)\/status\/(\\d+)!'<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #000088\">$url<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #000088\">$matches<\/span> <span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #000088\">$author<\/span> &nbsp;<span style=\"color: #339933\">=<\/span> <span style=\"color: #000088\">$matches<\/span><span style=\"color: #009900\">&#091;<\/span><span style=\"color: #cc66cc\">1<\/span><span style=\"color: #009900\">&#093;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #000088\">$tweetid<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #000088\">$matches<\/span><span style=\"color: #009900\">&#091;<\/span><span style=\"color: #cc66cc\">2<\/span><span style=\"color: #009900\">&#093;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #990000\">unset<\/span><span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #000088\">$matches<\/span> <span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #666666;font-style: italic\">\/\/ From this point, we fetch content from Twitter and print a JSON string.<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #666666;font-style: italic\">\/\/ If something goes wrong (like, Twitter unreachable) then we'll simply print anything but JSON<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #666666;font-style: italic\">\/\/ and let WP handle the response.<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #666666;font-style: italic\">\/\/ fetch http:\/\/api.twitter.com\/1\/statuses\/show\/$tweet.json<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #000088\">$apiurl<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #0000ff\">'http:\/\/api.twitter.com\/1\/statuses\/show\/'<\/span><span style=\"color: #339933\">.<\/span><span style=\"color: #000088\">$tweetid<\/span><span style=\"color: #339933\">.<\/span><span style=\"color: #0000ff\">'.json'<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #b1b100\">if<\/span> <span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #339933\">!<\/span><span style=\"color: #000088\">$result<\/span> <span style=\"color: #339933\">=<\/span> wp_remote_retrieve_body<span style=\"color: #009900\">&#040;<\/span> wp_remote_get<span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #000088\">$apiurl<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #990000\">array<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'timeout'<\/span> <span style=\"color: #339933\">=&gt;<\/span> <span style=\"color: #cc66cc\">15<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#041;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #990000\">die<\/span><span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #0000ff\">&quot;could not fetch <span style=\"color: #006699;font-weight: bold\">$apiurl<\/span>&quot;<\/span> <span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #666666;font-style: italic\">\/\/ Check that JSON is well formed<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #000088\">$result<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #990000\">trim<\/span><span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #000088\">$result<\/span> <span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #b1b100\">if<\/span> <span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #339933\">!<\/span><span style=\"color: #000088\">$data<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #990000\">json_decode<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$result<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#041;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #990000\">die<\/span><span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #0000ff\">&quot;Data was not JSON&quot;<\/span> <span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #666666;font-style: italic\">\/\/ Now extract a few variables from the $data object<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #000088\">$text<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #000088\">$data<\/span><span style=\"color: #339933\">-&gt;<\/span><span style=\"color: #004000\">text<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #000088\">$created_at<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #990000\">date<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'d M Y g:i a'<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #990000\">strtotime<\/span><span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #000088\">$data<\/span><span style=\"color: #339933\">-&gt;<\/span><span style=\"color: #004000\">created_at<\/span> <span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #b1b100\">if<\/span><span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #339933\">!<\/span><span style=\"color: #990000\">empty<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$data<\/span><span style=\"color: #339933\">-&gt;<\/span><span style=\"color: #004000\">source<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#041;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #000088\">$source<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #0000ff\">'via '<\/span><span style=\"color: #339933\">.<\/span><span style=\"color: #000088\">$data<\/span><span style=\"color: #339933\">-&gt;<\/span><span style=\"color: #004000\">source<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #b1b100\">if<\/span><span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #339933\">!<\/span><span style=\"color: #990000\">empty<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$data<\/span><span style=\"color: #339933\">-&gt;<\/span><span style=\"color: #004000\">in_reply_to_screen_name<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #000088\">$in_reply_to_screen_name<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #000088\">$data<\/span><span style=\"color: #339933\">-&gt;<\/span><span style=\"color: #004000\">in_reply_to_screen_name<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #000088\">$in_reply_to_status_id<\/span> &nbsp; <span style=\"color: #339933\">=<\/span> <span style=\"color: #000088\">$data<\/span><span style=\"color: #339933\">-&gt;<\/span><span style=\"color: #004000\">in_reply_to_status_id<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #000088\">$inreply<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #0000ff\">&quot;in reply to &lt;a href='http:\/\/twitter.com\/<span style=\"color: #006699;font-weight: bold\">$in_reply_to_screen_name<\/span>\/status\/<span style=\"color: #006699;font-weight: bold\">$in_reply_to_status_id<\/span>'&gt;@<span style=\"color: #006699;font-weight: bold\">$in_reply_to_screen_name<\/span>&lt;\/a&gt;&quot;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #000088\">$name<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #000088\">$data<\/span><span style=\"color: #339933\">-&gt;<\/span><span style=\"color: #004000\">user<\/span><span style=\"color: #339933\">-&gt;<\/span><span style=\"color: #004000\">name<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #000088\">$screen_name<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #000088\">$data<\/span><span style=\"color: #339933\">-&gt;<\/span><span style=\"color: #004000\">user<\/span><span style=\"color: #339933\">-&gt;<\/span><span style=\"color: #004000\">screen_name<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #000088\">$profile_image<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #000088\">$data<\/span><span style=\"color: #339933\">-&gt;<\/span><span style=\"color: #004000\">user<\/span><span style=\"color: #339933\">-&gt;<\/span><span style=\"color: #004000\">profile_image_url<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #666666;font-style: italic\">\/\/ Make up some HTML with the tweet info<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #000088\">$html<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #0000cc;font-style: italic\">&lt;&lt;&lt;HTML<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000cc;font-style: italic\">&lt;div class=&quot;tweet&quot;&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000cc;font-style: italic\">&nbsp; &nbsp; &lt;div class=&quot;text&quot;&gt;$text&lt;\/div&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000cc;font-style: italic\">&nbsp; &nbsp; &lt;div class=&quot;meta&quot;&gt;$created_at $source $inreply&lt;\/div&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000cc;font-style: italic\">&nbsp; &nbsp; &lt;hr\/&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000cc;font-style: italic\">&nbsp; &nbsp; &lt;div class=&quot;pic&quot;&gt;&lt;a href=&quot;http:\/\/twitter.com\/$screen_name&quot;&gt;&lt;img src=&quot;$profile_image&quot;&gt;$name&lt;\/a&gt;&lt;\/div&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000cc;font-style: italic\">&lt;\/div&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000cc;font-style: italic\">HTML<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #666666;font-style: italic\">\/\/ Put everything into a regular array<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #000088\">$json<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #990000\">array<\/span><span style=\"color: #009900\">&#040;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #0000ff\">'version'<\/span> <span style=\"color: #339933\">=&gt;<\/span> <span style=\"color: #0000ff\">'1.0'<\/span><span style=\"color: #339933\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #0000ff\">'type'<\/span> &nbsp; &nbsp;<span style=\"color: #339933\">=&gt;<\/span> <span style=\"color: #0000ff\">'rich'<\/span><span style=\"color: #339933\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #0000ff\">'width'<\/span> &nbsp; <span style=\"color: #339933\">=&gt;<\/span> <span style=\"color: #cc66cc\">1337<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #666666;font-style: italic\">\/\/ this parameter is mandatory according to the oEmbed specs, but we're not using it<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #0000ff\">'height'<\/span> &nbsp;<span style=\"color: #339933\">=&gt;<\/span> <span style=\"color: #cc66cc\">1337<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #666666;font-style: italic\">\/\/ same.<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #0000ff\">'html'<\/span> &nbsp; &nbsp;<span style=\"color: #339933\">=&gt;<\/span> <span style=\"color: #990000\">trim<\/span><span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #000088\">$html<\/span> <span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #666666;font-style: italic\">\/\/ Now output a JSON response &nbsp; <\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #990000\">header<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'Cache-Control: no-cache, must-revalidate'<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #990000\">header<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'Expires: Mon, 26 Jul 1997 05:00:00 GMT'<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #990000\">header<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'Content-type: application\/json'<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #990000\">header<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'Content-Disposition: attachment; filename=&quot;oembed.json&quot;'<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #b1b100\">echo<\/span> <span style=\"color: #990000\">json_encode<\/span><span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #000088\">$json<\/span> <span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>And&#8230; That&#39;s it. Now just paste a tweet URL and let WordPress do the job. For your convenience, get the whole plugin we&#39;ve just dissected in a single file<\/p>\n<div class=\"download\">\nDownload the plugin <a href=\"https:\/\/planetozh.com\/blog\/wp-content\/uploads\/2010\/05\/ozh-oembed-tweet.php_.txt\">ozh-oembed-tweet.php.txt<\/a><br \/>\n(save as .php then put in your plugins folder)\n<\/div>\n<h2>Further improvements and remarks<\/h2>\n<p>This simple &quot;proof of concept&quot; plugin returns very basic HTML, you&#39;d need to make it prettier (use the tweet background, CSS style it). Just modify the &quot;$html =&quot; starting at line 44 or, even cooler, add an interface to the plugin where you can define a template for the tweet output. If you want some inspiration, I do something like this in my plugin <a href=\"https:\/\/planetozh.com\/blog\/my-projects\/wordpress-plugin-better-feed-rss\/\">Better Feed<\/a> where the user can customize feed item footer with a simple templating system.<\/p>\n<p>When WordPress makes an oEmbed request, it does it once and stores its result in the postmeta table. Twitter has that feature we all know and don&#39;t care about any longer: unreliability. Notice the timeout parameter I&#39;ve added to the remote request (line 21), this is really needed. Still, if you blog cannot fetch the tweet from Twitter&#39;s API, it will store a dummy postmeta entry (<tt>{{unknown}}<\/tt>) and won&#39;t replace the tweet URL. A proper plugin should test for the postmeta value and re-poll the Twitter API if applicable.<\/p>\n<p>Twitter does not support oEmbed, but another site does it for them: <a href=\"http:\/\/oohembed.com\/\">Ooh Embed<\/a>. So, technically, a plugin to embed tweets using the oEmbed API would require just three lines, register oohembed.com as a producer. But that wouldn&#39;t be fun :) (and I don&#39;t know how reliable is oohembed.com)<\/p>\n<p>Technically again, you could use more efficiently function <tt>wp_embed_register_handler()<\/tt> to deal with the oEmbed request without having to sending an extra HTTP request to your own site. But that would probably make a confusing example of the general mechanism of the API, with a consumer sending a request to a producer.<\/p>\n<p>Any other comment or idea of something cool to do with Twitter and oEmbed? Use the comment form <span title=\"low pitched voice\">belllowwww<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A few days ago, Twitter published a new tool that lets you embed a tweet on your site, simplifying the old school way: take a screenshot, crop the picture, upload it, embed it. The problem is: this Twitter tool is way too lame. Basically you need to cut and paste a lengthy code snippet full of HTML and CSS. How\u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[367,371,85,292,245],"class_list":["post-1398","post","type-post","status-publish","format-standard","hentry","category-published","tag-how-to","tag-oembed","tag-plugins","tag-twitter","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/1398","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/comments?post=1398"}],"version-history":[{"count":0,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/1398\/revisions"}],"wp:attachment":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/media?parent=1398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/categories?post=1398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/tags?post=1398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}