As Checkmate approaches 300 downloads I started thinking about updates. Checkmate has already received 4 bug fixes and I would like to be able to do a major update in the future. But I also thought about those people that had not subscribed to the thread or the comments. I’m not sure if these people check back often for updates but I would say most people don’t.
So I thought about having themes “call home” and check for updates periodically and inform the user, much like how WordPress 2.5 informs users of updates. But for a free theme this seemed a bit invasive to me. A simpler and effective way I thought up is to pull a RSS feed into an admin page where users could check for update notices. I know you can do this manually on the dashboard, but most theme users won’t do that.
How to do it
First we want to open up the functions.php from your theme directory(or create it) and create a function called mytheme_updates. This function is going to hold the code that pulls in an external RSS feed and displays it.
<?php
function mytheme_updates() { ?>
<div class="wrap">
<table>
<h2>Update Alerts</h2>
<tr>
<td>
<?php
//get MagpieRSS
require_once(ABSPATH . WPINC . '/rss.php');
//Feed where updates are located
$rss = fetch_rss('http://feeds.feedburner.com/curtishenson');
if ($rss) {
// Split the array to show first 3
$items = array_slice($rss->items, 0, 3);
//loop through the latest rss items
foreach( $items as $item ) {
$pubdate=substr($item['pubdate'], 0, 16);
echo '<p>
<a href="'.$item['link'].'" title="'.$item['title'].'"><h3>'.$item['title'].'</h3></a>
<em>'.$pubdate.'</em><br />'
.$item['summary'].' summary
<a href="'.$item['link'].'">more...</a>
</p>'
;
}
}
else {
echo '<p>No updates yet</p>';
}
?>
</td>
</table>
</div>
<?php
}
?>
Now a lot just happened so let me explain whats going on. We start out with some basic html to make everything look nice in the admin area. We then call MagpieRSS and enter the feeds location. Magpie is an RSS parser and is used by WordPress so you don’t need to install anything separately.
<?php
//get MagpieRSS
require_once(ABSPATH . WPINC . '/rss.php');
//Feed where updates are located
$rss = fetch_rss('http://feeds.feedburner.com/curtishenson');
We then check if there are any feeds, limit the number of responses to three, and loop through them. I printed out the title with a link to the post, the date and the summary.
<?php
if ($rss) {
// Split the array to show first 3
$items = array_slice($rss->items, 0, 3);
//loop through the latest rss items
foreach( $items as $item ) {
$pubdate=substr($item['pubdate'], 0, 16);
echo '<p>
<a href="'.$item['link'].'" title="'.$item['title'].'"><h3>'.$item['title'].'</h3></a>
<em>'.$pubdate.'</em><br />'
.$item['summary'].' summary
<a href="'.$item['link'].'">more...</a>
</p>'
;
}
}
The else statement is called when there is nothing in the RSS feed.
Next
We need to make another function now which I’ll call mytheme_add_updates. This function will contain what we need to add the page into the admin section of WordPress. This function calls a WordPress function named add_management_page which will put my updates page under Manage in the admin section.(I could put it elsewhere also)
<?php
function mytheme_add_updates() {
add_management_page("Theme Updates", "Update Alerts", 8, basename(__FILE__), 'mytheme_updates');
}
?>
We’re almost done! We just need to use add_action to invoke the mytheme_add_updates function.
<?php
add_action('admin_menu', 'mytheme_add_updates');
?>
Now we will have a tab under Manage which says Update Alerts
that contains the three latest posts from whatever RSS feed you specify. Theres plenty of ways you could use this: point the feed to a post’s comments, a feed from a discussion thread, a hidden category on your site, etc.
The Whole Thing
<?php
//////////
function mytheme_add_updates() {
add_management_page("Theme Updates", "Update Alerts", 8, basename(__FILE__), 'mytheme_updates');
}
function mytheme_updates() { ?>
<div class="wrap">
<table>
<h2>Update Alerts</h2>
<tr>
<td>
<?php
//get MagpieRSS
require_once(ABSPATH . WPINC . '/rss.php');
//Feed where updates are located
$rss = fetch_rss('http://feeds.feedburner.com/curtishenson');
if ($rss) {
// Split the array to show first 3
$items = array_slice($rss->items, 0, 3);
//loop through the latest rss items
foreach( $items as $item ) {
$pubdate=substr($item['pubdate'], 0, 16);
echo '<p>
<a href="'.$item['link'].'" title="'.$item['title'].'"><h3>'.$item['title'].'</h3></a>
<em>'.$pubdate.'</em><br />'
.$item['summary'].' summary
<a href="'.$item['link'].'">more...</a>
</p>'
;
}
}
else {
echo '<p>No updates yet</p>';
}
?>
</td>
</table>
</div>
<?php
}
//////
add_action('admin_menu', 'mytheme_add_updates');
?>

Taking it further
There are many ways to improve this and expand this, and I plan on doing so. I still would like to have some sort of notification of when there are updates to the feed. If you have any improvements let me know!
2 Comments
Leave a CommentI know this is an older post but interesting topic, I’m just wondering why would it be invasive to have a theme check for updates? wouldn’t it work the same way an addon checks for an updates?
31st Dec 2009
I think what I meant by invasive was the way I had tried to do this first which was to have the nag message like wordpress uses for it’s updates.
I really should look at this again now, I had forgotten all about it.
31st Dec 2009