June 30, 2010

Display your tweets without JavaScript

by Karine in Beginner with 4 responses

I'm a site optimization freak and displaying my latest tweets with the usual code snippet was adding an other javascript to my website, so I searched for an other way of doing it, sans JavaScript. There were probably WordPress plugins for this but I hate having to install too much plugins. The first Google results do the job just fine... for english speakers. If you have special characters or accents, some PHP code won't show them properly.

I have found the solution on NickHeer.com ! Go to this page to get the code to display your latest tweets very simple with PHP or grab it here :

<?php
$username = "your username";
$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 = htmlspecialchars_decode($tweet,ENT_QUOTES);
return $tweet;
}
$twitterFeed = file_get_contents($feed);
echo('&quot;'.parse_feed($twitterFeed).'&quot;');
?>

You just have to edit your username on line 2 before using it !

Be Sociable, Share!

Tags

4 thoughts

  1. Salut Darine,

    Le hack fonctionne impeccable. Par contre, j'ai deux questions:

    - la première: comment afficher X tweets ?
    - la seconde (à deux balles): supprimer les guillemets ?

    Merci d'avance ;-)

    Reply
    • Karine
      on October 6, 2010
      at %I:%M %p

      Hello,

      pour afficher X tweets essaie de remplacer &rpp=1 par &rpp=X

      pour enlever les guillemets, remplace

      echo('&quot;'.parse_feed($twitterFeed).'&quot;');

      par

      echo(parse_feed($twitterFeed));

  2. Re Karine,

    J'avais essayais de remplacer cette valeur déjà, ça ne fonctionne pas... :/
    Par contre pour les guillemets c'est bon :-)

    Reply
  3. Alice Ralph
    on May 9, 2011
    at %I:%M %p

    Hello! Can somebody please help me with this? I can get this code to work 100% fine with every twitter account that I try... EXCEPT for the one that I am supposed to be using for it (@cominsteahouse). They have tweeted recently - I have even tried it with other usernames who tweeted longer ago than my client and it works. I don't understand why it won't work??

    Btw I am installing in the footer of a Magento ecommerce shop.

    Any advice gratefully received!!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of followup comments via e-mail. You can also subscribe without commenting.

↑ Back to Top