Posted By

thehamilton on 11/11/09


Tagged

wordpress tweet


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

vintagekrug
BrianCoyDesign
diegotrap


Display your latest tweet on Wordpress without a plug-in


 / Published in: PHP
 

URL: http://www.smashingmagazine.com/2009/03/04/15-useful-twitter-plugins-and-hacks-for-wordpress/

This ready-to-use code can be pasted anywhere in your theme files. Just don’t forget to change the value of the $username on line 4. The $prefix and $suffix variable can be used to insert a title, and the div element can be used for further CSS styling.

  1. <?php
  2.  
  3. // Your twitter username.
  4. $username = "TwitterUsername";
  5.  
  6. // Prefix - some text you want displayed before your latest tweet.
  7. // (HTML is OK, but be sure to escape quotes with backslashes: for example href=\"link.html\")
  8. $prefix = "<h2>My last Tweet</h2>";
  9.  
  10. // Suffix - some text you want display after your latest tweet. (Same rules as the prefix.)
  11. $suffix = "";
  12.  
  13. $feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=1";
  14.  
  15. function parse_feed($feed) {
  16. $stepOne = explode("<content type=\"html\">", $feed);
  17. $stepTwo = explode("</content>", $stepOne[1]);
  18. $tweet = $stepTwo[0];
  19. $tweet = str_replace("&lt;", "<", $tweet);
  20. $tweet = str_replace("&gt;", ">", $tweet);
  21. return $tweet;
  22. }
  23.  
  24. $twitterFeed = file_get_contents($feed);
  25. echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);
  26. ?>

Report this snippet  

You need to login to post a comment.