How to Show Custom Notifications in WordPress Dashboard?

How can I create custom admin notices in WordPress without using a plugin? I want to display errors, warnings, or success messages to users in the WordPress admin area. I know there is a hook called admin_notices that I can use, but I’m not sure how to write the code for it. Can anyone help me with some examples or tutorials?

To create custom admin notices in WordPress without using a plugin, you can use the admin_notices hook to display your messages in the WordPress admin area. You can also use different classes to style your notices as error, warning, success, or info.

// Add a custom function to the admin_notices hook
add_action( 'admin_notices', 'show_custom_notice' );

// Define the custom function
function show_custom_notice() {
    $type = 'info'; // error, warning, success,
    echo '<div class="notice notice-' . esc_attr( $type ) . ' is-dismissible">
            <p>Hello World!</p>
          </div>';
}

This is the HTML code for the notification.

<div class="notice notice-info is-dismissible">
      <p>Hello World!</p>
</div>

You have the below class available to change the message type

.notice-warning
.notice-error
.notice-info
.notice-success