How To Create Closeable Admin Notice in WordPress Dashboard

Create the function and echo the message.

function showMessage($message, $errormsg = false)
{
	if ($errormsg) {
		echo '<div id="message" class="error">';
	}
	else {
		echo '<div id="message" class="updated fade">';
	}
	echo "<strong>$message</strong></div>";
}

/**
 * Just show our message (with possible checking if we only want
 * to show message to certain users.
 */
function showAdminMessages()
{
	global $current_user;

	// Only show to admins
	if (user_can($current_user-&gt;ID, 'manage_options')) {
		$m = get_user_meta($current_user-&gt;ID, 'plg-delete-post-notice', true);
		// Shows as an error message. You could add a link to the right page if you wanted.
		if ($m != 'close' &amp;&amp; $m != 'remind-later') {
			showMessage('Be sure to backup database for WP Auto Delete Plugin.<span style="float: right;"><a class="notice-remind" href="notice-remind">Remind Me Later</a> | <a class="notice-close" href="notice-close">Close</a></span>', true);
		}
	}
}

Call the add_action to add the message to admin notices.

add_action('admin_notices', array($wpAcePosts, 'showAdminMessages'));

Create the jquery to do the close

function ajax_scripts()
{
<script type="text/javascript">
	var $jx = jQuery.noConflict();
	var ajaxurl = '<?php echo admin_url("admin-ajax.php"); ?>;';

	/**
	 * called when an element is clicked
	 * <input type="text" name="myname" />;
	 * <a href="#" onclick="javascript: alertName()">Click</a>;
	 */

	$jx('.notice-remind').click(function(){
		$jx.ajax({
			url:ajaxurl,
			type:'POST',
			data:'action=wpacedp_ajax&amp;type=remind-later',
			success:function(result){
			$jx('#message').slideUp('slow');
		}
		});

		return false;
	});

	$jx('.notice-close').click(function(){
		$jx.ajax({
			url:ajaxurl,
			type:'POST',
			data:'action=wpacedp_ajax&amp;type=close',
			success:function(result){
			$jx('#message').slideUp('slow');
			}
		});
		return false;
	});
</script>
<?php
} //end ajax_scripts()

Add ajax to add usermeta if the buttons are clicked

/** AJAX */
function ajax()
{
	global $current_user;
	$type = $_POST['type'];

	update_user_meta($current_user->ID, 'plg-delete-post-notice', $type);
	exit;
} // end ajax

 

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInPrint this pageEmail this to someone

Leave a Reply

Your email address will not be published. Required fields are marked *