WordPress.org

Make WordPress Core

Opened 3 months ago

Closed 3 months ago

Last modified 8 weeks ago

#48132 closed defect (bug) (reported-upstream)

Unsaved changes prompt triggered by "content_save_pre" filter

Reported by: elliotcondon Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Editor Keywords:
Focuses: Cc:
PR Number:

Description

Hi team,

There is a bug in WordPress 5.3-beta1 causing the "Unsaved changes promp" to appear when navigating away from an updated post.

The issue is triggered by any PHP code that modifies the "post_content" value outside of the editor, causing the block editor to incorrectly believe the user has unsaved changes. I believe this is due to some logic within the calculate the wp.data.select('core/editor').isEditedPostDirty() function.

There are many filters available to modify the "post_content" value such as "content_save_pre".

Adding a simple callback to perform a str_replace() will replicate the issue. A common example of this being used in the real-world would be a company name change.

<?php
add_filter('content_save_pre', function( $content ){
        return str_replace("Company Name 1", "Company Name 2", $content);
});

Steps to reproduce:

  1. Add the above code to functions.php
  2. Edit a post and include the text "Company Name 1" anywhere in the content
  3. Publish the post
  4. Navigate away from the post
  5. Notice the unload prompt

Attachments (1)

Screen Shot 2019-09-25 at 3.28.50 pm.png (146.1 KB) - added by elliotcondon 3 months ago.
Unload prompt

Download all attachments as: .zip

Change History (3)

#1 @youknowriad
3 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to reported-upstream
  • Status changed from new to closed

Thanks for the issue @elliotcondon This has been also reported upstream and a fix is being worked on.

https://github.com/WordPress/gutenberg/issues/17491

#2 @SergeyBiryukov
8 weeks ago

  • Component changed from General to Editor
Note: See TracTickets for help on using tickets.