1

I want to make a featured image required. For starters i am trying to show an error message after update or post buttons are pressed. The problem is that the notices which i try to include don't show up. For example

function my_admin_notice(){
    global $pagenow;
    if ( $pagenow == 'post.php' ) {
        echo '<div class="updated">
             <p>This notice only appears on the plugins page.</p>
         </div>';
    }
}
add_action('admin_notices', 'my_admin_notice');

Whatever notice i try to display in specific post admin screen it doesn't show. Could the problem be in the new Gutenberg editor?

Edit: Yes, when Gutenberg is active the notices don't show up. If i add filter to block the editor, then the messages are displayed. So, how can i make notices to shows in the Gutenberg editor too?

1
  • This is because your code shows an admin notice at the top of the page on a new page request, but Gutenberg uses REST API endpoints to save the post, not a form submission/page request
    – Tom J Nowell
    Commented Jul 31, 2019 at 21:03

1 Answer 1

1

I found the answer. Notices in the Block Editor would require JavaScript code.

   ( function( wp ) {
    wp.data.dispatch('core/notices').createNotice(
        'error', // Can be one of: success, info, warning, error.
        'Error Message.', // Text string to display.
        {
            isDismissible: true, // Whether the user can dismiss the notice.
            // Any actions the user can perform.
            actions: [
                {
                    url: '#',
                    label: 'View post'
                }
            ]
        }
    );
} )( window.wp );

Link: https://developer.wordpress.org/block-editor/tutorials/notices/

1
  • 1
    How do you call this from php so the snackbar is shown? I can't find an example anywhere....
    – Shahriar
    Commented Dec 22, 2022 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.