How to show an urgent message in the WordPress admin area

April 19, 2011 at 2:49 pm

When writing custom WordPress theme or plugins, you might want to inform users that something important needs doing, perhaps due to an upgrade. e.g. You need the user to update a setting, or check that their settings have been transposed correctly. This is a very short guide that shows you how to add a error or status message using the correct WordPress hooks.

The following is a snippet of my custom function that shows a message to the user. This be used from anywhere in your theme or plugin if you wanted to give the user a standard message. e.g. “Settings successfully updated” or something similar.

/**
 * Generic function to show a message to the user using WP's 
 * standard CSS classes to make use of the already-defined
 * message colour scheme.
 *
 * @param $message The message you want to tell the user.
 * @param $errormsg If true, the message is an error, so use 
 * the red message style. If false, the message is a status 
  * message, so use the yellow information message style.
 */
function showMessage($message, $errormsg = false)
{
	if ($errormsg) {
		echo '<div id="message" class="error">';
	}
	else {
		echo '<div id="message" class="updated fade">';
	}

	echo "<p><strong>$message</strong></p></div>";
}    

Now we just add a hook to the admin notices function to show our custom message.

/**
 * Just show our message (with possible checking if we only want
 * to show message to certain users.
 */
function showAdminMessages()
{
    // Shows as an error message. You could add a link to the right page if you wanted.
    showMessage("You need to upgrade your database as soon as possible...", true);

    // Only show to admins
    if (user_can('manage_options') {
       showMessage("Hello admins!");
    }
}

/** 
  * Call showAdminMessages() when showing other admin 
  * messages. The message only gets shown in the admin
  * area, but not on the frontend of your WordPress site. 
  */
add_action('admin_notices', 'showAdminMessages');     

And that’s all there is to it!

Thanks to Dan Harrison of WordPress Doctors for the tip!

Tags: