If you don’t want to use a plugin to clean up your WordPress website and insist on custom coding it, here is how to remove post revisions in a single website or a multisite network.
Add the code below to your theme or plugin, and trigger it using an action or a form or an event.
Disclaimer: Do not run it immediately, and do not activate it in production.
/**
* Delete all revisions from all posts in a single WordPress installation or a network.
*/
// Give the script 5 minutes to run.
set_time_limit( 60 * 5 );
/**
* Get a list of all blogs in our network.
*/
$blog_list = get_blog_list_full( 0, 'all' );
// Delete revisions for each blog.
foreach ( $blog_list as $blog ) {
echo '<p>Deleting from ' . $blog['domain'] . ':</p>';
echo delete_revisions_for_blog( $blog['blog_id'] ) . ' revisions.';
flush();
}
echo '<p>Done.<p>';
/**
* Delete all revisions for posts in a blog.
*
* @param int $blog_id The ID of the blog to delete revisions from.
*
* @return int The number of revisions deleted.
*/
function delete_revisions_for_blog( $blog_id ) {
global $wpdb;
if ( is_multisite() ) {
switch_to_blog( $blog_id );
}
// Retrieve revision IDs that are not autosave revisions.
$revision_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type = 'revision' AND post_name NOT LIKE '%-autosave%'" );
// Delete each revision, including related meta and taxonomy terms.
foreach ( $revision_ids as $revision_id ) {
wp_delete_post_revision( (int) $revision_id );
}
if ( is_multisite() ) {
restore_current_blog();
}
// Return the number of revisions deleted.
return count( $revision_ids );
}
/**
* Get a list of blogs in a multisite network.
*
* @param int $start Offset for the list.
* @param string $num Number of blogs to retrieve. Use 'all' to get all blogs.
* @param string $deprecated Deprecated parameter (not used).
*
* @return array An array of blog details (blog_id and domain).
*/
function get_blog_list_full( $start = 0, $num = 10, $deprecated = '' ) {
global $wpdb, $blog_id;
// For single blogs
if ( ! is_multisite() ) {
return [
$blog_id => [
'blog_id' => $blog_id,
'domain' => preg_replace( '%https?://%i', '', get_bloginfo( 'url', 'raw' ) ),
]
];
}
// For multisite setups
$blogs = $wpdb->get_results( $wpdb->prepare( "SELECT blog_id, domain FROM $wpdb->blogs WHERE site_id = %d AND deleted = '0' ORDER BY registered ASC", $wpdb->siteid ), ARRAY_A );
$blog_list = [];
// Build an array of blog details.
foreach ( (array) $blogs as $details ) {
$blog_list[ $details['blog_id'] ] = $details;
}
// Ensure $blog_list is an array.
if ( ! is_array( $blog_list ) ) {
return [];
}
// Return blogs based on the specified number.
if ( (string) $num === 'all' ) {
return array_slice( $blog_list, $start );
} else {
return array_slice( $blog_list, $start, (int) $num );
}
}
To disable revisions in WordPress, you can define the WP_POST_REVISIONS
constant in your wp-config.php
file. To completely disable post revisions, you can set it to false
or 0
. Here’s how to do it:
- Open your
wp-config.php
file for editing. You can find it in the root directory of your WordPress installation. - Add the following line to your
wp-config.php
file, preferably just above the line that says/* That's all, stop editing! Happy publishing. */
:
define('WP_POST_REVISIONS', false);
- Save the
wp-config.php
file.
This will disable post revisions for all your posts and prevent WordPress from creating and storing revisions in the database.
Photo by Laura Rivera on Unsplash