How to Add Recent Tweets to your WordPress Site

Adding your latest tweets from Twitter to your WordPress website can be acheived either through a plugin or through a small snippet of code. There are a number of great plugins available, such as WD Twitter Feed. I personally use the Genesis Latest Tweets plugin, because I run the Genesis Framework.

However, if you’re looking at showing your recent tweets in a more customized way, you need to add the code manually to your theme files.

WordPress makes it really easy by offering a built-in SimplePie integration.

PHP for showing recent tweets

<?php
include_once(ABSPATH . WPINC . '/rss.php');
$tweets = fetch_feed('https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=wpforce');
$maxtweets = $tweets->get_item_quantity(5);

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

This is the simple version of the code. It pulls in rss.php that comes with WordPress and this offers default 12-hour caching of the feed, that way you’re not hitting the feed on every pageload.

Conditional Loading of rss.php

Personally, I don’t like loading rss.php on every pageload if I really don’t have to. For some pages, you may not have a feed that you need to pull, so I like to do something like this.

<?php
$user = get_post_meta(get_the_ID(), 'twitter_user', true);
if ( $user ) {
include_once(ABSPATH . WPINC . '/rss.php');
$tweets = fetch_feed('https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=.'$user);
$maxtweets = $tweets->get_item_quantity(5);

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

By doing this, you can set the Twitter username on a post by post basis and only include rss.php if the value of $user is true.

You may not want the 12-hour cache time though, so it’s good thing that it’s easy to change.

function wpf_cache_time( $seconds )
{
  // change the default feed cache recreation period to 2 hours
  return 7200;
}

add_filter( 'wp_feed_cache_transient_lifetime' , 'wpf_cache_time' );

The value is based in seconds, so you could change it to shorter or longer, depending on your needs.

If you have any questions, please let me know in the comments.

Comments

  1. By on

    Remember the Twitter API version 1 has stopped so this code won’t work anymore.

    Reply »

    • By Jonathan Dingman on

      Ah thanks Paul, I haven’t been able to update the post, but I will soon.

      Reply »

Leave a Reply

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