HOW-TO: Make WordPress Blogroll's smarter

Warning: Heavy geek content ahead. If you’re not interested in PHP code, you can safely skip this post.

One thing that not a lot of people know about is that WordPress is capable of checking your bookmarks/blogroll links for you and organizing them according to the time they were last updated.

The reasons people don’t know this are:
a) It’s not automatically setup and working, and
b) It rarely works in the first place.

If you look at all the ways you can display links, one option kinda stands out.. In the case of wp_list_bookmarks(), that option is “orderby=updated”.

What does this do? Well, the Blogroll/Bookmarks have a field that is supposed to store the last time that each link was updated. And there’s a script in the wp-admin directory designed to keep it up to date. The script is named “update-links.php” and it… well… doesn’t work.

You see, pingomatic.com is included by default as one of the sites that gets pinged on every new post. So this site probably knows when every blog was updated. More or less. The bright idea at the time was to make the update-links script ask pingomatic when all these links were updated. And that’s what it still does. The problem with this is that pingomatic… well… it kinda sucks. The updated-batch request in it’s API rarely works. In point of fact, I’ve never gotten it to work.

So “update-links.php”, sadly, sits there. Semi-dead code. Nobody uses the “orderby=updated” parameter, because it doesn’t work without knowing when things were actually updated.

But nowadays, we have something better.. We have RSS feeds. And with a minor bit of code, you can revive your update-links.php. How? Well, one other thing every Blogroll/Bookmark link includes is a special field just for RSS feeds. Scroll down when you add or edit a link to find that field. Most people probably don’t fill that field in. But now we can make it useful.

The concept is simple: For every bookmark that has an RSS feed filled in, we’ll get the feed, find the Last-Modified time on the feed, and update our bookmark’s updated time. Then “orderby=updated” will actually work, and we’ll be able to have our Blogrolls reorder themselves automatically, putting the ones with the latest updates at the top of the list. Neat!

Here’s how you do it. First, you need to replace update-links.php with this code:

get_col("SELECT link_rss FROM $wpdb->links");
if ( !$link_rsses )
	wp_die(__('No links'));

foreach ($link_rsses as $rssurl)
{
	$feed = fetch_rss($rssurl);
	$mod = $feed->last_modified;
	if ($mod != null) {
		$modtime = strtotime($mod);
		$wpdb->query("UPDATE $wpdb->links SET link_updated = FROM_UNIXTIME($modtime) WHERE link_rss = '$rssurl'");
	}
}
?>

It’s a lot simpler than the old update-links.php, really.

Now, you need to find some way to run it every once in a while. If you’ve ever set a cron job up, you already know how to do this. You just need to make a cron job that loads http://example.com/blog/wp-admin/update-links.php every once in a while. However often you want to update. I recommend no more than once every 6 hours though, you’re pulling RSS feeds here. No need for extremes.

And that’s it. Change your sidebar to use “orderby=updated” and you’ll find that it works.

Future versions of WordPress may or may not have this change, and may or may not have the need for an external cron job (still working on these).. But this works right now, it’s a minor thing to do, and it’s kinda fun to have anyway. 🙂

3 thoughts on “HOW-TO: Make WordPress Blogroll's smarter”

  1. Hi Otto,

    read your post in the WP forum related to a “SELECT cat_id, cat_name FROM” problem. Now, I am using an older WP template and really can’t figure what to change in order to get it working with WP 2.2. Can you tell me exactly WHERE I need to change WHAT in order not to get the following error again:

    WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ” at line 1]
    SELECT cat_id, cat_name FROM

    I highly appreciate your advise!

    Best regards,
    Dan

Leave a Reply

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

css.php