Make WordPress Core

Opened 6 weeks ago

Last modified 5 weeks ago

#65024 new defect (bug)

Not possible to modify the "Draft saved" message

Reported by: stevejburge's profile stevejburge Owned by:
Milestone: 7.1 Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: has-patch
Focuses: administration Cc:

Description

It's currently not possible to modify the "Draft saved" message that appears when saving a post in Draft status.

https://imgur.com/a/XC3j2Bk

As far as we can see, that string is not exposed to any plugin-accessible filter.

https://build.trac.wordpress.org/export/61376/trunk/wp-includes/js/dist/editor.js

Attachments (1)

573240234-5231abdb-3b5f-47cd-aa3e-f53e3cf1cbe2.png (238.0 KB) - added by stevejburge 6 weeks ago.
Saving in draft status

Download all attachments as: .zip

Change History (6)

#1 @stevejburge
6 weeks ago

Possible solution here: https://github.com/WordPress/gutenberg/issues/77014#issuecomment-4182426163

"This would require a Trac ticket for a new item_drafted label or a similar label. Then the resolution could be the same as https://github.com/WordPress/gutenberg/pull/55155"

#2 @Mamaduka
5 weeks ago

  • Component changed from Editor to Posts, Post Types
  • Focuses administration added
  • Keywords has-patch added
  • Milestone changed from Awaiting Review to 7.1

This is similar to #51387.

#3 @Mamaduka
5 weeks ago

  • Keywords needs-patch added; has-patch removed

This ticket was mentioned in PR #11477 on WordPress/wordpress-develop by @sheldorofazeroth.


5 weeks ago
#4

  • Keywords has-patch added; needs-patch removed

Core Trac Ticket: https://core.trac.wordpress.org/ticket/65024

Problem
The "Draft saved." message that appears in the block editor snackbar notification when saving a post in draft status cannot be modified by plugins or themes. Unlike other editor save messages (e.g. "Post published.", "Post updated.", "Post trashed."), there is no WordPress filter or hook that exposes this string.

Root Cause
All other save notification messages in the block editor are sourced from postType.labels — PHP-defined post type label objects that are exposed via the REST API and filterable using the post_type_labels_{post_type} hook. However, the "Draft saved." message was hardcoded directly in JavaScript (wp-includes/js/dist/editor.js) inside the getNotificationArgumentsForSaveSuccess() function, bypassing the label system entirely and making it impossible to override.

Solution
Added a new item_draft_saved label to the post type labels system, following the same pattern as existing labels like item_updated, item_published, and item_trashed:

class-wp-post-type.php: Added item_draft_saved to WP_Post_Type::get_default_labels() with a default value of 'Draft saved.' for both hierarchical and non-hierarchical post types.
post.php: Added a contextual 'Pattern draft saved.' label for the wp_block post type (which already defines its own full set of custom labels), and updated the get_post_type_labels() docblock to document the new label.
editor.js: Replaced the hardcoded ("Draft saved.") string with postType2.labels.item_draft_saved, so the message now comes from the PHP label system like all other save notices.
Since the REST API post types controller already exposes the full $post_type->labels object, the new label flows to the JavaScript side automatically with no REST API changes needed.

Plugins can now filter the message using the existing hook:

add_filter( 'post_type_labels_post', function( $labels ) {

$labels->item_draft_saved = 'Your custom message.';
return $labels;

} );

#5 @sheldorofazeroth
5 weeks ago

I've submitted a patch that handles the PHP side of this fix.

What the patch does:

Adds a new item_draft_saved label to WP_Post_Type::get_default_labels() in class-wp-post-type.php with a default value of 'Draft saved.' for both hierarchical and non-hierarchical post types.
Adds a contextual 'Pattern draft saved.' label for the wp_block post type in post.php, consistent with how it already defines its own item_published, item_updated, etc.
Updates the get_post_type_labels() docblock to document the new label.
Since the REST API post types controller already exposes the full $post_type->labels object, the new label flows to the JavaScript side automatically with no REST API changes needed.

What still needs to happen (Gutenberg side):

The wp-includes/js/dist/editor.js file is a compiled build artifact synced from the Gutenberg monorepo and should not be edited directly. The corresponding JS change — replacing the hardcoded ( 'Draft saved.' ) with postType2.labels.item_draft_saved inside getNotificationArgumentsForSaveSuccess() in packages/editor/src/store/utils/notice-builder.js — needs to be submitted as a separate PR to the Gutenberg repository.

Note: See TracTickets for help on using tickets.