Integrating Twitter

Even though we just used Twitter as an example of how to integrate any XML feed, let's look at how you could integrate Twitter specifically. Twitter is currently the poster child for open web service APIs. The Twitter API is well documented and easy to use. In addition, it has tons of features. All of this makes integrating your Twitter activities with your WordPress installation a breeze. As such, there are several things you can do with Twitter integration. For example, you can show your latest tweets in a sidebar widget; this is an example of a simple social media badge. You could archive each tweet as its own WordPress post, or get fancier and have daily or weekly archives. You could grab specific tweets and use them in your header to create a dynamic first impression. Or finally, you could reverse the integration and automatically tweet every time you publish a new blog post.

The Twitter Tools plugin (http: //wordpress.org/extend/plugins/twitter-tools/) by Alex King does almost all of this. And it is all contained in one plugin with a simple control panel so you can pick and choose how you want to use it.

For example, showing your latest tweets in the simple social networking sidebar widget is straightforward functionality with this plugin. First, install the plugin on your WordPress site and activate it. Using the new Twitter Tools dashboard, configure your Twitter username and password. Next, save your settings and jump over to the Widgets control panel. You will notice a new widget for Twitter Tools. This widget, by default, will show your last three tweets. Drag this widget to the appropriate sidebar for your theme to enable it. The benefit of this method is that it is trivially simple to implement; turn it on and it's there. The downside is the transitive nature of this type of integration. You may generate some interest in people to follow you on Twitter, but it does not provide lasting content for your personal site.

Twitter Tools can also convert each individual tweet into a post. Depending on how you use Twitter, this can be a nice way to back up, or archive your tweets on your own site. For example, if you use Twitter to create notes to yourself, having WordPress convert these to posts will give you the simplicity of Twitter, but then include the power of the structured content in WordPress.

An alternative approach would be to publish your tweets as asides intermingled around your regular blog posts. Think of asides in the same context as comments you make in the course of conversation that aren't related to the main topic at hand. In this case, because they are originating from Twitter, they would be single-line posts, which make them ideal for asides. Simply take the tweets from Twitter and create new posts in a special category. Your theme would have to handle this special logic to create the right "asides" feel: shown in a sidebar, or highlighted, to differentiate them from the main narrative of your blog posts. Once you have the tweets making new posts, this could be as simple as using the Sandbox theme discussed in Chapter 8 and some creative CSS:

category-twitter h2, .category-twitter .entry-date,

.category-twitter .entry-meta { display:none;

.category-twitter .entry-content p {

background: #22739E url('images/twitter.png') 5px 5px no-repeat; color: #fff;

padding: 2px 5px 2px 25px;

If you're publishing each tweet to its own individual post, Twitter Tools also has the capability to create daily or weekly digests. This is particularly interesting when you use Twitter to capture moments of your day through tweets. The hook with Twitter is how easy it is to use, and yet, because of the size restriction you are forced to keep your posts short — hence microblogging. In this case, you are telling the daily story of your life in a series of small takes. Allowing WordPress to import them creates another form of the same narrative. It is no different from writing an entire post entry and publishing it, except this way is self-assembled from the microblogging format used by Twitter.

One more feature of Twitter Tools is to reverse the integration; that is, tweet when you publish a new post. This feature has to be explicitly enabled in the Twitter Tools dashboard. Once turned on, Twitter Tools will automatically tweet on your behalf that a new post has been published and will include a link to your article. For some people this is a nice alternative to RSS syndication, which we will look at later in the chapter.

Alex King's Twitter Tools plugin is very powerful and covers a broad spectrum of Twitter integration possibilities. However, sometimes you may need to do something unique, or something that a plugin was not designed for. Maybe you just have a different itch than the plugin author. Let's take a look at how you could show your latest tweet as part of your WordPress header, without using a plugin.This would function similarly to a Facebook current status and would be like a "What I am doing right now" visual hook in the masthead of your web site. To accomplish this, you'll create a special function that will retrieve the latest tweet from your Twitter timeline. We should note that Twitter does impose rate limits on the number of times the Twitter API can be accessed per hour, so depending on your traffic levels, you may want to consider augmenting this function with caching or rate limiting of your own to avoid triggering these limits.

The first step is to create the function that will retrieve your latest tweet. This functionality would be best placed in your functions.php file. You will notice some similarity to the previously discussed example for integrating a generic XML feed. We are using the same API, just fine tuning what is being retrieved, and abstracting it into a unique function. Here is the function:

function ddamstra_getLatestTweet($twitterUser = "mirmillo") {

$url = "http://twitter.com/statuses/user_timeline/$twitterUser.xml?count=1"; $xml = new SimpleXMLElement(file_get_contents($url)); $status = $xml->status->text; return $status;

This function gets the text of the latest tweet from Twitter. It only retrieves one tweet, as seen in the query string of the URL. This status text is then returned to the function caller. Next, you will need to access this information by calling the function. In your header.php template file, you can add some code to the latest tweet:

<h1 id="blog-title"><?php bloginfo('name') ?></h1> <div id="blog-description"><?php bloginfo('description') ?></div> <div id="twitter-current"><?php echo ddamstra_getLatestTweet(); ?></div> </div><!-- #header -->

Supplement this sample with a simple link to your Twitter account and you have added a nice touch of personality to your site. Assuming, of course, that your audiences overlap and you are a responsible Twitter user.

The Twitter API is very robust and open. You can do pretty much whatever you want with it; you just need to read the documentation and this is what makes playing with the Twitter API so much fun. The result is tons of plugins to integrate with Twitter including posting to Twitter from the WordPress Control Panel, adding a "tweet this'' to your blog posts to encourage visitors to spread your word. There is even a plugin to add Twitter information to comment posts and one to use Twitter pictures in lieu of gravatars.

Twitter White Knight

Twitter White Knight

What youll find in this report is the stuff that works. Laid out step-by-step in a way that eliminates confusion and information overwhelm. Youll know exactly what to do and when to do it. No guess work here. This is the manual I wish Id had when I was starting out. It would have cut my learning curve to almost nothing and had me bringing in floods of non-spam traffic within just hours of starting.

Get My Free Ebook


Post a comment