Twitter
Search Engine Optimization

How to add your Twitter feed to your WordPress website

Twitter

Learn how to add a Twitter feed to your WordPress website without using a plugin. This page will teach you how to do this using the PHP fetch_feed function or updated parse_feed function.

We’ve created two different PHP snippets bellow that allow you to add the latest tweets from your Twitter feed into WordPress template files such as header.php and sidebar.php.

Version 1

Copy the snippet below and paste it into your theme’s template/theme files where you want your latest tweets to be displayed.

  • Change “Botcrawl” on line 3 to your Twitter username.
  • To change the number of displayed tweets change “4” on line 4.
<?php
include_once(ABSPATH . WPINC . '/feed.php');
$rss = fetch_feed('https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=botcrawl');
$maxitems = $rss->get_item_quantity(4);
$rss_items = $rss->get_items(0, $maxitems);
?>

<ul>
<?php if ($maxitems == 0) echo '<li>No items.</li>';
else
// Loop through each feed item and display each item as a hyperlink.
foreach ( $rss_items as $item ) : ?>
<li>
<a href='<?php echo $item->get_permalink(); ?>'>
<?php echo $item->get_title(); ?>
</a>
</li>
<?php endforeach; ?>
</ul>

Version 2 (Add text before and after tweet)

This version allows you to add text before and after the tweet. Copy the snippet below and paste it into your theme’s template/theme files where you want your latest tweets to be displayed.

  • Change “Botcrawl” on line 3 to your Twitter username.
<?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 = "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 = str_replace("&lt;", "<", $tweet);
$tweet = str_replace("&gt;", ">", $tweet);

return $tweet;

}

$twitterFeed = file_get_contents($feed);

echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);

?>

How to change the Twitter API caching limit

As a precaution, the first version above is set to cache your Twitter feed every 12 hours because the Twitter API has a limited number of calls per hour. Still, the caching limited on your Twitter account can easily be modified by using the PHP code below (set to 2 hours).

Copy and paste the snippet below into your current theme’s functions.php file

function filter_handler( $seconds )
{
// change the default feed cache recreation period to 2 hours
return 7200;
}
add_filter( ‘wp_feed_cache_transient_lifetime’ , ‘filter_handler’ );

Sean Doyle

Sean is a tech author and security researcher with more than 20 years of experience in cybersecurity, privacy, malware analysis, analytics, and online marketing. He focuses on clear reporting, deep technical investigation, and practical guidance that helps readers stay safe in a fast-moving digital landscape. His work continues to appear in respected publications, including articles written for Private Internet Access. Through Botcrawl and his ongoing cybersecurity coverage, Sean provides trusted insights on data breaches, malware threats, and online safety for individuals and businesses worldwide.
View all posts →

24 Comments

  1. Pulling Twitter RSS feed not working as expected (fatal error) – PhotoLens

    […] trying to get some of the latest posts from a Twitter account, without a plugin. According to Botcrawl.com’s tutorial, this should be […]

  2. Kiearn Calvert

    Just to give the heads up on this.Twitter now require you to use Oauth library and their Json api to get feeds So this code is no longer valid. 🙁

  3. sidharthpkSid

    Whoa, it worked for me. I had tried the same method on another blog but unfortunately I didn’t add my username in the correct place, hence was getting an error. But since you highlighted the portion where which the editing , it did the job pretty well.

  4. Tommy

    Version 2 doesn’t seem to work? I can get Version 1 to work fine but when I try version 2 nothing is displayed other than the h2 tag. Suggestion?

    1. Sean Doyle

      Thank you Tommy, I have updated it but have not tested it out yet. It should work fine though.

      If not please let me know.

  5. Brad

    Hey, thanks for this. I’ve implemented version 1 – is there a way to just make the twitter username clickable?

    Thanks

    Brad

  6. Andre

    Fatal error: Call to undefined method WP_Error::get_item_quantity() in …
    I use wordpress 3.5, can you help me

    1. Sean Doyle

      Version 1 seems to be fine, just tested it.

      The only reason I could think of such an issue would be if the code to include the blue color was copied and pasted on your site as well (span style=”color: #0000ff;”).

      If not, please copy and paste the code you used here so I can check it out.

  7. Francis Thibault

    I get a weird Warning: explode() [function.explode]: Empty delimiter

  8. trewknowledge

    In version 2, how can I get more then 1 tweet to show up?

    1. Sean Doyle

      After all of the Twitter API changes I have not been able to repair the limit amount on version 2.
      Before, you were able to change the numeric on in the URL but it does not seem to work: $feed = “http://search.twitter.com/search.atom?q=from:” . $username . “&rpp=*HERE*“;

      I will have to look further into this and/or create an additional post.

  9. Anonymous

    This didn’t work for me. When I pasted the code above (ver2) into my sidebar.php it just displays the raw php code in the sidebar.

    1. Sean Doyle

      Thank you, updated the code on the page… should work fine.

      1. Anonymous

        Thanks!! Your changes worked and I’m seeing the output instead of the raw code. Appreciate the code.

  10. Dennis

    THANK YOU!

    I looked for a lot of solutions and none of them seemed easy. This was as easy as it gets!

    THANK YOU!!!!

  11. desmoines

    Hi Sean–

    Is there a way to remove the twitter name so it only displays the tweet? Example: joesbar: The special today Clam Chowder.

    Is there a way that is only displays the special today…. and not joesbar?

    Thanks

    1. Sean Doyle

      Add your Twitter handle to this code. Change text before and after tweet to whatever you wish or remove them/leave blank if you don’t want anything.

      < ?php
      $username = "Twitter handle here";
      $prefix = "Text Before Tweet";
      $suffix = "Text After Tweet";
      $feed = "http://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);
      ?>
  12. Dustin van Zyl

    Hi,
    Thanks, this is a great hack which doesn’t degrade any other php even when using in main content areas (unlike most)
    Would it be possible to also fetch my twitter avatar.
    and would it be possible to style the fetched content?

  13. maffei

    Super basic question:

    I want to only display the text of the single, most current tweet, and not as a hyperlink. I’ve managed to get it to not be a hyperlink, but how do I get rid of the twitter handle prefix? Right now, the display is “• (twitter handle): (twitter message)”. Ideally, I’d eliminate the list bullet and the twitter handle. Thanks in advance.

    1. Sean Doyle

      The list bullets will show on themes where appropriate, they are generally added to display permalinks on a single line.

      I apologize but I do not have the correct line to remove the Twitter handle, but if I come across it I will let you know.

      All that would need to change is < ?php echo $item->get_title(); ?>

      get_title() grabs the (twitter handle): (twitter message) as you stated.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.