Latest Tweet without a Plugin

Posted by Matt on January 7, 2010 with 10 Comments

I earlier this week, one of our clients wanted a better solution to getting his latest tweet from Twitter than simply using an RSS widget it WordPress to grab the feed. We could have simply used a plugin, but this site is HUGE, and we didn’t want to depend on Joe Pluginmaker to keep his code clean and up to date, so we went about doing it the old fashioned way: PHP. After doing a bit of research, we found THIS article from Smashing Mag, and I found some code in there that was lean and clean, so I decided to use it.  Below is basically what the code looks like out of the box:

<?php
// Your twitter username.
$username = "TwitterUsername";
// Prefix - some text you want displayed before your latest tweet.
// (HTML is OK, but be sure to escape quotes with backslashes: for example href=\"link.html\")
$prefix = "<h2>My last Tweet</h2>";
// Suffix - some text you want display after your latest tweet. (Same rules as the prefix.)
$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);
return $tweet;
}
$twitterFeed = file_get_contents($feed);
echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);
?>

Basically all you have to do is put your twitter id in the top part, and you are pretty well set to go.  The only problem we ran into with this code injected into a sidebar is that the links in a tweet were html coded.  To decode it, we simple added the following line of code:

$tweet = html_entity_decode($tweet);

Add this line in right before the return $tweet and it will filter out the unwanted code. The end product should look like this:

<?php
// Your twitter username.
$username = "TwitterUsername";
// Prefix - some text you want displayed before your latest tweet.
// (HTML is OK, but be sure to escape quotes with backslashes: for example href=\"link.html\")
$prefix = "<h2>My last Tweet</h2>";
// Suffix - some text you want display after your latest tweet. (Same rules as the prefix.)
$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);
?>

-matt

Share and Enjoy:
  • StumbleUpon
  • Twitter
  • Facebook
  • del.icio.us
  • Digg
  • Google Bookmarks
  • Print
  • email
  • RSS
  • Tumblr
Filed Under: WordPress

Get Updates via Email:


Don't miss a single post. Enter your email below to receive my latest blog post via email.


And... Follow me on Twitter.

Comments

  1. Greg Kerstin says:

    Thanks, works great! WordPress works well with anything PHP, and this appears to be a solid hand-coded approach.

  2. Peter Gent says:

    Trying to get this working on my site. Fricking awesome tip – thanks a bunch.

  3. Peter Gent says:

    Also how can I change the colour of the tweet – I’d like it in white contrasting with quotation marks to enclose it on either side of the tweet. Is that possible?

    • Matt says:

      @Peter – You can change the color in your style.css. I recommend using the FireFox addon Firebug to find the necessary class or id you need to change.
      Glad you found this useful.

      -matt

  4. Nice work! Ive already used this several times, and also integrated it into a wordpress admin option

  5. At risk of overlooking the obvious…what is the best way to use this in the widget area. As a snippet of php code, I can’t just place this into a Text widget and add it to my page. Is there some php wrapper function that I am missing?

    Or, should I employ a plugin, i.e, Samsarin’s PHP Widget Plugin? Perhaps there is a better “iThemes-blessed” approach? I looked in the Support forums but didn’t find anything specific to this.

    • Matt says:

      Bruce – the standard text widget does not support php. I tend not to like plugins that make widgets that can execute php, because they are a potential security risk. Instead, you could go in to your template files and add the above code in where the widgetization code is now. As long as you put it inside the same div as the widget code, it will get the same styling as it would if it were a widget.

      Hope that helps

      -matt

  6. Hi, this is great, I knew this was possible, thank you.
    I have some issues with UTF-8 encoding, special characters are not displayed right, just the code instead e.g. & as &
    I’m not sure if it is an issue with my template or the code but will be glad if you can help me.
    Thanks again,
    Daniel

    • Matt says:

      Daniel -

      Make sure this line is intact in your code:

      $tweet = html_entity_decode($tweet);

      That is the line that fixes the html encode. Once the tweet passes through that, it should be cleaned up.

      Let me know if you are still having the problem and I will see if I can help you chase it down.

      -matt

  7. Kath says:

    hi matt,
    thanks, awesome code! worked perfectly,
    one question though – how do i make it post morethan 1 tweet? right now, it was only showing 1 tweet. i thought by editing this part “&rpp=1″; will fix the problem (change the number 1 :P ) but it didn’t. hope you can help me. thanks in advance :)

Leave a Reply