WordPress Admin Notification Overhaul

WordPress admin notifications are messages which appear on the top of any admin page. Any plugin can trigger a function which renders html. It's impossible to stop certain messages from being rendered, but with some luck you can use CSS to hide the message.

Anyone can add an admin notice by hooking to the action admin_notices. The hook is ran at the top of the admin content section. The hook renders html by echoing content. The content can be anything, a <div> or a <figure> tag. There are no restrictions on what can be rendered.

function sample_admin_notice__success() {
    ?>
    <div class="notice notice-success is-dismissible">
        <p><?php _e( 'Done!', 'sample-text-domain' ); ?></p>
    </div>
    <?php
}
add_action( 'admin_notices', 'sample_admin_notice__success' );

Plugins often abuse the admin notice functionality, calling for upgrades to free plugins, donations to their development cost or rating their plugin on wordpress.org.

alt text Source: https://digisavvy.com/2016/04/remove-wordpress-admin-notifications/

Certain developers have tricks for hiding notifications. Other developers have plugins dedicated to handling the issue of rogue plugins spamming all admin users.

WordPress multi-site is often used for Software as a Service (SAAS). Having plugin notifications appearing in the admin cause confusion for customers. Customers who are paying for your product built on WordPress should not have to worry about plugin updates, but they still need some notifications. For example when a user updates a post they should receive a "Post Updated" notifications.

Because certain notifications are helpful and other notifications are a unwelcomed annoyance the solution is not to hide all notifications. A better solution is needed.

A better way to notify

A better solution awaits! Taking a nod from wp_enque_script we add two new functions and one new global variable.

wp_add_admin_notice( $handle, $function );
wp_remove_admin_notice( $handle );
global $wp_notices

Having the ability to remove a notice would be invaluable for SAAS products. In order to remove all notices except core admin notices we need access to any array of all notifications. Core admin notices are notices only set by core admin files, no plugins or themes.

Example of a few core notification handles:

$handles = [
  'wp_notification_post_updated',
  'wp_notification_post_trashed',
  'wp_notification_plugin_activated',
  'wp_notification_plugin_deactivated',
];

To make things even better WordPress could offer a filter for notification. This could give WordPress core the ability to filter out any notifications hijacking the WordPress core handles.

Don't break old builds!

So how do we implement the new notification engine without breaking old WordPress builds? Ummm....

In theory the add_action trigger could call the wp_add_admin_notice with the callable function as the handle. This would only work for named functions. Otherwise an inline callable function would need to be referenced via the ReflectionFunction class.

Quick fix

For a quick fix to the problem WordPress core could wrap their own admin notices in a different div using a different private method. That way developers could make sure core notifications can be given priority over developer driven notifications.

Thanks for reading. Code On.

Subscribe to our mailing list to receive updates about emerging tech trends, important security patches and really bad dad jokes.