How to disable auto-save and revision in WordPress?

 · 2 mins read

Sometimes the auto-save and revison function of WordPress really annoyed me, because they make a lot “trash” in database and result in incontinuous post ID.

I found a way to disable them, just write down before I forgot.

1.Edit the wp-config.php in root directory.

//disable autosave and revision
define( 'AUTOSAVE_INTERVAL', false );
define( 'WP_POST_REVISIONS', false );

2.Edit the functions.php in current theme.

//remove autosave in action
function disable_autosave() {
  wp_deregister_script('autosave');
}
add_action('wp_print_scripts','disable_autosave' );
remove_action('pre_post_update','wp_save_post_revision' );

3.Edit wp-admin/includes/post.php

LOGIC: if there is a autodraft in database, then use it otherwise create a new one

// before
if ( $create_in_db ) {
  $post_id = wp_insert_post( array( 'post_title' => __( 'Auto Draft' ), 'post_type' => $post_type, 'post_status' => 'auto-draft' ) );
  $post = get_post( $post_id );
  if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) )
    set_post_format( $post, get_option( 'default_post_format' ) );
}

//=>after
if ( $create_in_db ) {
  global $wpdb;
  global $current_user;
  $post = $wpdb->get_row( "SELECT * FROM {$wpdb->posts} WHERE {$wpdb->posts}.post_status = 'auto-draft' AND {$wpdb->posts}.post_type = '$post_type' AND {$wpdb->posts}.post_author = {$current_user->ID} ORDER BY {$wpdb->posts}.ID ASC LIMIT 1" );  //get autodraft
  if ( !$post ) { // if no autodraft, create one
    $post_id = wp_insert_post( array( 'post_title' => __( 'Auto Draft' ), 'post_type' => $post_type, 'post_status' => 'auto-draft' ) );
    $post = get_post( $post_id );
  }
  if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) )
    set_post_format( $post, get_option( 'default_post_format' ) );
}

Further clean in database:

login your phpmyadmin, find you wordpress databse, go to posts table, then input this sql and execute:

DELETE FROM wp_posts where id in (
    SELECT id from (SELECT id FROM wp_posts where post_name like '%revision%') tmp
);

PS: tmp is just a temp table.

In MySQL, you cannot modify a table and select from the same table in a subquery. This applies to statements such as DELETE, INSERT, REPLACE, UPDATE, and (because subqueries can be used in the SET clause) LOAD DATA INFILE.

So this is WRONG, just remind

DELETE FROM wp_posts where id in (SELECT id FROM wp_posts where post_name like '%revision%');