WPSurfer.com

How to Limit WordPress Post Revisions without a Plugin

Published on September 24, 2022 | Updated on May 26, 2024

WordPress post revisions are a feature that allows you to track changes made to your posts and pages over time.

WordPress revisions can be extremely useful for content creators and editors as it provides a detailed history of changes, enabling you to revert to previous versions if needed.

While post revisions are a powerful feature, they can accumulate over time and potentially bloat your database.

There are plugin that could prevent WordPress to limit the number of revisions it keeps but if you are like me, you probably don’t want to do that.

In this short WordPress tutorial, I will tell you how to limit post revisions without a plugin.


Add Filter in the WP-Config File

If you want to limit post revisions, you can do it by simply adding a line of code to your wp-config.php file

Here’s an example of how you can set the maximum number of revisions to 3:

define('WP_POST_REVISIONS', 3);

The wp-config.php file is usually located in the root directory of your WordPress installation and you can manually edit it by using a file explorer such as FileZilla or Cyberducks.


Mu-Plugin to Limit WordPress Revisions

I am not a coder, I just figure out how to create a mu-plugins that do exactly what I want and this is not exception.

This is my mu-plugin to limit the number of post revision in WordPress.

<?php
/**
 * Plugin Name: Post Revisions Control
 * Description: Limits the number of revisions for each post type.
 * Version: 1.0
 * Author: TicoLibre
 * Author URI: https://ticolibre.com
 */

// Define the maximum number of revisions allowed.
define( 'MAX_POST_REVISIONS', 5 );

// Hook into the 'wp_revisions_to_keep' filter.
add_filter( 'wp_revisions_to_keep', 'limit_post_revisions', 10, 2 );

/**
 * Limits the number of revisions for each post type.
 *
 * @param int    $num  The number of revisions to keep.
 * @param object $post The post object.
 * @return int The limited number of revisions to keep.
 */
function limit_post_revisions( $num, $post ) {
    // Get the post type of the current post.
    $post_type = get_post_type( $post );

    // Check if the post type supports revisions.
    if ( post_type_supports( $post_type, 'revisions' ) ) {
        // Limit the number of revisions to the defined maximum.
        return MAX_POST_REVISIONS;
    }

    return $num;
}

To make sure the mu-plugin really does what it says it does, I deleted all WordPress post revisions using Advanced Database cleaner, then I started making and saving changes to a single post and confirmed that

  • I am sure it prevents WordPress from loading more than 5 WordPress post revisions and that’s something you can see on your WordPress editor.
  • According to database, no more than 5 post revisions were stored on the site database.

In case you want to use this mu-plugin, just create a new PHP file on your mu-plugins folder and paste the code I provided above and forget about it.

If you think that five WordPress post revision are way to many you can limit to 2 or 3 by making a small change to the code

define( 'MAX_POST_REVISIONS', 2 );

Considerations When Limiting Post Revisions

Keep in mind that this mu-plugin won’t delete post revisions created and stored on your site database prior to the installation of this plugin.

So if you feel like using this mu-plugin, you should probably delete all post revisions using Advanced Database Cleaner and then enjoy the benefits of this mu-plugin


Manuel Campos

Manuel Campos

I am José Manuel. I am writing about things I know and things that I am learning about WordPress. I hope you find the content of this blog useful.

WP SURFER

home

about

privacy

contact

© 2024 WP SURFER • Made with Love in Costa Rica