admin管理员组

文章数量:1312635

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?

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?

Share Improve this question edited Feb 2, 2016 at 15:03 kaiser 50.9k27 gold badges150 silver badges245 bronze badges asked Feb 2, 2016 at 14:12 T.ToduaT.Todua 5,8609 gold badges52 silver badges79 bronze badges 4
  • Have you already checked when and where the constants get checked for the first time? Any research? Also, please mind your markup. Thanks. Btw, I fixed your title to fit your spare contents. – kaiser Commented Feb 2, 2016 at 15:01
  • Did any of the answers worked for you? If so, it would be nice to accept since doing so builds trust to others facing the same issues. – marikamitsos Commented Sep 11, 2016 at 11:51
  • @marikamitsos I have posted it as answer. However, finally I've managed not to disable what WP thinks is reccomended. – T.Todua Commented Sep 12, 2016 at 14:02
  • In that case, you can always accept your own answer. Or even post how you've "...managed not to disable what WP thinks is recommended" – marikamitsos Commented Sep 14, 2016 at 14:12
Add a comment  | 

4 Answers 4

Reset to default 1

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');

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.

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;
}

本文标签: autosaveDisable autosave and post revisions from inside a theme or plugin