Matt Danner
  • Blog
  • Contact
  • Twitter
  • Instagram
 January 7, 2010

Latest Tweet without a Plugin

***UPDATE***
As of June 2013, this no longer works because of a change in the Twitter API. Sorry…

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

***** UPDATE: This code has been updated a few times since this post. I know it kind of goes against the title of the post, but if you would like to use this exact code in a plugin that already has style and everything applied, see this post.*****

15 Comments
Categories : WordPress
Tags : matt danner, php, twitter, WordPress, wordpress development
← Next Post
Previous Post →

Comments

  1. Greg Kerstin says:
    February 12, 2010 at 9:24 pm

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

    Reply
  2. Peter Gent says:
    February 15, 2010 at 4:10 pm

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

    Reply
  3. Peter Gent says:
    February 15, 2010 at 4:15 pm

    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?

    Reply
    • Matt says:
      February 16, 2010 at 5:26 pm

      @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

      Reply
  4. Vince DePalma says:
    March 1, 2010 at 7:53 pm

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

    Reply
  5. Bruce Caraway says:
    March 3, 2010 at 12:50 pm

    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.

    Reply
    • Matt says:
      March 3, 2010 at 2:44 pm

      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

      Reply
  6. Daniel Villegas says:
    April 5, 2010 at 11:57 am

    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

    Reply
    • Matt says:
      April 6, 2010 at 4:20 pm

      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

      Reply
  7. Kath says:
    July 6, 2010 at 11:02 pm

    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 🙂

    Reply
  8. Antone Dockstader says:
    January 21, 2011 at 4:59 am

    I don’t like embedding strange javascript on my site, it makes it insecure. I’d rather have straight html, like this widget.

    Reply
  9. Newbie says:
    January 21, 2011 at 4:14 pm

    Will you please elaborate on why it’s better to decode the html? Thanks!

    Reply
    • Matt says:
      January 31, 2011 at 11:37 am

      the function html_entity_decode() basically just does the opposite of htmlentities(). It takes HTML entities and changes them to their applicable characters. Basically, special characters in your tweet will be displayed as their HTML entity unless you decode it, and that will not look like the original tweet.

      I hope this explains it a little better for you.

      -matt

      Reply
      • Geoffrey says:
        September 20, 2011 at 7:30 am

        Hi Matt,

        I am really trying to get this to work but my knowledge of wordpress and code is little html and font end stuff. Do i have to create a new php file and drop it into to my wordpres files in my filezilla to do all this or can i put it straight in to the page which at the moment isn’t working.

        Thank you

        Reply
        • Matt says:
          September 20, 2011 at 8:33 am

          Geoffrey –

          The simplest way to use this would be to just add it to the existing page template (php file) that you want it to appear on. For example, if you want it on your single posts, you could add it to single.php.

          Hope that helps.

          -matt

          Reply

Leave a Reply to Peter Gent Cancel reply

Your email address will not be published. Required fields are marked *

The Internet Home of Matt Danner Copyright © 2021
iThemes Builder by iThemes