Easy Code to Show Your Most Recent Tweets in WordPress
I’ve been reworking my website today and decided it was time to add a recent Twitter tweet to the front page. I checked out the Twitter widget and several (of the many) WordPress plugins that display your most recent tweets, but decided that they had a few issues.
I went looking for a solution. The result, 15 lines of PHP you can use to accomplish the goal. No need to mess with plugins. Instead, you pull your tweets from your own Twitter RSS feed using the simpler Twitter Search API, not the Twitter REST API.
Here’s the article I found and used as the basis for my own solution. I’ve made a few modifications to the parsing function that I think provides a better, more flexible outcome. One change is that I add the rel=“nollow” attribute to each “a” tag. Since the content you are displaying (in your tweet) is your own, Why would you want the search engines sending some of your hard-fought link juice back to Twitter?
The second change I made was to parse the array an additional time so as to separate out the body of the tweet from the metadata. I just want to display my tweet. That’s all. But, the new array can be used as you see fit.
<div id="my_tweets">
<?php
// Your twitter username; replace below; keep username in quotes
$username = "blahblah";
// Content you want to include before your tweet; I'm using this for a quick title
$prefix = "<h2>My Latest Tweet</h2>";
// Content you want to include after your tweet
/*$suffix = "<br /><br />You should follow me on Twitter";*/
$latest_tweet = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=1";
function parse_feed( $latest_tweet ) {
$tweet_array = explode( "<content type=\"html\">", $latest_tweet );
$tweet_array2 = explode( "</content>", $tweet_array[1] );
$tweet = $tweet_array2[0];
//No need for search engines to follow twitter links or tags; add no follow
$tweet = str_replace( "<a ", "<a rel=\"nofollow\" ", $tweet );
$tweet = str_replace( "<", "<", $tweet );
$tweet = str_replace( ">", ">", $tweet );
//some extra tweet metadata you can use if you want
$tweet_extra = $tweet_array2[1];
return array ( $tweet, $tweet_extra );
}
list ( $tweet, $tweet_extra ) = parse_feed( file_get_contents( $latest_tweet ) );
echo stripslashes( $prefix ) . html_entity_decode( $tweet );
// If you want to include more content after the tweet, use this instead
/*echo stripslashes( $prefix ) . $tweet . stripslashes( $suffix );*/
?>
</div>
I simply placed this code in a division within my index.php file. You can place the code wherever you like—in a sidebar, the footer, a separate page, etcetera. Of course, you could also add it to your functions.php file and simply call your custom twitter-feed function in your template. It’s your choice.
Also, I imagine this simple code could be adapted for use in BuddyPress as well.
To see how it looks on my site, just go to my homepage.
By the way, it may come as little surprise to you, but I am not a design genius nor even a moderately-skilled designer. I choose to spend my WordPress time either coding or actually creating content for my site. So, you are on your own as far as positioning the output. But, I hear that you can use CSS to do that.
Have fun!












What if you wanted to grab the 2 most recent tweets?
Need a loop?
Kory-
All you need to do is the following:
1.) Request the two most recent tweets by setting rpp = 2, instead of one, in the query string.
2.) Then, in the parse_feed function, add an additional variable to extract the second tweet array element:
$tweet_array3 = explode( “”, $tweet_array[2] );
3.) You’ll next need to add another $tweet variable and process it in the same way as the first one–making sure that where ever there is a $tweet variable referenced, you create a new reference for $tweet2. This could be done via a loop if you wished.
$tweet2 = $tweet_array3[0];
… etcetera
4.) Finally, you’ll need to make sure that the new $tweet2 variable is returned and also is referenced in the list() function.
return array ( $tweet, $tweet2, $tweet_extra );
…
list ( $tweet, $tweet2, $tweet_extra ) = parse_feed( file_get_contents( $latest_tweet ) );
Style to your liking!
How would the function be, to grab as many latest tweets as you want? not just 1 or 2.
Thank you.
Great code, thanks!
I’m using it to display three tweets on my site. One question though: How can I stop it showing the codes for non-alphabet characters, and actually show the characters?
Ex. “Today was great, I’m definitely doing that again” shows up as “Today was great, I'm definitely doing that again”
Oh crap, it replaced the code with the apostrophe. It shows up as
'.One last try. It shows up as:
& apos ;
but without spaces.
Ah, I thought I had corrected that issue already in the code above. I guess I never made the change.
Look at the code again, on line 33, I’ve changed the echoed output by passing it through an additional function to decode an HTML entities.
That worked great, thanks Jeff!