Limit WordPress Post Revisions for a Cleaner Database
Explanation
WordPress automatically saves different versions of your posts, called revisions, every time you make changes. This can be handy, but too many revisions can clutter your database. If you want to keep things tidy, you can limit the number of revisions WordPress saves.
The code snippet above helps you set a maximum number of revisions for each post. Here's how it works:
- Function: A function named wp_dudecom_limit_post_revisions is created to specify the maximum number of revisions you want to keep. In this example, it's set to 5.
- Filter Hook: The function is connected to WordPress using a filter called wp_revisions_to_keep. This tells WordPress to apply your limit whenever it saves revisions.
By using this code, you ensure that only the most recent 5 revisions of each post are kept, helping to keep your WordPress database lean and efficient.
Code
<?php
// Function to limit the number of post revisions in WordPress
function wp_dudecom_limit_post_revisions( $num, $post ) {
// Set the maximum number of revisions to keep
$max_revisions = 5;
// Return the maximum number of revisions
return $max_revisions;
}
// Hook the function to the 'wp_revisions_to_keep' filter
add_filter( 'wp_revisions_to_keep', 'wp_dudecom_limit_post_revisions', 10, 2 );
?>
Instructions
File Location: Add the following code to your theme's functions.php
file or a custom plugin file.
Prerequisites: No additional plugins or settings are required for this implementation.
Implementation Steps:
- Access WordPress Admin: Log in to your WordPress admin dashboard.
- Navigate to Theme Editor: Go to Appearance > Theme Editor. If you prefer using a custom plugin, navigate to Plugins > Editor and select your custom plugin file.
- Open functions.php: In the Theme Files section on the right, click on functions.php to open it in the editor.
- Insert Code: Scroll to the bottom of the
functions.php
file and paste the provided code snippet. - Save Changes: Click the Update File button to save your changes.
- Verify Implementation: Create or edit a post, make several changes, and check the revisions section to ensure only the latest 5 revisions are kept.
By following these steps, you will limit the number of post revisions to 5, helping to maintain a clean and efficient WordPress database.
If you need further assistance or more advanced functionality, consider reaching out to wp-dude.com for expert WordPress support.