On WordPress Bugs, Not Features

“It’s a feature, not a bug!”

It’s a classic line. A Microsoft line. About how bugs in software aren’t really bugs, they’re undocumented features.

WordPress 2.6 has a new feature called “Post Revisions.” Basically, every time you edit a post, you can “roll back” the post and undo changes, as users can with Wikipedia.

It sounds like a nifty idea.

In practical terms, it isn’t. It’s really more of a bug. A well-intentioned bug. But still a bug.

Why? Each “revision” is an entirely new row in the WordPress database. Edit a post to add another tag? That’s another row. Edit a post to fix a spelling mistake? That’s another row. Edit a post ten times, and you’ve added ten new rows to your WordPress data table.

And WordPress doesn’t let you turn it off.

Fortunately, WordPress guru Lester Chan found a way. An edit to the wp-config file turns off post revisions.

But, turning it off still leaves junk rows in the database.

Lester gives some SQL code to delete the rows.

I have a better idea.

A few years ago, code was posted in the WordPress support fora to turn off comments after so many days. The way it worked was that, whenever something was done to a post — from posting to editing to leaving a comment — this routine would run. If a post had open comments and it was more than 21 days old, the plugin would close the comments on the post.

It occurred to me that I could turn that code into something that went through the WordPress post table, looked for revision posts and turn them into draft posts. Then, the next time I wanted to write a post, rather than start up a new post, I could “recycle” one of these draft posts — and save a line in the database table.

I looked at the code. I studied my WordPress database tables. I thought. I wrote some code. I crossed my fingers. And she flew. 🙂


<?php
/*
Plugin Name: Revision To Draft
Version: 1.0
Plugin URI: http://www.allyngibson.com/?p=1973
Description: Change post Revisions to Drafts
Author: Allyn Gibson
Author URI: http://www.allyngibson.com/
*/

function revision_to_draft() {
global $wpdb, $tableposts;

if (!isset($tableposts))
$tableposts = $wpdb->posts;

$wpdb->query("
UPDATE $tableposts
SET post_type = 'post', post_status = 'draft', post_content = ''
WHERE (post_type = 'revision')
");

$wpdb->query("
UPDATE $tableposts
SET post_date = '0000-00-00 00:00:00', post_date_gmt = '0000-00-00 00:00:00'
WHERE (post_status = 'draft')
");
}

add_action('publish_post', 'revision_to_draft', 7);
add_action('edit_post', 'revision_to_draft', 7);
add_action('delete_post', 'revision_to_draft', 7);
add_action('comment_post', 'revision_to_draft', 7);
add_action('trackback_post', 'revision_to_draft', 7);
add_action('pingback_post', 'revision_to_draft', 7);
add_action('edit_comment', 'revision_to_draft', 7);
add_action('delete_comment', 'revision_to_draft', 7);
add_action('template_save', 'revision_to_draft', 7);
?>

You need only to upload it, activate it, post a test comment, and then deactivate the plugin. (There’s no reason to leave it active, as once you’ve changed all the revisions over to drafts, you don’t need to do it again, and everytime you do do something, the plugin would run, slowing down your server for no good reason.)

If you’re on WordPress 2.6 and you don’t want to gunk up your database table with useless rows, follow Lester’s instructions to turn off post revisions, and run the plugin to change your revisions into drafts.

It’s that simple. 😉

3 thoughts on “On WordPress Bugs, Not Features

  1. I don’t understand why you would do this.. Why not just delete the revision row? Is it so the auto-increment in the wp-posts table doesn’t reach really high numbers?!

    When you mentioned the comments plugin, what I *thought* you were going to come up with was a plugin that finds revisions older than the most recent 5 let’s say, and deletes them. I’d prefer a plugin like that.

    Although I understand your problems with post revisions, I currently need switched on. The reason is I have multiple users editing the same posts, and I need to keep track of who changed what.

    Therefore, my problem with post revisions is the fact that they appear to record the post editor incorrectly. Revisions are attributed to the post author, not the post editor. I wonder if any of your readership have noticed this bug?

    Best wishes. Andy

  2. I guess, Andy, it depends on different users and their unique needs.

    For my blog, it’s a single author blog. I don’t even use the author tag in my templates; there’s no need for it. (Also, I have an aversion to it, based on a bug in the Tarski theme when I used it which caused someone clicking the author link to pull up every post in the blog onto a single page. Weird code under the hood, definitely.)

    For me, recycling a post revision by changing it into a draft and then editing that isn’t a big deal, as I’ve begun posts, saved them, and then never get around to ever finishing them, and then I’ll spend a week where I take these “orphaned” posts and recycle them in some way into new and useful content. Maybe it’s news that’s no longer timely, maybe I came up with a better way of addressing my thought at a later time, maybe it’s something else entirely. So, dumping a hundred new drafts into my recycle queue isn’t a big deal; I’ll end up using them all eventually. 🙂

    For me, post revisions are a useless feature. If anyone’s editing the posts, it’s me, and if I’m editing the posts, it’s either to fix a URL, add another tag or two, or fix a monster of a typo. Doing massive reversions a la Wikipedia isn’t something that I either need or desire. It doesn’t fit my posting style, and it doesn’t fit the needs of my blog.

    I wish I knew enough PHP to code the sort of plugin you’re talking about. There has to be someone with the PHP skills to make that happen, though, and what you’ve described does sound useful.

    Good luck. 🙂

  3. I think I was a little hasty before – Your explanation is a good one!

    Some day I’ll get round to writing the plugin I described. When that day finally comes, I’ll post you a link!

    Cheers. Andy

Leave a Reply

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