Just Disable Autosave in the Block Editor

In our custom workflow plugin, every revision matters, so I needed to disable the autosave feature in WordPress.

You used to be able to just edit a line or two wp-config.php to effectively disable autosave:

define('AUTOSAVE_INTERVAL', 86400);

This technically set the autosave interval to one day (86,400 seconds). But since the release of the Block Editor, this wp-config trick hasn’t worked.

There is a plugin available called Disable Gutenberg Autosave. However, this plugin not only disables autosaves, it also adds a new toolbar button in the Block Editor so users can manually turn Autosave back on at whatever interval they prefer.

So, I set out to find a way to just disable autosave – and not provide any tools to turn it back on. After a long search, I pieced together a small snippet of code that succeeded.

Plugin: Just Disable Block Editor Autosave

In your /wp-content/plugins/ folder, create a new folder called autosave. Inside that folder, all you need is two files: autosave.php and autosave.js.

autosave.php is just long enough to tell WordPress this is a plugin, and enqueue one script in the Block Editor:

<?php
/* Plugin Name: Disable Block Editor Autosave */
function enqueue_disable_block_editor_autosave() {
    wp_enqueue_script( 'disable-block-editor-autosave', plugins_url( 'autosave.js', __FILE__ ), array( 'wp-edit-post', 'wp-blocks', 'wp-dom-ready' ), '1.0', true);
}
add_action( 'enqueue_block_editor_assets', 'enqueue_disable_block_editor_autosave');

The enqueue_block_editor_assets hook ensures this code will only run in wp-admin, not on the front end. The array of dependencies ensures that everything has loaded before our code runs – otherwise, Core will set the value and override what we’re trying to do.

The autosave.js file is even shorter:

wp.domReady( function() {
    wp.data.dispatch( "core/editor" ).updateEditorSettings({
        autosaveInterval: 99999
    });
});

Similar to the old wp-config trick, we are actually setting the autosave interval to a very high number, not technically disabling autosave altogether. But 99,999 seconds would be a very long time to leave the Editor open, so in effect we’ve achieved our goal.

Once you’ve uploaded these files, make sure you activate the plugin, and you can then hard-refresh the Editor (control + F5 on Windows) to ensure the new JS file loads. You can verify it is working by opening up the Console and typing in this command:

wp.data.select( "core/editor" ).getEditorSettings();

When you open up the object this returns, you will see that autosaveInterval is indeed set to 99999. You can leave the Editor open for however long the old value was – mine was at a default 300 seconds, which is 5 minutes – and run the same Console command again to verify nothing has been overwritten. If it has, or if it doesn’t work the first time, most likely you’re using another plugin that is conflicting – or WP Core has changed again, which has happened a lot more these past couple of years than ever before.

Leave a Reply

Your email address will not be published. Required fields are marked *