5

I can't find any info on this anywhere but i'm sure iv'e seen it done before.

I would like to have a user taken to the settings page for my plugin after install, so either auto-redirected or change the "back to plugins" link to a custom link.

Thanks in advance

SOLVED

function myplugin_activate() {    
    // TODO: Install your plugin here.

    // I don't know of any other redirect function, so this'll have to do.
    wp_redirect(admin_url('options-general.php?page=myplugin_settings'));exit;
    // You could use a header(sprintf('Location: %s', admin_url(...)); here instead too.
}
register_activation_hook(__FILE__, 'myplugin_activate');

@Zack - Your method works great however you forgot to exit after wp_redirect. (source http://codex.wordpress.org/Function_Reference/wp_redirect)

Many thanks

3 Answers 3

5

Register an activation hook and then redirect the user's browser when you're done installing your plugin.

function myplugin_activate() {    
    // TODO: Install your plugin here.

    // I don't know of any other redirect function, so this'll have to do.
    wp_redirect(admin_url('options-general.php?page=myplugin_settings'));
    // You could use a header(sprintf('Location: %s', admin_url(...)); here instead too.
}
register_activation_hook(__FILE__, 'myplugin_activate');
2
  • Note that "options-general.php?page=myplugin_settings" should reflect your options page request uri.
    – Zack
    Feb 19, 2012 at 18:18
  • works perfect however we need to exit; after wp_redirect. See my edit.
    – Danny
    Feb 19, 2012 at 20:46
4

I see the question is already answered. But there is a alternate scenario where you might want to redirect to settings page after a theme installed. You can do it very easily using following codes :)

if (is_admin() && isset($_GET['activated'])){

    wp_redirect(admin_url("themes.php?page=ot-theme-options"));
}

You just have to replace the following code themes.php?page=ot-theme-options With your own setting page url.

It is farly easy to do and we are using WP redirect function.

Thanks Sabbir

2

Well, you also can use another and more elegant solution:

register_activation_hook(__FILE__, function () {
    add_option('your_plugin_do_activation_redirect', true);
});

add_action('admin_init', function () {
    if (get_option('your_plugin_do_activation_redirect', false)) {
        delete_option('your_plugin_do_activation_redirect');
        if(wp_safe_redirect("options-general.php?page=myplugin_settings")) exit();
    }
});

Approved answer is old and produce error inside activation or redirect you to 404 page inside admin. This solution provide you safe redirection with no errors.

1
  • 1
    The awesome solution should be upvoted. Thanks @Ivijan Stefan Stipić
    – Sabbir H
    Sep 12, 2022 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.