Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I created the Plugin of my own. Everything seems OK but one thing I want to do in that plugin is redirect to the corresponding plugin settings page when the plugin is activate.

For example:
http://www.example.com/wordpress/wp-admin/options-general.php?page=rotator

Is there a way to redirect to the corresponding page on activation, for example with register_activation_hook?

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

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.

register_activation_hook(__FILE__, 'myplugin_activation');
function myplugin_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()
share|improve this answer
    
Thanks for your Response this also works :) –  Fresher Sep 17 '13 at 13:28
1  
Much saner approach. +1 –  kaiser Sep 17 '13 at 16:00
add comment

You should be able to do it 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);
}
// Solution 1
function my_plugin_redirect() {
    if (get_option('my_plugin_do_activation_redirect', false)) {
        delete_option('my_plugin_do_activation_redirect');
         wp_redirect("options-general.php?page=rotator");
         //wp_redirect() does not exit automatically and should almost always be followed by exit.
         exit;
    }
}

// OR

//Solution 2 (@kaiser suggestion)
function my_plugin_redirect() {
    if (get_option('my_plugin_do_activation_redirect', false)) {
        delete_option('my_plugin_do_activation_redirect');
         exit( wp_redirect("options-general.php?page=rotator") );
    }
}

UPDATE

Even if it's a inline comment, remember:

wp_redirect() does not exit automatically and should almost always be followed by exit.

Specific example

<?php
    wp_redirect( $location, $status );
    exit;
?>

UPDATE 10/29/2013

It was not mentioned, but please note that my solution offers the ability to check if is the case or not to do a redirect to plugin settings page. Check my_plugin_activate and the if condition in my_plugin_redirect.

Hope it helps!

share|improve this answer
1  
+1 Awesome Response This what i want. –  Fresher Sep 17 '13 at 13:25
    
Shouldn't there be an exit() around the wp_redirect()? –  kaiser Sep 17 '13 at 16:00
1  
@kaiser, you're right, thank you : ) –  iEmanuele Sep 17 '13 at 16:06
2  
@kaiser, answer updated. –  iEmanuele Sep 17 '13 at 16:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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