Skip to content
Advertisement

How to display toast like this in wordpress admin front side

I added this code but it’s not working and not showing error either in front of admin side

enter image description here

function sample_admin_notice__error() {
        $class = 'notice notice-error';
        $message = __( 'Irks! An error has occurred.', 'sample-text-domain' );
     
        printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) ); 
    }
    add_action( 'admin_notices', 'sample_admin_notice__error' );

Advertisement

Answer

Try below code

   function general_admin_notice(){
        global $pagenow;
        if ( $pagenow == 'options-general.php' ) { //it is work pagewise 
             echo '<div class="notice notice-warning is-dismissible">
                 <p>This notice appears on the settings page.</p>
             </div>';
        }
    }
    add_action('admin_notices', 'general_admin_notice');

You want to display a notice only to users with the author user role.

function author_admin_notice(){
    global $pagenow;
    if ( $pagenow == 'index.php' ) {
      $user = wp_get_current_user();
      if ( in_array( 'author', (array) $user->roles ) ) {
          echo '<div class="notice notice-info is-dismissible">
          <p>Click on <a href="edit.php">Posts</a> to start writing.</p>
          </div>';
      }
   }
}
add_action('admin_notices', 'author_admin_notice');
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement