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("<", "<", $tweet);
$tweet = str_replace(">", ">", $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.
12 Comments
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.
@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 =]
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!
Thanks for the code – modified it to suit my web page and it works great!
Thank you for the simplicity and the awesomness.
Great site, how do I subscribe?
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?
This system doesn’t work anymore!
Was great till yesterday, but now it return a blank string!
Any ideas?
best best best!!!
Hello,
I am Mark, soon 46 old, I teach sports in high school
see you,
Mark, my little blog moto
Thanks for the post i think its informative post. Nice.
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!
One Trackback
[...] 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 [...]