Warning: Use of undefined constant DATABASE_SERVER - assumed 'DATABASE_SERVER' (this will throw an Error in a future version of PHP) in /home/redtagco/public_html/hassan-raza.com/wp-config.php on line 31
Redirect user on plugin activation - Hassan Raza

Redirect user on plugin activation

Sometime, it is necessary to redirect users to an introduction or to a necessary options page on plugin activation. Here I am sharing a quick way to do that.

There are recommendations to do so directly on plugin activation which is not a recommended way as it can cause a 202 error which will stop the plugin to be activated. You will see an error page by WordPress saying “Sorry, you are not allowed to access this page.”

To redirect users to a specific page you need to use the register_activation_hook() function which registers a plugin function to be run when the plugin is activated.

The best way to do it is to have the activation option saved in wp_options table so that we can use and destroy it after successfully redirecting the user. For that, we need to create two separate functions.

First, we create a function to save the related option upon activation of our plugin. After that we will call it in our register_activation_hook().

 * Activation Hook
function mhr_plugin_activation(){
   add_option( 'mhr_plugin_do_activation_redirect', true );

// Calling the function in activation hook
register_activation_hook(__FILE__, 'mhr_plugin_activation' );

This will create a necessary option which we are going to use to redirect the user on admin_init action hook.

  * Function to check for the option if exist then delete it and
  * redirect the user.
function mhr_plugin_activation_redirect() {

   // check if option is set
   if ( get_option( 'mhr_plugin_do_activation_redirect', false ) ) {

      // delete the option 
      delete_option( 'mhr_plugin_do_activation_redirect' );

      // redirect user to your options page
      //wp_redirect() does not exit automatically and should almost always be followed by exit.

// Calling the function on admin-init action hook
add_action('admin_init', 'mpu_plugin_activation_redirect' );

Now, it will redirect users to the given part of the URL in wp_redirect() function. Make sure this information is correct.

Doing it inside the class

If you want to keep it inside a class then you need to use public functions and calling the functions in array() where the first argument will represent the current class called by $this variable and the second argument will be the function name as seen in the following piece of code combined.

 * Admin init action Hook inside class
public function mhr_plugin_activation(){
   add_option('mhr_plugin_do_activation_redirect', true);

register_activation_hook(__FILE__, array( $this, 'mhr_plugin_activation' ) );

 * Activation Redirect Function inside class
public function mhr_plugin_activation_redirect() {
   if (get_option('mhr_plugin_do_activation_redirect', false)) {
      //wp_redirect() does not exit automatically and should almost always be followed by exit.

// admin init hook inside class
add_action('admin_init', array( $this, 'mhr_plugin_activation_redirect') );

The hooks can be placed in related functions if you are using like mhr_init() with all initializations in it etc.

Remove Category or Tag Slug from url
There are currently no comments.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Do NOT follow this link or you will be banned from the site!