Closed collimarco closed 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.
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.
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!' )
@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.
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:
useEntityProp
that appears in this article to get the meta data for the post.Based on the metadata obtained, I believe you can use createNotice
described by @talldan to display the message.
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
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?
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.
Description
This code works properly on any admin page, but not in Gutenberg:
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):
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