Redirect after plugin activation

How do I redirect users to my plugin settings page after they activate my plugin, I tried

register_activation_hook(__FILE__,'activate_myplugin');
function activate_myplugin()
{
//create and populate DB tables
wp_redirect(plugin_setting_url);
}

but it does not work.

Related posts

Leave a Reply

5 comments

  1. You should be able to do something like this:

    register_activation_hook(__FILE__, 'my_plugin_activate');
    add_action('admin_init', 'my_plugin_redirect');
    
    function my_plugin_activate() {
        add_option('my_plugin_do_activation_redirect', true);
    }
    
    function my_plugin_redirect() {
        if (get_option('my_plugin_do_activation_redirect', false)) {
            delete_option('my_plugin_do_activation_redirect');
            wp_redirect(MY_PLUGIN_SETTINGS_URL);
        }
    }
    
  2. This will redirect to option page only if that plugin is activated only without using bulk activation mode .

    register_activation_hook(__FILE__, 'my_plugin_activate');
    add_action('admin_init', 'my_plugin_redirect');
    
    function my_plugin_activate() {
        add_option('my_plugin_do_activation_redirect', true);
    }
    
    function my_plugin_redirect() {
        if (get_option('my_plugin_do_activation_redirect', false)) {
            delete_option('my_plugin_do_activation_redirect');
            if(!isset($_GET['activate-multi']))
            {
                wp_redirect("options-general.php?page=your-plugin-option-page");
            }
        }
    }
    
  3. thanks for your code – it´s great, but only has one downside: upon bulk activation of plugins, you also get redirected to your defined redirect page – which might confuse user when deactivating/activating all plugins at once for test/debug reason. I therefore would propose the solution, to add an option to only redirect to your page on FIRST plugin activation:

    register_activation_hook(__FILE__, 'my_plugin_activate');
    add_action('admin_init', 'my_plugin_redirect');
    
    function my_plugin_activate() {
    add_option('myplugin_redirect_on_first_activation', 'true');
    }
    
    function my_plugin_redirect() {
        if (get_option(MYPLUGIN_REDIRECT_ON_FIRST_ACTIVATION_KEY) == 'true') {
            update_option(MYPLUGIN_REDIRECT_ON_FIRST_ACTIVATION_KEY, 'false');
            wp_redirect(MY_PLUGIN_SETTINGS_URL);
        }
    }
    
  4. Hello i have used bellows code redirect after plugin activation. You can use this code. It’s working nicely.

    register_activation_hook(__FILE__, 'nht_plugin_activate');
    add_action('admin_init', 'nht_plugin_redirect');
    
    function nht_plugin_activate() {
    add_option('nht_plugin_do_activation_redirect', true);
    }
    
    function nht_plugin_redirect() {
    if (get_option('nht_plugin_do_activation_redirect', false)) {
        delete_option('nht_plugin_do_activation_redirect');
        if(!isset($_GET['activate-multi']))
        {
            wp_redirect("edit.php?post_type=headline&page=news-headline");
        }
     }
    }
    

    nht_ is my plugin prefix & “edit.php?post_type=headline&page=news-headline” is redirect page. please replace this those.