WordPress Development Tutorials

Add Custom Admin Notice Until Admin Dismiss in WordPress Using Database

Written by Anup Kumar
Posted on - 10 min read

Hey,

If you want to be a good WordPress plugin developer then you must have knowledge about this small feature.

Tutorial Prerequisites

You can push custom messages for the following use cases:

  1. Ask your customer to review your plugin
  2. Help them by showing a link from where they should get started?
  3. Or you can redirect them to your setting page. But it’s not a good idea but still, you can do it.

We are also going to keep this Add Custom Admin Notice sticky till they don’t close it manually.

So it will be going to look like this:

wordpress custom admin notice on plugin activation
Showing welcome message to config NexGen Enhanced Ecommerce plugin config admin notice

So let’s add custom admin notice in just 3 steps:

Step 1. Understand my file hierarchy first to relate the code.

Step 2. Modify your plugin code as below instructions in your class

// These all below codes are in wp-content/plugins/nexgi-woo-enhance-ecommerce/admin/class-nexgi-woo-enhance-ecommerce-admin.php see the above screenshot

// define as private key variable
private $db_welcome_dismissed_key;


public function __construct() {

    // name of unique admin notice close key
    $this->db_welcome_dismissed_key =  'nwee_welcome_dismissed_key';

    // registering notice 
    add_action( 'admin_notices', array( &$this, 'dashboardNotices' ) );

    // private ajax URL to save admin has close the admin notice
    add_action( 'wp_ajax_nwee_dismiss_dashboard_notices', array( $this, 'dismissDashboardNotices' ) );
}


// show relevant notices for the plugin
function dashboardNotices() {
   // current admin page  
   global $pagenow;
   
   // 1. checking if user already closed the admin notice or not
   // 2. checking current user have capability to manage this plugin or not
   if (!get_option($this->db_welcome_dismissed_key )  && current_user_can( 'manage_options' )) {
        // check if user is not on the plugin page as this notice must not be visible at plugin page 
	if ( ! ( 'options-general.php' === $pagenow && isset( $_GET['page'] ) && 'nwee' === $_GET['page'] ) ) {
                // generating setting page url to pass on view
		$setting_page = admin_url( 'options-general.php?page=nwee');
		// load the notices view
		require_once __DIR__.'/partials/dashboard-notices.php';
		}
	}
}



// Dismiss the welcome notice for the plugin - ajax body

function dismissDashboardNotices() {
    check_ajax_referer( 'nwee-nonce', 'nonce' );
    // user has dismissed the welcome notice
    update_option( $this->db_welcome_dismissed_key, 1 );
    exit;
}

Step 3. Design the admin notice view.

We are done with all the changes in the class file. Now we have to design the admin notice view.

// below code will be inwp-content/plugins/nexgi-woo-enhance-ecommerce/admin/partials/dashboard-notices.phpsee the above screenshot to under the hierarchy

<div class="notice notice-success is-dismissiblenwee-notice-welcome">
	<p>
		<?php
		printf(
			/* translators: %s: Name of this plugin */
			__( 'Thank you for installing %1$s!', 'nexgi-woo-enhance-ecommerce' ),
		'NAME OF YOUR PLUGIN'
		);
		?>
		<a href="<?php echo $setting_page; ?>"><?php esc_html_e( 'Click here', 'nexgi-woo-enhance-ecommerce' ); ?></a> <?php esc_html_e( 'to configure the plugin.', 'nexgi-woo-enhance-ecommerce' ); ?>
	</p>
</div>
<script type="text/javascript">
	jQuery(document).ready( function($) {
		$(document).on( 'click', '.nwee-notice-welcome button.notice-dismiss', function( event ) {
			event.preventDefault();
			$.post( ajaxurl, {
				action: '<?php echo 'nwee_dismiss_dashboard_notices'; ?>',
				nonce: '<?php echo wp_create_nonce( 'nwee-nonce' ); ?>'
			});
			$('.nwee-notice-welcome').remove();
		});
	});
</script>

All done!

Step 4 Improvements Guidelines:

  1. Replace nwee- with your customer’s unique slug. So it doesn’t conflict with my plugin.
  2. Use constant instead of nwee-. I have removed the constant as it becomes more confusing for you guys to implement the solution.

chat-box