Displaying Twitter Status In My Blog
by ZetaGecko | 7 Comments | Atom/RSS, Blogging, CaRP, Social Media
Twitter has a widget that you can use to display your latest update in your blog sidebar or elsewhere on your website. But it's...well, not perfect. I found that it was bigger than necessary for the text it contained, and the empty space didn't exactly enhance the look of my blog.
So I wrote my own code to display my Twitter updates. You'll see my two latest updates over in the sidebar. Each begins with a link to the update page (not terribly useful, since you can read the whole update right there anyway, but who knows), followed by the date and time it was posted, and the update text itself.
If there's a link in the text, it is automatically converted into a live link.
Both links open new browser windows.
So, how do I do it? I use CaRP Evolution and this code:
<ul><?php function LinkMyURL($ii,$fn,$iind,$in,$va,$rv) { return preg_replace('#(http://[-\\w./]{1,22})([-\\w./]*)#', '<a target="_blank" href="\\1\\2">\\1</a>', str_replace('AntoneRoundy: ','',$va['TITLE'])); } function DoMyConfig() { CarpConf('iorder','url,date,mytitle'); CarpRegisterCallback('','LinkMyURL','handlefield','mytitle'); CarpConf('cborder',''); CarpConf('idateformat','j-M@g:ia'); CarpConf('aidate','</i> - '); CarpConf('bi','<li>'); CarpConf('ai','</li>'); CarpConf('maxitems',2); CarpConf('encodingout','UTF-8'); CarpConf('biurl','<a target="_blank" href="'); CarpConf('aiurl','">Go »</a> '); } require_once '/my/path/to/carp/carp.php'; CarpRegisterCallback('','DoMyConfig','cachemiss',''); CarpCacheShow('http://twitter.com/statuses/user_timeline/5934972.rss', 'blog_twitter'); ?></ul>
Here's what all that means (just in case you're interested):
I've created two PHP functions. The first, "LinkMyURL", removes "AntoneRoundy: " from the update text, and uses a little regular expression voodoo to convert URLs to hyperlinks (thus the name "LinkMyURL"). The link text may be truncated to prevent really long URLs from breaking the formatting (just in case Twitter doesn't convert to a tinyurl link sometime -- I don't think they ALWAYS do that).
The second, "DoMyConfig", is used to configure CaRP whenever it needs to refresh its cache. I tell CaRP to do that using the call to CarpRegisterCallback where I register it as a callback for the "cachemiss" phase of processing.
The last line, "CarpCacheShow", tells CaRP to download my Twitter status RSS feed (if a fresh copy isn't already in its cache) and display it, storing the formatted output in a file named "blog_twitter".
The contents of the DoMyConfig function are as follows:
The "iorder" setting tells CaRP to display the RSS feed item's link ("url"), date (from pubDate or dc:date), and a custom field I created named "mytitle" (which gets its data from the RSS "title" field -- that's "$va['TITLE']" in LinkMyURL). The call to CarpRegisterCallback on the next line registers my LinkMyURL function as a callback for processing my "mytitle" field.
The empty "cborder" setting tells CaRP not to display any RSS channel data.
The "idateformat" setting says to display the day and month, "@", the hour and minute, and "am" or "pm". "aidate" tells CaRP what HTML code to display after the date (CaRP starts the date with an "<i>" tag by default, do I didn't need to specify that, but I wanted to override the default "</i><br />" setting for what to display after it).
The "bi" and "ai" settings tell CaRP put put each RSS feed item in a "list item" tag (I do that because of how my blog template displays sidebar data).
"maxitems" specifies that two updates should be displayed.
Setting "encodingout" to "UTF-8" ensures that the character encoding matches my blog (otherwise, some characters could get displayed incorrectly).
Finally, the "biurl" and "aiurl" settings tell CaRP what to display before and after the URL. I use these to turn the URL into a hyperlink with "Go »" as the link text.
Twitter is cool, but I like this a lot better than their widget.
October 7th, 2008 at 8:19 pm
Keep in mind that if you copy the code above, and paste it into your source code to go through it and convert all the single and double quotes.
October 7th, 2008 at 8:47 pm
Thanks for pointing that out! I forgot that WordPress automatically converts everything to "smart quotes". I may have to hack my WordPress to prevent that.
January 8th, 2009 at 7:46 pm
I've discovered that surrounding the code with a "pre" tag prevents WordPress from messing with the quote marks.
January 9th, 2009 at 7:02 pm
Interesting observation:
As I read this case study, I noticed the frequency that this page updates.
I know it is a blog and wondered if each update is also a ping.
I am pretty sure it is not, because that would be lots of pinging and I believe that is frowned upon.
However out of curiosity I Googled:
Displaying Twitter Status In My Blog,
Twitter Status, Twitter Status Blog, and Displaying Twitter Status. Just to see how the spiders treat this frequent updating.
For each term there are a little over 1 million search results. For each of those terms this article appears in the top 10 SERP. For 3 of the 4 it was in the top 3 SERP.
I guess there is something to this frequent updating.
How do you do it?
April 2nd, 2009 at 4:05 pm
If you grabbed the code before 4/2/2009, you'll want to add hyphens in two places on the third line -- was [\\w./] -- changed to [-\\w./]
June 10th, 2009 at 10:18 pm
I've just created a free WordPress widget (CaRP Evolution required) based on the code above that makes it really easy to integrate into your blog sidebar. It requires a widget-aware WordPress theme.
http://www.geckotribe.com/rss/carp/Twidget-WP/
July 15th, 2010 at 11:25 pm
Thanks Antone,
This is heaps better than the Twitter Gadget.