How to Redirect On Theme Activation

theme-redirect-evil

WordPress plugin have a activation method, It’s very useful, and we can use it for various things.

One of the popular method in plugin, is “Activation Redirect”, this is a method used by a lot ( I mean **A LOT**) of popular plugins to redirect user to plugin settings page, setup page, or even plugin about page (of course, within the admin panel) when user activate plugin.

This is (probably) useful for user on-boarding purpose, annoy user, keep the user inform about the feature of the plugin or help them setup the pages required by the plugin or install additional plugins/add-on.

Note: I actually really hate it when plugin/theme author did this. But in some case it could be useful.

Usually plugin use register_activation_hook() function to do this. But theme don’t have similar method/function. However, there’s a workaround for that.

Here’s an example code to do theme activation redirect:

/* Redirect on theme activation */
add_action( 'admin_init', 'tarex_theme_activation_redirect' );


/**
 * Redirect to "Install Plugins" page on activation
 */
function tarex_theme_activation_redirect() {
    global $pagenow;
    if ( "themes.php" == $pagenow && is_admin() && isset( $_GET['activated'] ) ) {
        wp_redirect( esc_url_raw( add_query_arg( 'page', 'because', admin_url( 'themes.php' ) ) ) );
    }
}

It’s pretty simple right.

It’s using admin_init hook. So we need to use $pagenow instead of $hook_suffix global, because $hook_suffix is not set yet on admin_init hook.

Here’s an example theme I create with this example:

IMPORTANT: This method is no longer allowed for theme hosted at WordPress.org. It is recommended to simply add admin notice with a link to theme settings/about page.