Make WordPress Core

Opened 3 years ago

Closed 3 years ago

#52450 closed defect (bug) (duplicate)

Autosave prompts unsaved changed for custom post types

Reported by: dbtedg's profile dbtedg Owned by:
Milestone: Priority: normal
Severity: normal Version: 5.6.1
Component: Autosave Keywords:
Focuses: Cc:


Our sites uses a number of custom post types configured to exclude the 'editor' support on set up. Example:

        'supports' => array( 'title', 'page-attributes', 'comments', 'excerpt', 'thumbnail' ),

Since updating to 5.6.1, we have noticed that the WP Admin prompts for unsaved changes when exiting any editor page (although no changes have been made to the post content).

This can be confirmed by running wp.autosave.server.postChanged() from the browser console which returns true for any custom post type configured without editor support with no content changed.

This appears to be caused by setInitialCompare() (wp-includes/js/autosave.js [48]) which was added in 5.6.1.

This function checks if element #content value exists and if not sets an empty string:

initialCompareData = {
    post_title: $( '#title' ).val() || '',
    content: $( '#content' ).val() || '',
    excerpt: $( '#excerpt' ).val() || ''

During postChanged() (wp-includes/js/autosave.js [704]) there logic check for the difference between the current editor value and the initialComparedData():

if ( $( '#' + field ).val() !== initialCompareData[ field ] ) {

This compares $('#content').val() (undefined) to initialCompareData['content'] (empty string), which fails as undefined !== ''

Expected result

Editor should only prompt for unsaved changes when content has changed, regardless of the custom post type support for "editor".

A similar logic check should be added on $( '#' + field ).val() to return an empty string, or initialCompareData[ field ] should be set as undefined

Change History (1)

#1 @audrasjb
3 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Hi @dbtedg, welcome to WordPress Core Trac and thank you for opening this ticket,

This issue is already tracked in ticket #52440. Please feel free to add your thought there :)

By the way, I'd like to thank you for your research, that's super helpful.


Note: See TracTickets for help on using tickets.