I am using WordPress 2.7. WordPress has an annoying function: revisions. If you use WordPress 2.6 or above, there may be lots of unuseful revisions in your database. Normally, a post have 5 or more revisions. They make the database larger and larger. Since there are lots of rubbish, the efficiency may become very slow.
Many people want to disable the “revision” function, but WordPress doesn’t provide any option to do it. I see there are some plugins that can turn off the revision function. However, they doesn’t work well. For example, although you have activated the plugin, when you edit a post, autosave can still produce “revisions”. To solve this problem, you can use “disable autosave” plugin to turn off autosave. However, I think autosave is a good feature. I want to find a way to solve the problem: disable revisions but not for autosave. Fortunately, WordPress is an open-source application, so I can know the mechanism of it.
By checking the source code, I found that there can’t be any plugin to satisfy my requirement. So I have to hack WordPress. At last, I found a simple way, only 2 lines need to be changed! Let’s have a look.
Open wp-includes\default-filters.php and find the line
add_action( 'pre_post_update', 'wp_save_post_revision' );
It means the function ‘wp_save_post_revision’ is hooked ‘pre_post’update’. i.e. when you update a post, WordPress will call ‘wp_save_post_revision’ function and it will save a revision for the current post. Of cource, the revision isn’t useful. So I need to remove or comment it:
// add_action( 'pre_post_update', 'wp_save_post_revision' );Now when you update a post, it doesn’t produce revisions any more. However, when you edit a published post in WordPress, the autosave function can still generate a revision draft. It is not what I want, so I must to solve the problem.
Open wp-admin\includes\post.php and found the function wp_create_post_autosave (near line 1030). The last 2 lines should be
// Otherwise create the new autosave as a special post revision return _wp_put_post_revision( $_POST, true );
Through the comment I see it create a new revision from the $_POST array. I changed it to “return edit_post();” and tested in diffent conditions. I found it works well. Now the function wp_create_post_autosave looks like
function wp_create_post_autosave( $post_id ) { $translated = _wp_translate_postdata( true ); if ( is_wp_error( $translated ) ) return $translated; // Only store one autosave. If there is already an autosave, overwrite it. if ( $old_autosave = wp_get_post_autosave( $post_id ) ) { $new_autosave = _wp_post_revision_fields( $_POST, true ); $new_autosave['ID'] = $old_autosave->ID; return wp_update_post( $new_autosave ); } // Otherwise create the new autosave as a special post revision // return _wp_put_post_revision( $_POST, true ); return edit_post(); }
By now, WordPress doesn’t save the annoying revisions and autosave works well. I think the only disadvantage is that it hacks WordPress core code. Once you upgrade WordPress, you have to change the 2 lines.
Tags: WordPress
Thanks For Your solution.
Hi! just came here because I googled for the line of code I always forget… *lol*
actually there is a simpler way without touching core files.
Just add this to wp-config.php:
define(’WP_POST_REVISIONS’, false);
Revisions crap gone, and no hazzle when updating.
???????, ????? ?????? ???? ????? ????? ?????????
Hey There, I have to say, that I lover your blog.
Please go on like that and don’t stop posting. I hope this comment motivates you to do so, smile
regards, kali
A very easy to read explanation well done!
I keep listening to the news speak about getting free online grant applications so I have been looking around for the best site to get one.
Hi, I followed your instructions to disable the multiple autosaves for a friend, now that friend reports that text is not visible in the “Visual” tab when editing existing posts. It is visible in the “HTML” tab. Any ideas or suggestions?
I don’t know the reason, while my wordpress works well in visual mode.