I am Lee Baillie

Display Tweets on Your Wordpress Blog Without Plugins or Widgets

Yes, Twitter has an HTML widget that you can paste easily into any page. But the truth is that this widget is a little clunky and adds code to your page that can be a pain to style.

If you have a Wordpress blog, the whole process just got a little simpler with the wonderful hack displayed below. Simply replace *YOUR-TWITTER-USERNAME* with, you guessed it, your Twitter username and you’re done!

You can play around with it and add a prefix and a suffix where required. For example, you might wish to place a ‘follow me on Twitter’ link at the end of your tweet.

A perfect example of its use can be seen at the top of this page. I’m using it myself.* And you don’t need to worry about @somebody links — they show up too.

Simply paste the PHP below where you want your tweets to appear and you’ll be up and running in no time. No clunky external JavaScripts or Wordpress widgets.


<?php

$username = "*YOUR-TWITTER-USERNAME*";

// PREFIX - Place anything you want to appear BEFORE your tweet here
// WARNING: You can use HTML but remember to backslash any quotation marks (e.g. style=\"background:#FFF;\")
$prefix = "<h3>I tweet about...</h3>";

// SUFFIX - Place anything you want to appear AFTER your tweet here
// WARNING: Same as for PREFIX
$suffix = "<br /> <a href=\"http://twitter.com/*YOUR-TWITTER-USERNAME*\">follow me on Twitter</a>";

$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);
?>

Happy blogging/tweeting.

* I was using this piece of code but I’ve since re-designed my site. I needed slightly more functionality and this little snippet just didn’t quite cut it so I’ve had to revert back to Twitter’s own HTML widget.

Post a comment or leave a trackback: Trackback URL.

12 Comments

  1. Posted 30 Apr 2009 at 23:38 | Permalink

    I tried creating a static web page and copied and pasted this into the HTML editor in a wordpress post. It just shows the code and not my twitter feed. :sad:

    ReplyReply
  2. admin
    Posted 01 May 2009 at 6:45 | Permalink

    @pete

    I hadn’t really intended this code to go into a Wordpress post — It’s usually best simply pasting it somewhere in the template. I guess it’s still possible, though…

    Wordpress doesn’t allow you to add PHP code to posts by default but you could try the plugin below. Then your Tweets should start showing up!

    http://wordpress.org/extend/plugins/include-it/

    Let me know how it goes =]

    ReplyReply
  3. Posted 07 May 2009 at 20:58 | Permalink

    Any way to get the date and time of the Tweet included?

    I used the plug-in you recommended to put the tweet inside a WordPress page, and it worked great!

    ReplyReply
  4. Posted 17 May 2009 at 19:22 | Permalink

    Thanks for the code – modified it to suit my web page and it works great! :mrgreen:

    ReplyReply
  5. Posted 28 Jun 2009 at 9:36 | Permalink

    Thank you for the simplicity and the awesomness.

    ReplyReply
  6. Posted 03 Oct 2009 at 9:45 | Permalink

    Great site, how do I subscribe?

    ReplyReply
  7. mark
    Posted 15 Oct 2009 at 8:30 | Permalink

    IT does displays the tweets but why is that when i validate it on W3C, it sees a lot of errors? is there anyway we can solve this?

    ReplyReply
  8. Posted 14 Nov 2009 at 10:30 | Permalink

    This system doesn’t work anymore!
    Was great till yesterday, but now it return a blank string!
    Any ideas?

    ReplyReply
  9. Posted 29 Dec 2009 at 14:46 | Permalink

    best best best!!!

    ReplyReply
  10. Posted 06 Jan 2010 at 11:10 | Permalink

    Hello,

    I am Mark, soon 46 old, I teach sports in high school

    see you,

    Mark, my little blog moto

    ReplyReply
  11. Posted 18 Jan 2010 at 9:17 | Permalink

    Thanks for the post i think its informative post. Nice. :smile:

    ReplyReply
  12. Posted 08 Mar 2010 at 17:46 | Permalink

    Good day!
    I would like to present interesting site:
    acomplia prezzo acomplia doctor ceftin apotheke ceftin espana celexa pharmacy celexa pharmacy cleocin fedex cleocin sale diflucan prix diflucan purchase elavil uk elavil online hytrin price hytrin drug imitrex prix imitrex prix lexapro sale lexapro canada prednisone pharmacy prednisone prescription remarin buy remarin generic prevacid overnight prevacid discount sustiva overnight sustiva buy testosterone cheap testosterone oral topamax apotheke topamax doctor viramune doctor viramune effects xenical nextday xenical buying zyban pharmacy zyban price zyloprim prix zyloprim espana zyprexa pharmacy zyprexa overnight
    To greet!
    See you!

    ReplyReply

One Trackback

  1. [...] Display Tweets on Your Wordpress Blog Without Plugins or Widgets Posted by root 2 hours 21 minutes ago (http://leebaillie.com) Post a comment or leave a trackback trackback url what makes a good web designer what do you know i 39 m proudly powered by wordpress Discuss  |  Bury |  News | Display Tweets on Your Wordpress Blog Without Plugins or Widgets [...]

Post a Comment

Your email is never shared. Required fields are marked *

*
*

I tweet, I tweet

    Stalk me on Twitter »

     

    Yes sir, I can boogie

    Some of the albums I listen to the most in alphabetical order. Click the album to view it in the iTunes store.

     

    Hotly anticipating...

    Doctor Who - New series starts in Spring 2010