How to Disable Revisions in WordPress
A revision is a backup of a post at a specific time.
Revisions
If revisions are enabled for a post, WordPress creates a new revision for the post each time the post is updated. This feature is useful to track changes and to restore the post to a previous state.
Revisions are instances of the 'revision'
post type. They are stored in the table wp_posts. This means that the table wp_posts grows quickly when this feature is enabled. For performance reasons, I recommend disabling revisions if you are not using the benefits of this feature.
Method 1
Exclude the 'revisions'
value from the 'supports'
attribute when a post type is registered, and WordPress will not store revisions for instances of the post type. The following code registers the custom post type Book, which does not support revisions.
Learn here how to register a post type in WordPress.
/*
* Registers the custom post type Book.
*/
function ns_register_book_cpt(){
// See all possible labels in the PHPDoc of the function get_post_type_labels
$labels = array(
'name' => __('Books', 'ns'),
'singular_name' => __('Book', 'ns'),
'menu_name' => __('Books', 'ns'),
'all_items' => __('All Books', 'ns'),
'add_new' => __('Add New', 'ns'),
'add_new_item' => __('Add New Book', 'ns'),
'edit_item' => __('Edit Book', 'ns'),
'new_item' => __('New Book', 'ns'),
'view_item' => __('View Book', 'ns'),
'view_items' => __('View Books', 'ns'),
'search_items' => __('Search Books', 'ns'),
'not_found' => __('No books found.', 'ns'),
'not_found_in_trash' => __('No books found in Trash.', 'ns'),
'archives' => __('Book Archives', 'ns'),
'filter_items_list' => __('Filter books list', 'ns'),
'items_list_navigation' => __('Books list navigation', 'ns'),
'items_list' => __('Books list', 'ns')
);
// See all possible attributes in the PHPDoc of the function register_post_type
$args = array(
'label' => __('Books', 'ns'),
'labels' => $labels,
'public' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_nav_menus' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'hierarchical' => false,
'supports' => array('title', 'editor', 'author'),
'taxonomies' => array('post_tag'),
'has_archive' => true,
'rewrite' => array('slug' => 'books'),
'query_var' => 'book'
);
register_post_type('ns_book_cpt', $args);
}
add_action('init', 'ns_register_book_cpt', 10, 0);
Method 2
Use the 'wp_revisions_to_keep'
filter to control the number of revisions to keep for a post. The callback receives the current number of revisions to keep for the post, and an object of class WP_Post
that represents the post.
The callback should return the new number of revisions to keep for the post. To disable revisions, return zero. The code below disables revisions for the post types Post and Page.
/*
* Disables revisions for the post types Post and Page.
*/
function ns_disable_revisions($num, $post){
$target_types = array('post', 'page');
$is_target_type = in_array($post->post_type, $target_types);
return $is_target_type ? 0 : $num;
}
add_filter('wp_revisions_to_keep', 'ns_disable_revisions', 10, 2);
Method 3
Add the following code to the file wp-config.php and WordPress will disable revisions for all post types. Note that the 'wp_revisions_to_keep'
filter has precedence over this setting.
// Disable revisions for all post types
define('WP_POST_REVISIONS', 0);
User interface
If revisions are enabled for a post and there are revisions for the post, WordPress shows the current number of revisions in the Publish box of the Edit Post screen.
The best way to check that revisions are disabled for a post is by updating the post title or the post content several times and checking that a revision section does not appear in the Publish box.
Further reading
I recommend the other tutorials in this series to learn more about revisions in WordPress.
- How to Disable Revisions in WordPress
- How to Limit the Number of Revisions Kept for a Post in WordPress
Exercise
Register the custom post type Magazine. This post type should support revisions, but only the latest five revisions should be kept for every entry of the post type.
After registering the post type, create and publish a new magazine. Repeat the process outlined below at least seven times and verify that the number of revisions shown in the Publish box is never greater than five.
- Change the post title or the post content
- Click the Update button
Source code
The source code developed in this tutorial, including a possible answer to the exercise, is available here.
Comments