How to redirect to settings page once the plugin is activated?

Maybe using the wp_redirect() function in the activation hook. In the following example myplugin_settings is a placeholder. Normally this simply is the $hook_suffix you get back from $hook_suffix = add_menu_page( /* etc. */ ); and similar functions.

THIS CODE DOESN’T WORK, READ BELOW

register_activation_hook(__FILE__, 'cyb_activation');
function cyb_activation()
{
    // Don't forget to exit() because wp_redirect doesn't exit automatically
    exit( wp_redirect( admin_url( 'options-general.php?page=myplugin_settings' ) ) );
}

References:

  1. Register activation hook
  2. admin_url()

EDIT

The redirect inside the activation hook seems to be performed before the plugin is effectively activated, maybe because of the call of exit() before the activation is executed. This code seems to work well using activated_plugin action hook:

function cyb_activation_redirect( $plugin ) {
    if( $plugin == plugin_basename( __FILE__ ) ) {
        exit( wp_redirect( admin_url( 'options-general.php?page=myplugin_settings' ) ) );
    }
}
add_action( 'activated_plugin', 'cyb_activation_redirect' );

If you use this code outside the main plugin file you will need to replace __FILE__ with path of the main plugin file.

THOUGHT

Redirecting the user after your plugin has been activated is not a very good approach. In WordPress you can activate plugins in bulk. What happen then if you perform a redirect in this situation? You will break the activation of some plugins, maybe not if your plugin is the last being activated, but definitely you are breaking the user experience.

Leave a Comment