How to Disable and Limit WordPress Post Revisions
There is nothing worse than a slow website!
WordPress powered websites often slow down overtime because of a number of reasons. An important reason is the number of post revisions that add to the clutter that bogs down the website.
How to Disable WordPress Post Revisions
- Disable WordPress Post Revision
- Revision Control for Posts
- Revision Control Cleanup Through SQL Query
- Options To Pass Value of Post Revisions
- An Alternative
Post revisions are a great WordPress feature, but not for users with limited database space, or for users who simply do not need the autosave feature for their website. For all these users, simply disabling the Post revisions feature in the WordPress is the best option.
In the following steps, I will demonstrate how you could easily turn off WordPress Post revisions. I will also discuss an alternate option for limiting limit post revisions.
First of all, create a backup copy of wp-config.php file (commonly located at the root of the WordPress website folder structure) to a safe location. In case of anything going wrong, this backup will ensure that your website returns to normal.
Disable WordPress Post Revision
Open wp-config.php in your code editor. I will add the following code snippet.
1 2 3 4 5 |
//disable WP Post Revisions define('AUTOSAVE_INTERVAL', 300); // seconds define('WP_POST_REVISIONS', false); |
Notice that in the first line, I have changed the default autosave interval.
Note: This code snippet must be inserted above ‘ABSPATH’. Otherwise, it won’t work.
Whenever you make a change in a post, WordPress automatically saves a new version. Even for a mid-sized blog, these versions increase the size of the database. WordPress has a built-in revisions control option that could be set in the wp-config.php file. You can even disable the revisions altogether!
Revision Control for Posts
You need to write custom code for controlling the number of revisions for individual posts. The code is simple and involves passing on two parameters to the filter, the number of revisions to retain, and wp_post object representing the post to be targeted:
1 2 3 4 5 6 7 |
add_filter( 'wp_revisions_to_keep', 'filter_function_name', 10, 2 ); function filter_function_name( $num, $post ) { return $num; } |
Revision Control Cleanup Through SQL Query
One problem with the above code is that it will not delete the past revisions that are already saved in the database. To delete all previous revisions, go to PHPMyAdmin and run the following SQL query.
1 |
DELETE FROM wp_posts WHERE post_type = "revision"; |
Options To Pass Value of Post Revisions
If you are setting up WordPress from the scratch, post revisions should be automatically enabled on the website, with a default setting of storing every post revision.
If you do not see revisions as an option in the post settings, they are, almost certainly, been switched off at the configuration level.So, as the first step in working out what’s going on, open up the wp-config.php file in the code editor and look for a line like the following:
1 |
define( 'WP_POST_REVISIONS', false ); |
When it comes to passing a value to WP_POST_REVISIONS, you have got three basic options:
- true or -1: This is the default option in WordPress and it stores every revision per post.
- false or 0: This removes revisions entirely and limits you to the most recent autosave per post.
- A number greater than zero: This limits your revision count to a specific number and automatically deletes older revisions.
Save the wp-config.php file and re-upload it to your server. And you are done! The revisions option next to the Publish button is no longer visible.
An Alternative
Alternatively, if you want to limit the number of revisions of a post, you can use a number. A common scenario is when the clients need WordPress revisions, while the developers wish to reduce the strain on the database. In such cases, the developers set a number of revisions to be retained. WordPress will automatically limit the number of saved post revisions to the selected number.
Check out the following code snippet:
1 2 3 |
define('AUTOSAVE_INTERVAL', 300); // seconds define('WP_POST_REVISIONS', 3); |
The above code will save only three WordPress revisions. This is important because WordPress Posts could often save hundreds of revisions or a single posts. To view a particular revision, click Browse.
Conclusion
In this tutorial, I have covered how to limit post revision by defining the code in the configuration file. The WordPress revisions system stores a record of each saved draft or published update. If you need help with post revisions control, do leave a comment below.
Create Faster WordPress Websites!
Free eBook on WordPress Performance right in your inbox.