WordPress Code Snippets

A fresh mix of code snippets and tutorials

WordPress Dismissible Admin Notice

261

Here is an easy snippet for adding a dismissible admin notice to any theme or plugin.

Update: I’ve written a complete guide to WordPress Admin Notices over at DigWP.com!</update>

Just add the following to your theme’s functions.php file, or to your plugin:

function shapeSpace_plugin_notice() {
	
	global $current_user;
	
	$user_id = $current_user->ID;
	
	if (!get_user_meta($user_id, 'shapeSpace_plugin_notice_ignore')) {
		
		echo '<div class="updated notice"><p>'. __('This is a notice!') .' <a href="?my-plugin-ignore-notice">Dismiss</a></p></div>';
		
	}
	
}
add_action('admin_notices', 'shapeSpace_plugin_notice');
	
function shapeSpace_plugin_notice_ignore() {
	
	global $current_user;
	
	$user_id = $current_user->ID;
	
	if (isset($_GET['my-plugin-ignore-notice'])) {
		
		add_user_meta($user_id, 'shapeSpace_plugin_notice_ignore', 'true', true);
		
	}
	
}
add_action('admin_init', 'shapeSpace_plugin_notice_ignore');

Once in place, this snippet will add a dismissible update alert notice at the top of each page in the WordPress Admin Area. The notice will include a link that each user can click to dismiss permanently.

That’s all there is to it, should be entirely plug-n-play, although you’ll probably want to customize the actual alert message (in the shapeSpace_plugin_notice() function).

Learn more

WordPress Themes In Depth


Show Support

Like our new Facebook Page to show support!