Disable auto-save and post revisions from inside a theme or plugin

Per default, the way to disable autosaving and post revisions, is to modify wp-config.php. Is there a way, to do that from within a plugin or a themes functions.php?

Topic autosave posts Wordpress

Category Web


For guys who use WordPress 5.0+ version with Gutenberg Editor, the below code snippet works to disable auto drafting/saving.

/**
 * Disables auto-saving feature for Gutenberg Editor (set interval by 3600s)
 */
add_filter( 'block_editor_settings', 'rsm0128_block_editor_settings', 10, 2 );
function rsm0128_block_editor_settings( $editor_settings, $post ) {
    $editor_settings['autosaveInterval'] = 3600;
    return $editor_settings;
}

i have found this code:

define('my_revisions_amount', 1);         // let keep only one revision
define('my_autosave_interval', 600);      // 600 minutes is enough

if (is_admin()){   
    add_filter( 'wp_revisions_to_keep', function(){
        return my_revisions_amount;
    } );
    add_filter( 'wp_print_scripts', function(){
        wp_localize_script( 'autosave', 'autosaveL10n', 
              array(
                   'autosaveInterval'=> my_autosave_interval,
                   'blog_id'         => get_current_blog_id(),
              ) 
        );  
    }, 11 );    
}

This is my way of disabling autosave. It can be improved:

jQuery(document).ajaxSend(function( event, jqxhr, settings) {
    if(settings && settings.data && settings.data.indexOf("wp_autosave") > 0) {
        jqxhr.abort();
    }
});

Just checks the ajax request before it's sent to the server, and then cancels it if it's data contains wp_autosave string.


You mentioned:

Is there a way, to do that from within a plugin or a themes functions.php?

You may want to try the following code. Just place it inside your theme's functions.php file. Using it, you do NOT have to alter the wp-config.php file.

define('WP_POST_REVISIONS', false);
function disable_autosave() {
    wp_deregister_script('autosave');
}
add_action('wp_print_scripts', 'disable_autosave');

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.