To display your latest twitter entry, just paste the code below anywhere on your blog.
<?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("<", "<", $tweet);
$tweet = str_replace(">", ">", $tweet);
return $tweet;
}
$twitterFeed = file_get_contents($feed);
echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);
?>
That's all! Your latest twitter entry is now displayed on your blog.
Credits goes to Ryan Barr for this awesome recipe!
30 Responses
This is a great recipe. If you’re interested in going the other way, and allowing a user to send a link to Twitter, you can use the code here:
http://pastebin.com/f199c95d9
This alleviates the need to use a third party such as TwitThis.com - and it let’s you customize the message you’re visitors Twit. See it in action on my site (click my name - no spam urls from me!).
Thanks for your link, this will probably be the subject of another recipe
I get errors:
Warning: explode() [function.explode]: Empty delimiter in [URL removed on purpose] on line 34
Warning: explode() [function.explode]: Empty delimiter in [URL removed on purpose] on line 35
How do I solve this?
Found out by visiting the original code…
Your code:
$stepOne = explode("", $feed);$stepTwo = explode("", $stepOne[1]);
Should be replaced with:
$stepOne = explode("", $feed);$stepTwo = explode("", $stepOne[1]);
Damn, the code won’t show
should be between the empty quotationmarks (minus the space after the <)…
Sorry, the code won’t show… but compare your code with the one shown in the original link, and you’ll see the difference…
@m-alo: You’re totally right. Sorry for that mistake, I’m correcting the code right now. Thanks for letting me know!
Yet another great code from you, Jean. I’m seeing the big jump on your subscribers count and to achieve it in such a short period of time is no easy feat.
Hey, I love the idea of “recommended premium themes” on your footer. What a smart way of affiliating! Keep up the good work, buddy.
Yan
@Blog for Beginners: Thanks for your compliments, Yan! About the footer, I probably write a tutorial on how to achieve this on Cats Who Code, so stay tuned
Have a great day my friend!
I sure could do with this. Have been looking for a Twitter “widget” thingy for a while now. This cut-and-paste code would do great for me. Thanks!
Very good way to find out mistakes. Beta version rules
Can we get that twiter code with no mistakes? Thanks.
@Daniel Richard: I have corrected the code, so it works now
Anything I can do to lessen the plugin load of a site is well worth it. And this code did the trick in two seconds flat.
Thanks for sharing it; and thanks to Ryan for creating it.
thans for your share,I have copy your code and set my twitter user name,but it doesn’t work,what’t wrong whit it.
Yes, I have also copied your code and changed the username but I am getting error messages too. A warning comes up about URL file-access is disabled in the server configuration. Any thoughts?
Wondering if it was possible to allow multiple instances of this? We all use twitter on the site and would like to show all 4 of our latest tweets. is this possible? I tried coping it and got a “cannot declare parse_feed()”
Is it possible to hide @replies from showing up ?
Even better, how about adding a specific hashtag to only the posts you want to show up on your blog from twitter. Modify the search feed definition as follows:
$blogtag = “twitblog”; //Replace this with whatever your hash tag is
$feed = “http://search.twitter.com/search.atom?tag=” . $blogtag . “&from:” . $username . “&rpp=5″;
Simple receipt but it works, thanks a lot!
ohw no…my blog error when copy paste to widget.php. Why i should ???
Trackbacks: