For the plugin I’m developing I wanted to show a message after activation to prompt the admin to enter the licence key and provide a direct link to the Settings page where they key needs to be added.

My first attempt was to use the admin_notices action like so:

add_action( 'admin_notices', array( $this, 'SCW_admin_notices' ) );

But this action does not work when used inside the plugin activation hook. I haven’t quite figured out why, any smart ideas please leave in comments! So back to Google and I found several code snippets which had suggestions how to show a message inside the activation hook. The final solution I settled on is based on setting a transient on activation, adding an action to check for the transient and if found, show the activation notice to the same user who activated the plugin.

Step 1 in my activation function I set a transient, the value of which is the ID of the user who is activating the plugin:

set_transient( 'scw-stock-control-for-woocommerce-activated', wp_get_current_user()->ID, 0 );

Step 2 in code (in my case the EDD class) I added an action to admin_notices. Notice the use of an array with $this as I’m using this inside a class. If you’re not working inside a class, then the second argument is just the function name.

add_action( 'admin_notices', array( $this, 'SCW_admin_notices' ) );

Step 3 in the function I added the code to check for the transient, check the User ID, display a message and delete the transient so that the message is only shown once:

/**
 	* This is a means of catching errors from the activation method above and displaying it to the customer
 	*/
	public function SCW_admin_notices() {
		
		/* Check transient, if available activation notice */
		if( ( get_transient( 'scw-stock-control-for-woocommerce-activated' ) == wp_get_current_user()->ID  ) && ( get_option( SCW_LICENCE_STATUS ) !== 'valid' ) ) {
			?>
			<div class="notice notice-warning is-dismissible">
				<p>
					<?php echo sprintf( __('Thank you for installing SCW Stock Control! Please enter your SCW Stock Control licence to enable updates. %sGo to SCW Settings%s', 'agal_scw'), '<a href="' . admin_url( 'admin.php?page=scw_settings' ) . '">', '</a>'); ?> 
				</p>
			</div>
			<?php
			/* Delete transient, only display this notice once. */
			delete_transient( 'scw-stock-control-for-woocommerce-activated' );
		}

	}

In my code I also added a check for the SCW_LICENCE_STATUS option which is set elsewhere and stores the value of the licence (valid, expired etc).

So there you have it – a quick way to show a message after plugin activation to the admin. This could also be used for a welcome message or any other message you want to show immediately after activation.

A note about admin messages: in my messages I always include the class “is-dismissible”, this adds the X closing icon in the top right corner of the message. If the message is not relevant to them, then they can click it away and it won’t be shown again. For details on the classes you can use have a look at the WP site here: https://developer.wordpress.org/reference/hooks/admin_notices/.

Assumptions

For my code I’m assuming you already have a plugin with an activation hook. If you don’t here’s the code I use. My plugin uses classes and Namespaces. To register the activation I’m placing this code in my core plugin file which links to the activate() function in the SCW_Activator class.

register_activation_hook( __FILE__, array( __NAMESPACE__ . '\SCW_Activator', 'activate') );

Within the activate class my functions need to be static and I’m using the following code to set up the tables for my plugin and set the transient. The code is multisite compatible, if the plugin is run on MUWP and activated network wide it will run for every blog. A neat trick is that for activation, WP makes the $network_wide variable available which makes it super easy to check if the plugin was activated locally on a single install or network wide:

public static function activate( $network_wide ) {
	
		global $wpdb; 
			 
		if ( is_multisite() &&  $network_wide ) {
	
			// Get all blogs in the network and activate plugin on each one
			$blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
					 
			foreach ( $blog_ids as $blog_id ) {           
				switch_to_blog( $blog_id ); 
				  
				self::create_table();       
				self::plugin_functionality();   
			
				restore_current_blog();
			}
				 
		} else {
				 
			self::create_table();     
			self::plugin_functionality(); 
				 
		} 
		
	}

Finally, in the plugin_functionality() function I’m inserting the code as described above to set the transient:

private static function plugin_functionality() {
		  
		/* Create transient data for showing welcome / activation message */
		set_transient( 'scw-stock-control-for-woocommerce-activated', wp_get_current_user()->ID, 0 );
		
	  }

Tweaks

For your code you may want to consider the following tweaks:

  • If you don’t delete the transient after the message is shown, then the message will continue to be shown to the admin until either the licence is activated or the message is dismissed by clicking it away.
  • If you set the transient to a simple boolean TRUE then you could check if transient == true and show the message to all admins
  • Choose the right message class based on the content eg “notice-success” if you want to show a welcome message

Hope this blog post helps you in your own pugin development! If you’re working on a plugin share your experience below, would love to hear what your current project is!

Leave a Reply

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