How to show your latest tweets on your WordPress site without a plugin

Showing your latest tweets on your WordPress site is a good way to help your readers staying tuned with you, and doing it became a such common task. A plug-in can do that job but it’s simple tasks and if you are WordPress themer you probably should do it without a plug-in.

If you want to show only the latest tweet on your site, the method Ryan Barr suggested is good to go. (Although he said this method can show multiple tweets with changing the number of rpp on line 12 but it’s never worked for me. Also don’t forget add line 21.)

Method for displaying the latest tweet

<?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 = "";
// Suffix - some text you want display after your latest tweet. (Same rules as the prefix.)
$suffix = "";
$feed = "https://search.twitter.com/search.atom?q=from:" . $username . "&rpp=1";
function parse_feed($feed) {
   $stepOne = explode("", $feed);
   $stepTwo = explode("", $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);
?>


UPDATE
However, if you want to show more than one tweets, the method InstantShift presented is the best way! With this method we are even able to show tweets from multiple accounts! I modified their code to make links clickable and quote & apostrophe appearing as they should be.

Method for displaying more than one tweets from account(s)

<?php
$usernames = "YourTwitterUsername"; // Pull from accounts, separated by a space
$limit = "3"; // Number of tweets to pull in, total.
$show = 0; // Show username? 0 = No, 1 = Yes.
$prefix = "
    "; // This comes before the entire block of tweets. $prefix_sub = "
  • "; // This comes before each tweet on the feed. $wedge = "
    "; // This comes after the username but before the tweet content. $suffix_sub = "
  • "; // This comes after each tweet on the feed. $suffix = "
"; // This comes after the entire block of tweets. function parse_feed($usernames, $limit, $show, $prefix_sub, $wedge, $suffix_sub) { $usernames = str_replace(" ", "+OR+from%3A", $usernames); $feed = "https://search.twitter.com/search.atom?q=from%3A" . $usernames . "&rpp=" . $limit; $feed = file_get_contents($feed); $feed = str_replace("&", "&", $feed); $feed = str_replace("<", "", ">", $feed); $clean = explode("", $feed); $amount = count($clean) - 1; for ($i = 1; $i <= $amount; $i++) { $entry_close = explode("", $clean[$i]); $clean_content_1 = explode("", $entry_close[0]); $clean_content = explode("", $clean_content_1[1]); $clean_name_2 = explode("", $entry_close[0]); $clean_name_1 = explode("(", $clean_name_2[1]); $clean_name = explode(")", $clean_name_1[1]); $clean_uri_1 = explode("", $entry_close[0]); $clean_uri = explode("", $clean_uri_1[1]); // Make the links clickable and take care quote & apostrophe $clean_content[0] = str_replace("<", "", $clean_content[0]); $clean_content[0] = str_replace("&", "&", $clean_content[0]); $clean_content[0] = str_replace(""", """, $clean_content[0]); $clean_content[0] = str_replace("'", "'", $clean_content[0]); echo $prefix_sub; if ($show == 1) { echo "" . $wedge; } echo $clean_content[0]; echo $suffix_sub; } } echo $prefix; parse_feed($usernames, $limit, $show, $prefix_sub, $wedge, $suffix_sub); echo $suffix; ?>

Author: Takashi Irie

I'm a designer, father, husband, and house music lover living in the UK. I work at Automattic; the people who make WordPress, Jetpack, and a whole bunch of cool stuff.

11 thoughts on “How to show your latest tweets on your WordPress site without a plugin”

  1. Pingback: Resources About Wordpress Hacks And Customizations
  2. Thanks for the code from Instant Shift but I have a question. The output doesn’t know how to handle the ” in my tweets so for example a Tweet with I’m shows up on my website as I'm. Any idea how to fix this?

  3. Hi Harold,
    Thank you for pointing out. I found a better way which fixes quote and apostrophe so I update this post. With this method, we are even able to show tweets from multiple accounts!!

  4. Pingback: Wordpress Hacks And Customizations « MoeMir
  5. Pingback: Wordpress Hacks e Customizations! | sinergie.::.websolution | il blog
  6. Hi, great piece of code! Do you now any way to make it show old tweets as well? If you havent updated your twitter account for about 2 weeks, nothing shows..

  7. Pingback: sinergie.::.websolution » Blog Archive » Wordpress Hacks e Customizations!
  8. Thank you for pointing out. I found a better way which fixes quote and apostrophe so I update this post. With this method, we are even able to show tweets from multiple accounts!!

  9. Thank you very much this code !

    But i’m tottally beginner in php and i would like to add the attribute tagert=”_blank” to the links. I think it’s at line 45 but i don’t know the right syntax.

    ps : sorry for my english i’m french :p