So if you’ve been over to the “Stuff” page, you already know that I’ve started keeping up with the Tweeps that I’ve helped using a Google Earth map. I recently added a “tweetstream” of the people on the map. I thought of using a list to acquire the tweets, but I didn’t want to enter the usernames in 2 places, so I decided to instead parse the names out of the KML file. I used the native PHP extension simplexml to parse the KML file, and then used SimplePie to create a new RSS feed from all the users tweetstreams.
Updated: I have modified the script heavily, and added more comments in the code. Now I only retrieve one tweet from each user, then sort those tweets by date. The WordPress function I wrote now retrieves tweets until a certain number of characters is obtained, attempting to fill an area with tweets, regardless of individual tweet length.
Here is the code that creates the RSS feed:
<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:georss="http://www.georss.org/georss">
<channel>
<title>Twitter Helped KML</title>
<link>http://blog.perfectedperspectives.com/stuff</link>
<description>
Stream of tweets from tweeps I've helped with web development
</description>
<atom:link type="application/rss+xml" href="http://twitter.com/statuses/user_timeline/14388935.rss" rel="self"/>
<language>en-us</language>
<?php
//setup sortbydate
function sortbysub($a, $b) {
if($a['date'] > $b['date'])
return 1;
if($a['date'] < $b['date'])
return -1;
return 0;
}
//load existing KML file
$xml = simplexml_load_file('http://blog.perfectedperspectives.com/wp-content/uploads/TwitterHelped.kml');
//
foreach ($xml->Document->Folder as $folder) {
//parse each placemark for the TwitterUsername
foreach ($folder->Placemark as $placemark) {
$trimtweepurl = 'http://twitter.com/statuses/user_timeline/' . (string)trim($placemark->name, " @") . '.rss';
$tweepurls[] = $trimtweepurl;
}
}
//include SimplePie
include 'simplepie.inc';
//Setup error Checking
$firstfeed = 0;
//call SimplePie for each Tweeps RSS feed
foreach ($tweepurls as $simpleurl) {
//SimplePie parameters and setup
$feed = new SimplePie();
$feed->set_feed_url($simpleurl);
$feed->set_cache_duration (1000);
$feed->init();
$feed->handle_content_type();
//counter so that we only get the latest tweet from each tweep
$itemlimit = 0;
//retrieve the latest tweet from each feed
foreach ($feed->get_items() as $item) {
//count n tweet(s) from each user
if($itemlimit==1){break;}
//load tweet into array
$tweet['title'] = $item->get_title();
$tweet['link'] = $item->get_permalink();
$tweet['date'] = $item->get_date();
$tweet['guid'] = $item->get_id();
$tweet['description'] = $item->get_description();
//load each tweet as array element
$tweeptweets[] = $tweet;
//increment counter so we only get n tweets
$itemlimit = $itemlimit + 1;
//end foreach
}
//this if statement prevents the next from running on ALL RSS queries, this prevents having to wait for the entire script to run if the twitter ratelimit is exceeded from just the first RSS query
if($firstfeed > 0){break;}
if(in_array('<error>', $tweeptweets)){
//this is the error message displayed if Twitter denies our RSS query
?>
<item>
<title>Rate Limit Exceeded</title>
<link></link>
<pubDate></pubDate>
<guid></guid>
<description>Twitter HATES you!</description>
</item>
<?php
}
}
//here we sort the tweets by date, using the function declared earlier
usort($tweeptweets, 'sortbysub');
//flip into descending order
$tweeptweets = array_reverse($tweeptweets);
//create each item for the final rss feed
foreach ($tweeptweets as $tweeptweet) {
?>
<item>
<title><?php echo $tweeptweet['title']; ?></title>
<link><?php echo $tweeptweet['link']; ?></link>
<pubDate><?php echo $tweeptweet['date']; ?></pubDate>
<guid><?php echo $tweeptweet['guid']; ?></guid>
<description><?php echo $tweeptweet['description']; ?></description>
</item>
<?php }
//close the rss file
?>
</channel>
</rss>
I then created a shortcode in my functions.php file in my WordPress theme, using this code:
function display_helped_tweets() {
include_once(ABSPATH.WPINC.'/simplepie.inc');
$tweepstream = '<div id="tweepstream"><ul id="tweepstreamlist">';
$feed_url = 'http://tools.qtekso.com/pie/kmltwits.php';
$feed = new simplepie($feed_url);
foreach($feed->get_items() as $item) :
$tweetparts = explode(':', $item->get_title(), 2);
$tweepstream .= '<li class="tweepstreamtweet">';
$tweepstream .= '<a href="http://twitter.com/' . $tweetparts[0] . '" title="' . $item->get_title() . '">';
$tweepstream .= '@' . $tweetparts[0];
$tweepstream .= '</a>"' . $tweetparts[1] . '"</li>';
endforeach;
$tweepstream .= '</ul></div>';
return $tweepstream;
}
add_shortcode('tweepstream', 'display_helped_tweets')
WordPress Function to Style New or Updated Posts
Recently I’ve been doing a lot of WordPress theme customization work, and a client wanted to add those nifty “new” or “updated” icons to his post. I wrote this simple function to handle it.
Simply add this to your theme’s functions.php file, or if your theme doesn’t have a functions.php file, create one, and add the following also available as a zip: Add Style to New/Updates posts.
function pp_get_post_status() {global $post;
//begin config Vars
$old_days = 2; //set how many days a post is considered "new"
$up_days = 1; //set how many days a post is considered "updated"
$new_class = 'new'; //class you want applied for posts considered "new"
$up_class = 'updated'; //class you want applied for posts considered "updated"
$class_tag = ''; //set default class, if any
//end config
$cur_time = time(); //get current system time
$post_age = $cur_time - get_the_time('U', $post->ID); //determine posts' age
$up_age = $cur_time - get_the_modified_time('U', $post->ID); //determine last update age
if($post_age < ($old_days * 86400)) {$class_tag = ' ' . $new_class;} //if post is newer than n days, apply "new"
elseif($up_age < ($up_days * 86400)) {$class_tag = ' ' . $up_class;} //if post older than n days, but updated within n days, apply "updated
echo $class_tag;
}
You can call it from within the loop, here is an example where it’s adding a class to the div containing the post, you could use it in any CSS class declaration. You will notice that it automatically adds the space between any existing classes and the class it adds.
<div class="article<?php pp_get_post_status(); ?>">You could then use the CSS selector:
.article .new {style here}for new posts and:
.article .updated {style here}for updated posts.