Skip to content

Limit WordPress Revisions without a Plugin

wordpress logo

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 my WordPress sites.

Tutorial Objectives

  • Introduce the problem
  • Why is it a problem
  • Solution to limit page revisions
  • Conclusion

Difficulty

%

Easy w/ access

A personalized Google Ad




A personalized Google Ad




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 on one of the websites I was developing and noticed something intriguing. The plugin is called WP Optimize and one of the items is shown on its list of things to optimize are page revisions. It showed that the site I was working on had 230 saved revisions, which is crazy considering that the only page I even had so far was the homepage!

After thinking about it for a second I actually wasn’t surprised there were that many revisions. Whether you’re using Elementor, Beaver Builder, or Divi, making quick page updated is so easy you lose track of how often you actually hit that save button. I mean when it takes literally 4 seconds to open the front-end editor, update something, and save it, 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 metadata in a table called post_meta. The more revisions you save the larger this table gets. In my example where I only have a homepage but 230 revisions, this single table grew to 150 megabytes. Just for comparison this same table on a completed site with 40 pages and around 100 posts is only 24 megabytes, yeah a huge difference. MySQL database tables simply should not be that big and introduce a host of issues like taking longer to save updates or making deploying sites hard which I personally ran into when trying to update my URLs to the live domain, my search and replace kept hanging when it hit this specific table.

To view the size of the individual tables I use the Better Search and Replace plugin. Normally I use this to update URLs on sites but it has the added benefit of telling you how large each table is in the database.

WP-Optimize

Clean, Compress, Cache, created by David Anderson, Ruhani Rabin, Team Updraft

Better Search Replace

Create by Delicious Brains

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 );

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 though I set it to 12. I feel it’s a good balance between still having some revisions to work with and not going overboard.

If you don’t have access for your wp_config file there is just as simple a code that can be added to functions.php. I prefer the version that goes into the wp_config for several reasons. For instance the functions.php file is tied to your theme so if you change the theme and forgot to add the code back 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.

Conclusion

It’s really that simple but makes a world of difference if you are tasked with website or server maintenance like myself. I hope this small tip helps all you out!

Other Reviews and Guides

When I learn something I like to share it. Expect new articles all the time!