I’m building a WordPress plugin and having trouble with admin notifications on my custom admin page. I tried two different approaches but neither works properly.
First attempt - Manual HTML:
<div id="notification" class="updated below-h2 notice is-dismissible">
<p><?php echo $alert_text; ?></p>
<button type="button" class="notice-dismiss">
<span class="screen-reader-text">Close this notification.</span>
</button>
</div>
The dismiss button doesn’t work because there’s no click event handler attached.
Second attempt - Using admin_notices hook:
function show_admin_notification() {
?>
<div class="notice notice-info is-dismissible">
<p>Innovation distinguishes between a leader and a follower ~ Steve Jobs</p>
</div>
<?php
}
add_action('admin_notices', 'show_admin_notification');
This displays notifications everywhere in the admin area. On default WordPress pages the dismiss functionality works fine, but on my custom admin page the close button doesn’t respond to clicks.
I read somewhere that admin notifications only work properly on built-in WordPress admin pages. However, I’ve seen other plugins that successfully implement dismissible notifications on their custom pages.
How can I make admin notifications work correctly on custom plugin pages?