Intro
WordPress revisions are helpful when you need to retrieve an old copy of a page or post. While saved revisions are important to managing your WordPress site they can pose an interesting problem that I recently started experiencing when saving pages on WordPress sites I was developing.
Full Guide
Watch the overview video or keep reading the full article below
Table of Contents
The Problem
I noticed on several sites after saving the same page many times that process of saving was taking longer and longer to complete. It got to the point where the saving process just hung and never actually finished. I thought to myself that this can’t be a server issue because I have a beefy dedicated server with plenty of horsepower, RAM, and an optimized backend.
So I installed a database optimizing plugin called WP Optimizer on one of the websites I was developing and noticed something intriguing. Among the list of items, the plugin looks to optimize are page revisions. It showed that there were 477 saved revisions in the database, which needless to say sounded a bit high, considering this site only had 7 main pages including the homepage.
After thinking about it for a second thought I actually wasn’t surprised there were that many revisions. One reason for this is whether you’re using Elementor, Beaver Builder, Divi, or really any other advanced page builder, making quick page updates is so easy you lose track of how often you actually hit that save button. I mean when it takes literally 6 seconds to open the front-end editor, update something, and save it, it’s no wonder there are a tone of saved revisions.
Why is it a problem
So why is this a problem? Well to answer that we have to take a little trip into the bowls of a website, mainly the database and the fundamental tables that make up the WordPress core. Page revisions are saved with the page/post meta data in a table called post_meta. The more revisions you save the larger this table gets. The site I mentioned with 477 saved revisions, this single table grew to over 240 megabytes. Just for comparison this same table on a completed site with 20 pages and around 30 posts is only 24 megabytes, yeah a huge difference. MySQL database tables simply should not be that big because it introduces a host of issues. One of those issues is the one I introduced at the beginning, saving pages with a tone of revisions can taking longer to save after an update. Another issue comes when you are trying to deploy a dev site, which I personally ran into when trying to update my URLs to the live domain, my search and replace kept hanging when it hit that post_meta table.
To view the size of the individual tables I use either the WP Optimize plugin under the Tables tab or the Better Search and Replace plugin. Normally I use this to update URLs on sites but it has the added benefit of telling you the size of each table is in the database.
WP-Optimize
Clean, Compress, Cache, created by David Anderson, Ruhani Rabin, Team Updraft.
Better Search Replace
Easily and quickly search and replace almost anything in your WP database. Great for URL updates.
Solution for Limiting WordPress Revisions
One obvious solution is just to use the WP-Optimize plugin to flush out the revisions every once in a while. But this has a couple of drawbacks. Mainly it’s a manual process and when you have multiple people working on a site it can still get out of hand quickly, also you’ll be wiping out all of the revisions which someone may need. So we need a solution that’s automated and keeps some revisions available for users who may need them. Luckily there is an incredibly simple solution that takes only one line of code to implement.
Deep in the WordPress development documentation, there is a little piece of code that limits the numbers of revisions saved to a given amount. This code accomplishes exactly what I wanted.
Here it is:
define( 'WP_POST_REVISIONS', X );
Yep, that’s it. This goes into your wp_config file.
Simply swap out the X for the number of revisions you want to save or set it to 0 to save no revisions at all, which may be suitable for some. For me I set it to 12. I feel it’s a good balance between still having some revisions to work with without going overboard. One peculiar thing about the code is that it only seems to work when added near the top of the wp_config file. If you add it at the bottom it doesn’t work for whatever reason.
Another thing to take into account is that simply adding the code to an existing site will not remove old revisions. It will just show however many revisions you specified when editing a page or post. You’ll still need to run the WP Optimize operation to flush out all the saved revisions to decrease the size of the database.
If you don’t have access to your wp_config file there is just as simple a code that can be added to the functions.php file. I prefer for the former method for several reasons, but mainly since the functions.php file is tied to your theme if you change the theme and forgot to add the code you’ll go back to saving all revisions again.
Here is the alternate solution:
add_filter( 'wp_revisions_to_keep', 'page_revisionLimit', X, 2 ); function page_revisionLimit( $num, $post ) { return $num; }
Again just replace the X with the number of revisions you want to keep.
I’ve integrated the former piece of code onto my development template so it’s always there when I start a new website. If you want to implement this code on a site that’s already been around and has a tonne of existing saved revisions I found it best to run the optimizer to remove the current page revisions and then add the code. This just makes sure that the database has been cleaned out.