WordPress.org

Make WordPress Core

#52450 closed defect (bug) (duplicate)

Autosave prompts unsaved changed for custom post types

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

Description

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

<?php
register_post_type(
    'custom_post_name',
    array(
        ...
        '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
10 months 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.

Cheers,
Jb

Note: See TracTickets for help on using tickets.