How to Take Control of Page and Post Revisions in WordPress …

Control of Page and Post Revisions in WordPress

This article is part of a series created in partnership with SiteGround. Thank you for supporting the partners who make SitePoint possible.

WordPress provides a revisions system which records a full copy of every page and post when it’s saved. The advantage: you can revert to an earlier version of the document at any time and make comparisons. Or you could discover who’s to blame for spolling and errors grammatical.

By default, there is no limit to the number of revisions stored per page or post. (Note only a single auto-save is made per post per editor — the most recent auto-save overwrites the previous one.) Every revision requires a separate row in WordPress’s posts table and perhaps multiple entries in the postmeta and term_relationships tables. That’s rarely a problem for smaller sites but it could affect the performance and efficiency of larger installations. Tables eventually become filled with redundant data which will never be used.

Limiting Revisions

The number of revisions can be set in WordPress’s wp-config.php file. If you’re not aware of that file, I recommend you seek further guidance from a developer. Make a copy of wp-config.php before editing because the smallest error could break your WordPress site.

To disable revisions entirely, add the following line to wp-config.php:

define('WP_POST_REVISIONS', 0);

To limit revisions, change the number to a positive integer, e.g. for no more than ten revisions per page/post:

define('WP_POST_REVISIONS', 10);

To revert back to unlimited revisions, remove the line or change the value to -1:

define('WP_POST_REVISIONS', -1);

Revision Plugins

If file editing feels a little too hardcore, there are several plugins to control revisions. For example, WP Revisions Limit sets the revision limit in an identical way.

Programmatically Limiting Revisions

The wp_revisions_to_keep filter allows plugins or your theme’s functions.php file to control how many revisions are retained for a given post.

The filter’s function is passed two arguments:

  • the default number of revisions to keep
  • the WP_Post object of the current post

and must return the number of permitted revisions.

The following example sets a limit of five revisions for posts with the type “custom_post”:

add_filter( 'wp_revisions_to_keep', 'control_revisions', 10, 2 );

function control_revisions($num, $post) {

  if('custom_post' == $post-post_type) $num = 5;
  return $num;

}

You can also use the WordPress REST API to list, retrieve and delete revisions.

How to Remove Old Revisions

The methods above activate immediately so, ideally, you should set WP_POST_REVISIONS shortly after installing WordPress. However, the setting will not remove old revisions from your MySQL database. It is possible to clean the old data but please be aware of the danger. Before taking any action, remember to…

BACK UP YOUR DATABASE!

The easiest option is to use a plugin such as WP-Optimize which cleans your WordPress database by removing revisions as well as other optimizations. You can run it once or set a regular schedule.

Alternatively, you can live dangerously and run a SQL command to clean revisions. First, find your WordPress table prefix — it is specified in wp-config.php, e.g.

$table_prefix = 'wp_';

wp_ is the default. We’ll assume wp_ has been specified for the following code but change the references if necessary. To delete all revisions for all pages and posts, start a MySQL administration tool such as phpMyAdmin and run the following SQL command:

DELETE a,b,c
FROM wp_posts a
LEFT JOIN wp_term_relationships b ON ( a.ID = b.object_id )
LEFT JOIN wp_postmeta c ON ( a.ID = c.post_id )
LEFT JOIN wp_term_taxonomy d ON ( b.term_taxonomy_id = d.term_taxonomy_id )
WHERE a.post_type = 'revision'
AND d.taxonomy != 'link_category';

Thanks to Michael Ambrosio for providing this fix when my original code, ahem, broke some stuff!

All going well, you’ll have a sparkling clean database and WordPress will be noticeably faster. Alternatively, WordPress won’t start and you’ve lost a decade-worth of amazing posts. But you did back-up first, of course…

Article source: https://www.sitepoint.com/wordpress-post-revision-control/

Related Posts