How to Add Custom Alerts to a WordPress Dashboard

How to Add Custom Alerts to a WordPress Dashboard

There are many different ways to reach out to your users – email newsletter, Facebook, Twitter, Google+, etc. The problem with these communication mediums is that users must opt in to receive these notifications. With this simple code snippet you can now place alerts in the WordPress dashboard without your users opting in to receive notifications. It’s even WordPress Multisite compatible.

How to Add Custom Alerts to a WordPress Dashboard
Here is an example of a WordPress Dashboard Alert that we will be making.

How to Add Alerts to a WordPress Dashboard

Add the following code snippet to your WordPress theme’s functions.php file and change the text within the div to whatever you want.

function addAlert() { ?>
<script type="text/javascript">
$j = jQuery;
$j().ready(function(){
	$j('.wrap > h2').parent().prev().after('<div class="update-nag">This is a test alert. Do with it what you want.</div>');
});
</script>
<?php } add_action('admin_head','addAlert');

How to Add an Alert to the WordPress Dashboard Home Only

Add the following code snippet to your WordPress theme’s functions.php file to add an alert to the WordPress Dashboard Home – not all WordPress Dashboard pages.

function addDashboardAlert() { ?>
<script type="text/javascript">
$j = jQuery;
$j().ready(function(){
	$j('h2:contains("Dashboard")').parent().prev().after('<div class="update-nag">This is a test alert for the dashboard home only. Do with it what you want.</div>');
});
</script>
<?php } add_action('admin_head','addDashboardAlert');

How to Add an Alert to the WordPress Dashboard Based on User Level

If you only want to display a message to your Contributors or Editors, no problem. Use the following code snippet in your WordPress theme’s functions.php to display an alert on the WordPress Dashboard to administrators. If you want to change it to a different user level, check out User Levels on the WordPress Codex.

if (current_user_can('administrator')) {
	function addDashboardAlert() { ?>
	<script type="text/javascript">
	$j = jQuery;
	$j().ready(function(){
		$j('.wrap > h2').parent().prev().after('<div class="update-nag">This is a test alert for admins only. Do with it what you want.</div>');
	});
	</script>
	<?php } add_action('admin_head','addDashboardAlert');
}

Conclusion

Creating an alert in the WordPress Dashboard is extremely easy – not only is it easy, but it’s a great way to convey an important message to your WordPress site’s users be it on a standard WordPress install or on WordPress Multisite.

Follow wpLifeGuard

« Back to Blog