admin管理员组文章数量:1122846
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:
.php?page=rotator
Is there a way to redirect to the corresponding page on activation, for example with register_activation_hook
?
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
?
3 Answers
Reset to default 26Maybe 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:
- Register activation hook
- 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.
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!
I have used the below code redirect after plugin activation. You can use this code. It's working nicely.
register_activation_hook(__FILE__, 'nht_plugin_activate');
add_action('admin_init', 'nht_plugin_redirect');
function nht_plugin_activate() {
add_option('nht_plugin_do_activation_redirect', true);
}
function nht_plugin_redirect() {
if (get_option('nht_plugin_do_activation_redirect', false)) {
delete_option('nht_plugin_do_activation_redirect');
if(!isset($_GET['activate-multi']))
{
wp_redirect("edit.php?post_type=headline&page=news-headline");
}
}
}
本文标签: activationHow to redirect to settings page once the plugin is activated
版权声明:本文标题:activation - How to redirect to settings page once the plugin is activated? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736309534a1934055.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论