Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

admin_notices are not displayed in Gutenberg #44976

Closed
collimarco opened this issue Oct 14, 2022 · 8 comments
Closed

admin_notices are not displayed in Gutenberg #44976

collimarco opened this issue Oct 14, 2022 · 8 comments
Labels
General Interface Parts of the UI which don't fall neatly under other labels. Needs Design Feedback Needs general design feedback. [Type] Enhancement A suggestion for improvement.

Comments

@collimarco
Copy link

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 talldan added Needs Design Feedback Needs general design feedback. General Interface Parts of the UI which don't fall neatly under other labels. labels Oct 17, 2022
@talldan
Copy link
Contributor

talldan commented Oct 17, 2022

Some history here as to why admin notices aren't shown - #12243, #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
Copy link
Author

collimarco commented Oct 17, 2022

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
Copy link
Contributor

talldan commented Oct 18, 2022

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
Copy link
Author

@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
Copy link
Contributor

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:

  • isCurrentPostPublished (or other selector): Check to see if the current post has been published
  • Meta Boxes: This is the documentation on the metabox, but I believe you can use 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.

@talldan
Copy link
Contributor

talldan commented Oct 24, 2022

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
Copy link
Contributor

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?

@jordesign jordesign added the [Type] Enhancement A suggestion for improvement. label Aug 2, 2023
@t-hamano
Copy link
Contributor

t-hamano commented Aug 2, 2023

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.

@t-hamano t-hamano closed this as not planned Won't fix, can't repro, duplicate, stale Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
General Interface Parts of the UI which don't fall neatly under other labels. Needs Design Feedback Needs general design feedback. [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

No branches or pull requests

4 participants