Follow Jeff Sayre on Twitter

Easy Code to Show Your Most Recent Tweets in WordPress


Note: The code below will no longer work as it uses a now deprecated Twitter API. Unfortunately, to grab your tweets, or any tweets from the public timeline, you have to set up an OAuth connection. If you’re interested in doing that, see my last comment in this post and visit the link I’ve posted there. I will not be providing any more support on this post.

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">
		// 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 = "" . $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( "&lt;a ", "&lt;a rel=\"nofollow\" ", $tweet );			    						    						    
		    $tweet = str_replace( "&lt;", "<", $tweet );
		    $tweet = str_replace( "&gt;", ">", $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 );*/

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!

Article Comments

  1. Kory says:

    What if you wanted to grab the 2 most recent tweets?

    Need a loop?

    • Jeff Sayre says:


      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!

  2. Iair S. says:

    How would the function be, to grab as many latest tweets as you want? not just 1 or 2.

    Thank you.

  3. jobbogamer says:

    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”

  4. Nate says:

    Thanks Jeff, I tried multiple methods and finally your method worked.

    Thanks for sharing!

    All the best,


    ps. we share similar interests and I would enjoy connecting with you on LinkedIn or elsewhere.

  5. John Sanabria says:

    It worked like a charm, Jeff, thanks!

  6. ReF says:

    I see the following error message in your homepage:

    Warning: file_get_contents( [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 410 Gone in /home/sayremedia/ on line 86

    Take care 😉

    • Jeff Sayre says:

      ReF –

      I have not had time to look into what might be going on. As this post was more than three years old when you posted your comment, I’m assuming that there is a change to Twitter’s API.

      • Jeff Sayre says:

        After a few minutes of investigation, it turns out that v1.0 of Twitter’s API was deprecated on June 11th 2013. You now must use v1.1. To do so requires that you setup an OAuth session. You can no longer do a simple HTTP request and parse the returned JSON data.

        As jumping through all the hoops to establish an OAuth connection for a personal blog is too time intensive, you will have to find a plugin or a service that can retrieve your desired Tweets. I will not be able to provide a simple piece of code to allow tweet retrieval.

        See this thread on Twitter’s 3rd-party developer site for more details:

        Here’s a possible solution. Please note that I have not tried out this method and do not vouch for its suitability or security:

Share on Twitter
Share on Facebook
Share on FriendFeed
Share on LinkedIn
Share on StumbleUpon
Share on Digg
Share on Delicious
Share on Technorati
Add to Google Bookmarks