Redirect Plugin Page After Plugin Activation in WordPress

Hello devs

In this tutorial i will show you how we can redirect a user to plugin page after activating a wordpress plugin. It is very simple. Sometimes when we building a WordPress plugin, we may want to have the plugin immediately show its settings page after activation. 

Many beginner users don't know what to do or how to do. They don't find plugin setting page. That's why you can add this features to redirect user after plugin installaion to pluging settings page. It may help your plugin users.

If you don't know how to redirect plugin page after plugin activation in wordpress then this tutorial is for you. We will use register activation hook to do that. So follow this tutorial.

First you need to register an activation hook. In your plugin file, you can add this code:

register_activation_hook( __FILE__, 'codechief_activate' );

function codechief_activate() {}

 

now add this line of code in your plugin file.

add_action( 'admin_init', 'codechief_redirect_after_installation' );

 

Then create the below method

function jwe_activate() {
	add_option( 'codechief_redirect_after_installation', true );
}

 

Now in the final step, create the below method

function codechief_redirect_after_installation() {
	
	if ( get_option( 'codechief_redirect_after_installation', false ) ) {
		
		delete_option( 'codechief_redirect_after_installation' );

		wp_safe_redirect( admin_url( 'options-general.php?page=your_plugin_page' ) );

		exit;
	}
}

 

Read also : Woocommerce Related Products Custom Query Example

 

Hope it can help you.