How To Limit WordPress Post Revisions
WordPress provides revision control system for title and content added via editor on posts and pages so that we can go back any time and see previous edits written over in the database. Our latest edits are published on the front-end and all previous edits are saved in database, since post/page was created, for help in case we did a mistake or unforeseeable event such as power outage, browser crashes, computer crash etc. and need the previous version of content back.
What are the benefits of limiting post revisions?
Certainly, this is very useful feature by WordPress but over time this can lead to unnecessary overhead in our WordPress database which means more records for nothing. Let’s have a look at primary benefits of limiting post revisions in our WordPress theme.
1- Optimize WordPress Database: To prevent WordPress from saving unnecessary data in database, this is a MUST DO step. It would make sure that your database holds only the data you need.
2- Optimize WordPress Theme Speed: When there would be to-the-point data in database then query execution time would also be less ofcourse and it results in an optimized theme speed and performance.
How do I create a PHP function to limit post revisions?
You can add this simple one line of code to your theme’s functions.php file to define your WordPress post revision function.
if (!defined('WP_POST_REVISIONS')) define('WP_POST_REVISIONS', 5);
if (!defined('WP_POST_REVISIONS')) define('WP_POST_REVISIONS', false);
This code checks if WP_POST_REVISIONS limit is already set in wp-config.php or not. If it hasn’t then pass a parameter to the function that would do this work. In the example above, we have applied a limit of “5” post revisions.