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 distinguished tech author and entrepreneur with over 20 years of extensive experience in cybersecurity, privacy, malware, Google Analytics, online marketing, and various other tech domains. His expertise and contributions to the industry have been recognized in numerous esteemed publications. Sean is widely acclaimed for his sharp intellect and innovative insights, solidifying his reputation as a leading figure in the tech community. His work not only advances the field but also helps businesses and individuals navigate the complexities of the digital world.

More Reading

Post navigation

24 Comments

Leave a Reply

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