SJL Web Design

Show off your Latest Tweet using SimplePie

We have already written about SimplePie in one of our earlier posts explaining how to pull information from an RSS feed onto your website. In this blog post we will show you how to display your latest twitter tweet on your website using the same method and the same library.

If you haven’t already got SimplePie installed on your website head over to and download the latest version of the library. Unzip the folder and take the file and upload it to your server, in this example I’m going to upload it into a folder called inc short for includes.

Once you have uploaded the file to your server you are ready to start rocking the code. If the extensions of your pages are anything else than .php for example .html or .htm you will need to change the them to .php, this will make them compatible with the SimplePie PHP code.

Now we are going to need to include the SimplePie library by adding the following piece of code at the very top of each page you want the tweet displayed on:

<?php require_once('inc/'); ?>

Now that SimplePie has been included on your site we are going to need to give it the URL of your Twitter feed using the following lines of code:

$feed = new SimplePie('');

You will need to add these lines just before the closing PHP tag (?>) and after the include. You are then going to need to change TWITTERUSERNAME to your unique Twitter username.

So altogether the block of code at the top of your page should be looking like this:

$feed = new SimplePie('');

Displaying the Tweet

Now we are going to need to pull the tweet, the link to the tweet and the date the tweet was tweeted. To do this we are going to add the following code to the area of the page we want the tweet to be displayed:

<?php foreach ($feed->get_items(0, 1) as $item): ?>
<li><?php echo $item->get_description(); ?> <span><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_date('D, M j, Y'); ?></a></span></li>
<?php endforeach; ?>

This will now display your latest tweet in an unordered list, with the date linking back to your Twitter page. If you would like to display more than one tweet you can change the value from 1 to the number of tweets you would like displayed on this line:

<?php foreach ($feed->get_items(0, 1) as $item): ?>

Leave a Reply