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!
72 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 ???
how do I display more than one tweet?
If u want to display more than 1 post and record your tweets into SESSION, use this script (made by my friend Gustavo Straube):
$twit = stripslashes($twit);
$twit = preg_replace(‘/(http:\/\/[a-z0-9\-_\.\/\?&=%]+)/i’, ‘$1‘, $twit);
echo $twit;
$twits = NULL;
if (!empty($_SESSION['twits'])) {
$twits = $_SESSION['twits'];
} else {
$username = ‘username’;
$feed = “http://search.twitter.com/search.json?q=from:{$username}&rpp=10″; // displays your last 10 tweets
$result = @file_get_contents($feed);
preg_match_all(‘/”text”:”(.*?)”/i’, $result, $m);
$s = count($m);
if ($s > 1) {
$twits = array();
foreach ($m[1] as $twit) {
$twit = stripslashes($twit);
$twit = preg_replace(‘/(http:\/\/[a-z0-9\-_\.\/\?&=%]+)/i’, ‘$1‘, $twit);
$twits[] = $twit;
}
$_SESSION['twits'] = $twits;
}
}
if (!empty($twits)) {
foreach ($twits as $twit) {
echo “” . $twit . “”;
}
}
I tried integrating this script into a theme I’m developing and it no longer works. Is it possible that Twitter has changed something that would be causing this?
Hey, This is an AWESOME fix for a single tweet, but multiple tweets are a little more difficult, I wrote this(its 100 lines so you may want to turn it into a function..)
http://killainstnct.com/?p=78
Check it out if you want to display basically an unlimited number of your tweets(or however many twitter remembers?)
Thanks for this recipe. Really helpful
I used this solution (ie: pupidizuccaro.com) but it doesn’t work anymore.
Why?
Some week ago it was good.
Here is my code http://pastebin.com/f43935b94
Using this in a new theme I’m developing but would like to display the single tweet with retweet, reply, and follow links. Aswell as one to take them directly to my twitter page of course.
Is there something as simple and n00b-coder friendly that’ll do this (that isn’t a plugin)?
I just added your code to my website but found that it was adding my6 website as a prefix of any links in my latest tweet. Any ideas on why this is happening and how I fix it?
Does this just work for wordpress blogs or also for my homepage?
Thanks for this, very useful!
Any idea how one could go about deferring the twitter info to load last on a page?
The issue is, putting this in a custom sidebar prevents the rest of the sidebar from loading until twitter has return the tweet.
Depending on the user’s geo-location in relation to the Twitter server & their connection type (many upcoming WLAN services have broadband speed but also with high latency for initial server requests), it could take another second or 2.
This extra delay is undesirable, would rather the load / user experience be:
1) Page loads and looks nice.
2) Any external calls are then loaded and it just works.
Any ideas? Thanks again!
i am facing a problem when i add your code. it gives error like this
Warning: file_get_contents() [function.file-get-contents]: php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\wamp\www\TestBlog\wp-content\themes\Kennedy\l_sidebar.php on line 60
I can get it to work.
I get this message:
Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in /var/www/vhost/creapolispublicidad.com/home/html/01/wp-content/themes/creapolis/index.php on line 66
@Thomas – Just add this code:
$tweet = str_replace(“"”, ‘”‘, $tweet);
after the other two str_replace and it will work just fine.
Links do not work in my tweets. My domain always proceeds the link url. Any ideas? I tried the solution above but it didn’t work.
Thanks!
Hi, this code works perfectly, it’s wonderful!
My only problem is that the apostrophe in -We’re- looks like ' so it’s barely readable when you tweet has many apostrophes…
Any solution about that? Thx!
ok, Twitter noob here.. So I’ve got this code snippet on 3 different servers; Localhost, Media Temple, and 1&1. I could not get it to work on any of them. I did adjust the username.. I am starting to wonder if there is a setting I am missing in my Twitter account? I have double checked the “protect my tweets” and it is turned off.
On a web server I get a couple errors “Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled……” On my local host I don’t get an error, just no tweet.
I’ve also tried taking this line “http://search.twitter.com/search.atom?q=from:usernamehere” and placing in the URL, but no feed shows.
Is this a setting in my Twitter account?
Thanks!
(been pulling my hair out for an hour now and it’s probably something simple, haha)
Can i detect urls in ma tweet? My div for my tweets is 150px width and if i post a link with more than XX characters the url will go out of the box =( so i want to detect the url and wirte a function to place a into the title of the link. cause if u count ur tweet characters and break after 20 u dont get the url only 20 characters of the html… i hope someone can help me
@Thomas and @Neil, if you still need solution for broken links – try this:
$tweet = str_replace(‘"’, ”, $tweet);
Hope this helps!
Thanks for the great snippet of code! My latest tweets are displaying well on the site I’m working on.
Unfortunately, I’m having the same problem as Thomas and Neil and neither of the solutions offered have worked for me. I had a look at Neil’s website and he still seems to have the same problem with the website link preceding the external link.
This one:
$tweet = str_replace(“””, ‘”‘, $tweet);
gives me this: Parse error: syntax error, unexpected ‘”‘
And this one:
$tweet = str_replace(‘"’, ”, $tweet);
isn’t making any difference to before the line was inserted.
Any suggestions would be greatly appreciated!
Hi Tegan,
If your links are still preceeded with the domain link, try this:
$tweet = str_replace(‘"’, ”, $tweet);
instead of this:
$tweet = str_replace(‘"’, ”, $tweet); (the semi colon is missing from the quote charceter entity which is my bad!)
Aah, the form stripped out the quote character entity in my first code line.
instead of this ", use "(add semi colon here)
Hope this helps!
I searched for something that can replace plugins for display of twitter updates. This seem simple enough but I wish to add the date of the tweet, how can I do this?
Thanks
Hi,
I get this problem…
Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in /var/www/vhost… on line 163.
Why is this happening?
Thanks
hello ,
this code doesnt work ,
do you have the updated version of it ?
thanks
I believe this should help:
$tweet = str_replace(‘"’, ”, $tweet);
Worked for me
Or yet even better:
$tweet = str_replace(‘"’, ‘”‘, $tweet);
Another fix:
There was a problem with ‘ sign. For example word: “can’t” was displayed as: “can't”.
To fix it try to use this:
$tweet = str_replace(‘'’, “‘”, $tweet);
Still can’t get links to be clickable with this code. Any one know a solution?
@ Emil
still doesn’t work -
Trackbacks: