Code Snippet to Enable Post Revision Management in WordPress

Jan S Jan 18, 2025 Post Revisions
How do I keep track of all the changes I make to my blog posts?
What code snippet can I use to enable or customize post revisions in WordPress to manage content history effectively?
Andy answered Jan 18, 2025

Post Revision Management in WordPress

Custom Code Solution

Add this code to your theme's functions.php file or in a site-specific plugin:

Enable or disable post revisions completely:

define('WP_POST_REVISIONS', true); // Set to false to disable, or number to limit

Set a specific number of revisions to keep per post:

define('WP_POST_REVISIONS', 5); // Keeps 5 revisions per post

Control revision cleanup by post type:

function custom_revision_settings($num, $post) {
    switch ($post->post_type) {
        case 'post':
            return 10; // Keep 10 revisions for posts
        case 'page':
            return 5;  // Keep 5 revisions for pages
        default:
            return 3;  // Keep 3 revisions for other post types
    }
}
add_filter('wp_revisions_to_keep', 'custom_revision_settings', 10, 2);

Add custom intervals for autosave:

function custom_autosave_interval($seconds) {
    return 120; // Set autosave to every 2 minutes (120 seconds)
}
add_filter('autosave_interval', 'custom_autosave_interval');

Database Cleanup

Remove all revisions from database:

function delete_all_revisions() {
    global $wpdb;
    $wpdb->query(
        "DELETE FROM $wpdb->posts WHERE post_type = 'revision'"
    );
}
// Hook to run on theme activation
add_action('after_switch_theme', 'delete_all_revisions');

Plugin Solutions

If you prefer a plugin solution, these options provide user-friendly interfaces:

  1. WP Revisions Control - Simple revision management
  2. Better Delete Revision - Advanced revision cleanup
  3. Simple Revision Control - Post-type specific controls

Important Notes

  • Post revisions are stored in the database, so limiting them can help manage database size
  • The default WordPress revision system keeps all revisions unless limited
  • Changes take effect for new posts/revisions only
  • Always backup your database before making changes to revision settings
  • Some hosting providers may limit revision storage

When using these solutions, choose the approach that best matches your needs:

  • Use custom code for precise control
  • Use plugins for user-friendly management
  • Consider database size impact when setting revision limits