WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.43k stars 4.17k forks source link

admin_notices are not displayed in Gutenberg #44976

Closed collimarco closed 1 year ago

collimarco commented 1 year ago

Description

This code works properly on any admin page, but not in Gutenberg:

function some_notices() {
  echo '<div class="notice notice-success is-dismissible"><p>Example notice.</p></div>';
}
add_action( 'admin_notices', 'some_notices' );

The notices are not displayed.

Step-by-step reproduction instructions

Here's a more complete example (when a new post is published some notifications are sent, and a notice should be displayed to the admin):

  1. generate some notices: https://github.com/pushpad/pushpad-wordpress/blob/642f156c87d963573aa8203b3115c9eea3c65094/includes/metabox.php#L76
  2. try to display the notices (but they are not displayed by Gutenberg): https://github.com/pushpad/pushpad-wordpress/blob/642f156c87d963573aa8203b3115c9eea3c65094/pushpad.php#L62

Screenshots, screen recording, code snippet

No response

Environment info

No response

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

talldan commented 1 year ago

Some history here as to why admin notices aren't shown - https://github.com/WordPress/gutenberg/issues/12243, https://github.com/WordPress/gutenberg/pull/12444.

My feeling is that this is the right decision. Like the customizer (which also doesn't display admin notices), the editor isn't a typical admin screen. Admin notices are still there for the user in the admin interface. I'll also invite some feedback from a designer, since it's a design related issue.

There is an API for notices that are just for the editor, and plugins can use this (https://developer.wordpress.org/block-editor/how-to-guides/notices/).

I guess one thing that could change this and bring admin notices back is to display them in a separate part of the interface, like a notifications sidebar. I'm still not sure how some of those bigger notices shown in #12243 would look in a smaller space though.

collimarco commented 1 year ago

Maybe you can get the notices automatically from the admin_notices hook and display them (using JavaScript or anything else): what do you think?

To be honest it seems impossible / extremely complex to display a notice in Gutenberg with current state. Where are you supposed to put that JavaScript code? How can you fetch the message to display from the backend (get_option doesn't work in the client-side)?

I've seen people writing hundreds of lines of code just to display a simple notice. This seems pretty crazy and their code will probably break in future versions.

Please provide an integration with admin_notices or a simple / standard solution to display a message from the backend.

talldan commented 1 year ago

Maybe you can get the notices automatically from the admin_notices hook and display them (using JavaScript or anything else): what do you think?

Wouldn't that reintroduce the issues I mentioned above?

I've seen people writing hundreds of lines of code just to display a simple notice. This seems pretty crazy and their code will probably break in future versions.

Seems a bit excessive when one line works:

wp.data.dispatch( 'core/notices' ).createNotice( 'info', 'Hello, World!' )
collimarco commented 1 year ago

@talldan Ok, but what is thee "official" way to display a notification to the admin generated in the backend (i.e. the source of the message is from PHP and not JavaScript)?

For example, let's say that you send some emails when a post is published (in publish_post), how can you display a notice to the admin about the successful/unsuccessful sending of those emails? You are inside a PHP function, not in the JavaScript frontend... and it seems that Gutenberg doesn't offer any binding between frontend and backend for this kind of messages.

t-hamano commented 1 year ago

In my opinion, what you want to achieve may be done with JavaScript. The approach is to use the core data API rather than the idea of linking data generated by PHP to JavaScript.

From your code, it looks like you want to display a message based on the value of get_option. To achieve this on the block editor, I think the following API would be available:

Based on the metadata obtained, I believe you can use createNotice described by @talldan to display the message.

talldan commented 1 year ago

You might also be able to add an inline script that calls the createNotice function, but passes in some PHP values.

WordPress core seems to do this whenever editing the privacy policy page: https://github.com/WordPress/wordpress-develop/blob/a5463b4500a183373476c66d88639d8890f9bd3f/src/wp-admin/includes/class-wp-privacy-policy-content.php#L339-L354

jordesign commented 1 year ago

This issue seems to have stalled - and my read is that this is more of a wontfix (as the behaviour is by design). @talldan @t-hamano would you agree that we can close this issue?

t-hamano commented 1 year ago

Yes, I, too, think this issue should be closed. The API to push notifications to the block editor is well provided, and this comment shows how to notify the editor using messages generated by the backend. How these are implemented would be left to the developer.

@collimarco

I would like to close this issue, but if I have missed something feel free to comment.