WordPress.org

Ready to get started?Download WordPress

Forums

HOW-TO: Make your blogroll smarter... (6 posts)

  1. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    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:

    <?php
    require_once( dirname( dirname(__FILE__) ) . '/wp-config.php');
    require_once( ABSPATH . 'wp-includes/class-snoopy.php');
    require_once( ABSPATH . 'wp-includes/rss.php');
    
    if ( !get_option('use_linksupdate') )
    	wp_die(__('Feature disabled.'));
    
    $link_rsses = $wpdb->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. :)

  2. Chris_K
    Member
    Posted 7 years ago #

    Otto42, that's quite slick. Thanks for that!

  3. enthusemarc
    Member
    Posted 6 years ago #

    This looks very useful, and not too complicated, but...

    - what if you have never set up a cron job? I know what they are, and I've looked over the documentation, such as it is, for cron.php functions, but a step-by-step guide to setting up the cron job would be helpful. If there is such a thing hidden away, either in the codex or the support forums, I've not been able to find it.

  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    Cron jobs are external to WordPress... at least, they used to be. Some hosts support cron jobs via their hosting system. Some don't. Regardless, it's something you need to talk to your host about.

    In other words, if you have to ask how to do it, then it's currently a bit too advanced for you to be doing. ;-)

    I am trying to get some changes (like this one) into future versions of wordpress. I've made a few patches and submitted them, so hopefully some of them will get included in 2.3, making this more automatic and working by default.

  5. dubyah
    Member
    Posted 6 years ago #

    Any chance you can help me with this issue? Trying to spit out the links/blogroll list to include a button for that links xml/rss feed where appropriate? It seems to be the only thing that you can't do with get_list or its variants?

  6. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    This code for update-links.php works with 2.3, and supports some types of feeds that the previous one above did not work with. It also gives you some feedback on the update date/times when you hit it from a browser:

    <?php
    require_once( dirname( dirname(__FILE__) ) . '/wp-config.php');
    require_once( ABSPATH . 'wp-includes/class-snoopy.php');
    require_once( ABSPATH . 'wp-includes/rss.php');
    
    if ( !get_option('use_linksupdate') )
    	wp_die(__('Feature disabled.'));
    
    $link_rsses = $wpdb->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) $mod = $feed->channel["pubdate"];
    	if ($mod == null) $mod = $feed->items[0]["pubdate"];
    echo "$rssurl : $mod";
    	if ($mod != null) {
    		$modtime = strtotime($mod);
    		$wpdb->query("UPDATE $wpdb->links SET link_updated = FROM_UNIXTIME($modtime) WHERE link_rss = '$rssurl'");
    	}
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic