-
-
Save jimmynotjim/2324885 to your computer and use it in GitHub Desktop.
How to display your latest tweets in your WordPress site without a plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// How to display your latest tweets in your WordPress site without a plugin | |
// Source: http://dinolatoga.com/2010/07/31/how-to-display-your-latest-tweets-in-your-wordpress-blog-without-a-plugin/ | |
include_once(ABSPATH . WPINC . '/feed.php'); | |
//configuration | |
$username = "EricRasch"; // Just insert the username of the Twitter account you want to display | |
$feed = "http://twitter.com/statuses/user_timeline/$username.rss"; // Changed the code from dinolatoga.com 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); | |
endif; | |
?> | |
<ul> | |
<?php if ($maxitems == 0) echo '<li>No items.</li>'; | |
else | |
foreach ( $rss_items as $item ) : ?> | |
<li> | |
<?php | |
$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="http://twitter.com/$1">$0</a>', $tweet); // converts hashtags to clickable links | |
$tweet = preg_replace('/#([\\d\\w]+)/', '<a href="http://twitter.com/search?q=%23$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>"; | |
?> | |
</li> | |
<?php endforeach; ?> | |
</ul> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment