Autosave control in WordPress

Autosave:

Autosave is simply about saving your posts automatically in the background while you are editing. So it’s different from revisions and for every post there will be only one autosave per user. This is from the document:

There is only ever a maximum of one autosave per user for any given post. New autosaves overwrite old autosaves.

You can manipulate how soon the autosave will happen by defining AUTOSAVE_INTERVAL constant in wp-config.php file. For example, to autosave posts / pages you’re editing every 30 seconds:

define('AUTOSAVE_INTERVAL', 30 );

This is useful in case your net connection is lost or computer turns off accidentally before you could save the changes. With autosave interval set to 30 seconds, you’ll only lose maximum 30 seconds worth of edit.

Revisions:

Revision is about deciding how many changes to your post you want to keep. So if revision is turned on and you save a post 24 times, then WordPress will keep the 24th version as your original post and other 23 versions as revisions.

Note: WordPress doesn’t keep autosaved versions as revisions. Only the saves you’ve made intentionally (by clicking save or publish) are kept as revisions.

By default, WordPress keeps infinite number of revisions if revision is activated. So unless you specify, WordPress doesn’t delete any revision automatically. However, this behaviour can be altered using WP_POST_REVISIONS constant or wp_revisions_to_keep filter.

For example, to keep the last 22 revisions only, you may set the following in the wp-config.php file:

define( 'WP_POST_REVISIONS', 22 );

or using the following CODE in your theme’s functions.php file or in a custom plugin:

add_filter( 'wp_revisions_to_keep', 'wpse257846_num_revisions_to_keep', 10, 2 );
function wpse257846_num_revisions_to_keep( $num, $post ) {
    // you may use the $post variable to manipulate this number as needed
    if( 'my_custom_post' == $post->post_type ) {
        // for 'my_custom_post' post type, keep last 100 revisions
        // this is only about how many revisions to keep,
        // not about how old they are
        // to decide how old revision you want to keep,
        // you need to make more complex query about date here 
        return 100;
    }
    if( 'my_most_important_post' == $post->post_type ) {
        // returning -1 means WordPress will never delete revisions
        // for 'my_most_important_post' post type
        return -1;
    }
    // if for all other post types we want to keep just the last 22 revisions
    return 22;
}

Leave a Comment