Good Ideas

Add the last Tweet on your blog

Posted by:

|

On:

|

Many of you ask me how to add the last tweet in the web site header, as you can see in action on the Dynamick top. Now I’m going tell you how to do it.

The below script is written in PHP language and it has to be placed in the functions.php inside your theme folder. This is the script:

[code lang=”php”]
function getTweet() {
if (date("s")==30) {
$username=’dynamick’; // set user name
$format=’json’; // set format
$tweet=json_decode(file_get_contents("http://api.twitter.com/1/statuses/user_timeline/{$username}.{$format}")); // get tweets and decode them into a variable
# $link = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\" rel=\"nofollow\">\\0</a>", $tweet[0]->text); # experimental
$link = $tweet[0]->text;
if ($link!="") file_put_contents(‘twitter.txt’, $link, FILE_TEXT|LOCK_EX );
} else {
$link = file_get_contents(‘twitter.txt’);
}

return $link; // show latest tweet
}
[/code]

The only thing you’ve to do is to configure your twitter username. This script save your last tweet in a text file and is updated only if the request is done at second “30”. This reduce the http traffic from your blog.

This function has to be called from inside the header.php, where exactly you want to see the tweet. Hope this script could help you. Stay tuned!

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *