Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to display your latest tweets in your WordPress site without a plugin
// How to display your latest tweets in your WordPress site without a plugin
// Source:
include_once(ABSPATH . WPINC . '/feed.php');
$username = "EricRasch"; // Just insert the username of the Twitter account you want to display
$feed = "$username.rss"; // Changed the code from to actually use the username variable
$num = 2; // Set the number of Tweets you want to display
// this is a function which will convert text links to clickable links
function makeClickableLinks($text) {
$text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)','<a href="\\1">\\1</a>', $text);
$text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)','\\1<a href="http://\\2">\\2</a>', $text);
return $text;
$rss = fetch_feed($feed);
if (!is_wp_error( $rss ) ) :
$maxitems = $rss->get_item_quantity($num);
$rss_items = $rss->get_items(0, $maxitems);
<?php if ($maxitems == 0) echo '<li>No items.</li>';
foreach ( $rss_items as $item ) : ?>
$tweet = str_replace($username.':','',$item->get_title()); // replaces the username which is displayed on the feed
$tweet = makeClickableLinks($tweet); // converts text links to clickable links
$tweet = preg_replace('#@([\\d\\w]+)#', '<a href="$1">$0</a>', $tweet); // converts hashtags to clickable links
$tweet = preg_replace('/#([\\d\\w]+)/', '<a href="$1">$0</a>', $tweet); // converts @username to links
echo $tweet . " <small><a href='".$item->get_permalink()."'>" . human_time_diff($item->get_date('U'), current_time('timestamp')) . " ago</a></small>";
<?php endforeach; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment