Last updated on September 25, 2023

Disable Auto-Saving of Posts and Pages

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Streamline editing; disable auto-save.

To disable the auto-saving feature in WordPress, you can use the wp_autosave_interval filter hook. By setting the interval to a high value or to zero, you can effectively disable the auto-saving of posts and pages.

Here’s an example code snippet that disables the auto-saving feature:

function wpsnippets_disable_autosave() {
    return 0; // Set the auto-save interval to zero
}
add_filter( 'wp_autosave_interval', 'wpsnippets_disable_autosave' );

This code snippet defines a custom function wpsnippets_disable_autosave() that returns a value of zero. The wp_autosave_interval filter hook is then used to apply this function and disable the auto-saving feature.

You can add this code to your theme’s functions.php file or in a custom plugin file. Once added, the auto-saving of posts and pages will be disabled.

This code snippet can be useful in situations where you want to prevent auto-saving of posts and pages to reduce unnecessary database queries or to enforce a different saving mechanism.

Examples

Example 1: Disabling auto-saving for all posts and pages

This code example disables the auto-saving feature for all posts and pages in WordPress. It removes the action hook wp_autosave using the remove_action() function.

function wpsnippets_disable_autosave() {
    remove_action( 'wp_autosave', 'wp_autosave_post_revision' );
}
add_action( 'admin_init', 'wpsnippets_disable_autosave' );

Example 2: Disabling auto-saving for specific post types

This code example disables the auto-saving feature for specific post types, such as ‘portfolio’ and ‘testimonial’. It checks the post type using the get_post_type() function and removes the action hook only for those post types.

function wpsnippets_disable_autosave_specific_post_types() {
    $post_type = get_post_type();
    if ( in_array( $post_type, array( 'portfolio', 'testimonial' ) ) ) {
        remove_action( 'wp_autosave', 'wp_autosave_post_revision' );
    }
}
add_action( 'admin_init', 'wpsnippets_disable_autosave_specific_post_types' );

Example 3: Disabling auto-saving for a specific user role

This code example disables the auto-saving feature for a specific user role, such as ‘editor’. It checks the user role using the current_user_can() function and removes the action hook only for users with the ‘editor’ role.

function wpsnippets_disable_autosave_specific_user_role() {
    if ( current_user_can( 'editor' ) ) {
        remove_action( 'wp_autosave', 'wp_autosave_post_revision' );
    }
}
add_action( 'admin_init', 'wpsnippets_disable_autosave_specific_user_role' );

In all three examples, the remove_action() function is used to remove the wp_autosave_post_revision action hook from the wp_autosave event. This effectively disables the auto-saving feature for the specified scenarios.

Last updated on September 25, 2023. Originally posted on October 8, 2023.

Leave a Reply

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