I created yet another Twitter widget for WordPress. There are many out there in the field but none that were to my liking that generated as yummy XHTML code as this one. Also, a lot of the ones out there parsed the feed using JavaScript instead of PHP on the server side.
So here it is, the ‘Elegant Twitter Widget’. The code is heavily commented and the output is in template functions so everything is fully customizable. The widget works just like any other good widget. Nothing crazy.
The XML is parsed using the SimpleXML PHP extension so PHP >= 5.0 is required. I may rewrite this using the DOM XML in the future. I have rewritten the widget using the PHP XML parser to support PHP 4 and above. If you are running PHP >= 5.0, you may want to download version 1.0, which features cleaner code thanks to the SimpleXML extension.







10 Comments
Permalink
How do I configure it?
Permalink
If you look at the file elegant-twitter-widget.php, you will find functions topwrapper_template(), status_template(), and bottomwrapper_template().
topwrapper_template() is called once at the beginning of the widget. status_template() is called for every twitter update displayed. This function uses parameters documented right above the function code. bottomwrapper_template() is called once after all the twitter updates have been displayed.
To configure the username or number of updates to display, edit the preferences under the Widgets tab in your WordPress administration panel.
Permalink
He Paul, thanks for this widget. Thats exactly this, what I’ve been looking for several weeks last year. Later i decided to you use an JS-solution, but it was horrible.
Yours is much cooler.
Is there a possibillity to replace umlauts in the $text?
The parser cuts the $text at each umlaut and ends the imtem by closing the list-element. Than it starts with a new list-elemnent that includes only the umlaut. And at least in a third list-element the $text is continued with the first letter after the umlaut and finished correctly. Crazy.
Permalink
Hey Carsten. I’m pretty sure that is an issue with encoding and is possibly throwing off the PHP XML parser. First of all, if you are running PHP >= 5.0, you can try using the 1.0 version of this widget, which uses SimpleXML instead.
However, you can do a str_replace on the variable $response before it is sent to the parser. In the code, search for the line $response = file_get_contents($url); and add the str_replace right after to get replace anything inside the XML string you want.
I hope this helps.
Permalink
Hey Paul,
My web host doesn’t allow file_get_contents(), but they had a way to use cURL to do the same thing.
Here’s the tidbit of code that I found (at http://wiki.dreamhost.com/CURL) that I slightly modified (changed variables where needed):
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$response = curl_exec($ch);
curl_close($ch);
Permalink
Hi Paul. This truly is an Elegant Twitter Widget! Thanks!
Also! I found a great function/reg expression borrowed from another Twitter Widget that hyperlinks links and @ replies. I thought you might be interested.
The widget I got the function from is here: http://anwanore.com/projects/mytwitter
They attribute it to @krokodilerian - http://vasil.ludost.net/blog/
Here’s the function:
function myTwitterFormatter($tweet) {
$tweet = ereg_replace(”[[:alpha:]]+://[^[:space:]]+[[:alnum:]/]”,”link“,$tweet); // turn any URL’s into links
$tweet = ereg_replace(”@([a-zA-Z0-9]+)([^a-zA-Z0-9])”,’@\\1\\2′,$tweet); // add “@username” links
return $tweet;
}
You can see your widget, equipped with that function on my blog.
Thanks again!
Permalink
Hi again Paul.
I seem to be having issues with the date stamp of my twitter posts. Looks like they’re just wrong. Is anyone else having that problem? I wonder if it’s a server configuration to script compatibility issue on my end.
Permalink
Hi Dusty, you’re not alone. I am seeing incorrect date stamps on mine as well.
Permalink
If you’re having incorrect date stamps, it’s probably related to differences in date calculations between PHP4 and PHP5. I ran into the same problem when I wrote the MyTwitter plugin. I fixed it a while back. Feel free to check out the code if you like if it helps you fix your problem — it’s Creative Commons so just make sure to share the love.
Our plugins are similar — I felt the same way about wanting to write my own so I understand what you’re going for.
Permalink
FYI - If you (like me) are using a host like godaddy that doesn’t allow get_file_contents(), you can do the request via cURL.
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$response = curl_exec($ch);
curl_close($ch);