WordPress has started warning me "Leave Site? Changes you made may not be saved" when no changes have been made

Viewed 1028

In the WordPress theme my team has developed, we've recently run into an issue where no matter what, when trying to exit a Post/Page/Custom-Post-Type, WordPress will throw up the warning, "Leave Site? Changes you made may not be saved." Even if you click update and then immediately try to exit the page, it will throw that warning.

Leave Site Image

I cannot figure out what is causing this. This behavior started when I pushed out a major update a couple of days ago. I tried completely removing that update, reverting all the changes via git, and the behavior persists. I even switch to a branch of the theme that hasn't been worked on in months, and the behavior persists.

I've tried Chrome, Firefox, Edge, and the behavior persists. I've had coworkers try it on their computers s, and the behavior persists.

It's driving me crazy! Does anyone have any advice even where to start looking? Is it just a recent WordPress issue and nothing to do with our Theme?

2 Answers

This issue is caused by a bug that was introduced in version 5.6.1 of WordPress.

As a workaround, you can temporarily downgrade WordPress from 5.6.1 to 5.6. One way to do that is by using the WP Downgrade plugin.

Alternatively, you can just ignore the issue for a few days/weeks until they patch it.

Adding this to functions.php worked for me:

// WordPress 5.6.1: Window Unload Error Final Fix
add_action('admin_print_footer_scripts', 'wp_561_window_unload_error_final_fix');
function wp_561_window_unload_error_final_fix(){
    ?>
    <script>
        jQuery(document).ready(function($){

            // Check screen
            if(typeof window.wp.autosave === 'undefined')
                return;

            // Data Hack
            var initialCompareData = {
                post_title: $( '#title' ).val() || '',
                content: $( '#content' ).val() || '',
                excerpt: $( '#excerpt' ).val() || ''
            };

            var initialCompareString = window.wp.autosave.getCompareString(initialCompareData);

            // Fixed postChanged()
            window.wp.autosave.server.postChanged = function(){

                var changed = false;

                // If there are TinyMCE instances, loop through them.
                if ( window.tinymce ) {
                    window.tinymce.each( [ 'content', 'excerpt' ], function( field ) {
                        var editor = window.tinymce.get( field );

                        if ( ( editor && editor.isDirty() ) || ( $( '#' + field ).val() || '' ) !== initialCompareData[ field ] ) {
                            changed = true;
                            return false;
                        }

                    } );

                    if ( ( $( '#title' ).val() || '' ) !== initialCompareData.post_title ) {
                        changed = true;
                    }

                    return changed;
                }

                return window.wp.autosave.getCompareString() !== initialCompareString;

            }
        });
    </script>
    <?php
}

Credit to Konrad Chmielewski

More info on the issue here

Related