Written by on

Get Your Latest Tweet on Your Blog – No Plugin Required

Everyone wants more followers on Twitter. Who wouldn’t? A great way to drive traffic from your blog to your twitter page is to include your latest tweet somewhere on your blog. twitter-img I have seen a plethora of WordPress plugins that can accomplish this, but I really didn’t want to mess with finding one that did what I needed, and was stable.  So, I went about doing it without a plugin.

I did some looking around, and found some popular code to get me started.  It didn’t do things exactly the way I wanted, but after a bit of modification, I think it is WordPress ready.

Start by creating a file in your theme’s folder called twitter.php.

Now paste the following code inside:

$prefix = "<h2></h2>";
$suffix = "";
$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=1";
function parse_feed($feed) {
$stepOne = explode("<content type=\"html\">", $feed);
$stepTwo = explode("</content>", $stepOne[1]);
$tweet = $stepTwo[0];
$tweet = str_replace("&lt;", "<", $tweet);
$tweet = str_replace("&gt;", ">", $tweet);
$tweet = html_entity_decode($tweet);
return $tweet;
$twitterFeed = file_get_contents($feed);
echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);

Don’t forget to replace the filler text in the first line with your twitter id, but leave the quotes. This is the meat of the code, and it does the leg work of fetching your wit and charm and bringing it back to your blog. Now, where do we want to put it? Let’s say we want to add it to our sidebar.  Pretty simple.  Open up your sidebar file (hopefully it is named sidebar.php) and add this code where you want the tweet to go:

<?php include (TEMPLATEPATH . '/twitter.php'); ?>
Of course you are going to want to add some style to it, so wrap that code you just inserted with a div and give it an id of “last-tweet” like this:

<div id="last-tweet">
<php include (TEMPLATEPATH . '/twitter.php'; ?>

Now, we can style that id in the style.css file.  Scroll to the very bottom of your stylesheet and add some style:

#last-tweet {
background: transparent url('images/twitter_bg.jpg');
padding: 10px auto 5px;

Keep in mind that this is just example style, and you will have to modify it to fit your needs (unless for some reason you have a photo in your images folder named twitter_bg.jpg that needs that exact padding to make the text fit).

This should get you well on your way to having a sweet custom twitter feed on your blog.  You can check out an example where I have used this exact process to get latest tweets on a live site by heading over to baptistmessenger.com.


  1. @Stephen – If you add the code in right before the widgetization code, it will not interfere. My only issue with using a PHP widget is that those plugins use evals that can be dangerous if not properly maintained by the developer. I just don’t like taking that risk. Thanks for your input and tip. I really appreciate it.

    @Kerry – I am glad you enjoyed it. It really is a fun little project for when you have a few extra minutes.



Sale Ends Today! Save 35% OFF BackupBuddy Get the coupon