最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Redirect to a welcome page after updating plugin

programmeradmin1浏览0评论

I am working on an update to a WordPress plugin with some major changes and need to explain these changes to the user after they update the plugin. I am able to redirect the user to a welcome page if they manually install the plugin as I can use the activation hook, but it doesn't work if they update the plugin from the WordPress Plugins page.

The current method I am using sets a transient in the activation hook:

set_transient( '_abc_activation_redirect', true, 30 );

And then redirects the user if the transient is present:

add_action( 'admin_init', 'abc_welcome_screen_do_activation_redirect' );
function abc_welcome_screen_do_activation_redirect() {
    // Bail if no activation redirect
    if ( ! get_transient( '_abc_activation_redirect' ) )
        return;

    // Delete the redirect transient
    delete_transient( '_abc_activation_redirect' );

    // Bail if activating from network, or bulk
    if ( is_network_admin() || isset( $_GET['activate-multi'] ) )
        return;

    wp_safe_redirect( admin_url( 'index.php?page=abc-welcome-page' ) ); exit;
}

Is it possible to redirect the user to a welcome page after they update the plugin on the WordPress Plugins page? I haven't been able to find the answer to this anywhere!

Many thanks in advance!

I am working on an update to a WordPress plugin with some major changes and need to explain these changes to the user after they update the plugin. I am able to redirect the user to a welcome page if they manually install the plugin as I can use the activation hook, but it doesn't work if they update the plugin from the WordPress Plugins page.

The current method I am using sets a transient in the activation hook:

set_transient( '_abc_activation_redirect', true, 30 );

And then redirects the user if the transient is present:

add_action( 'admin_init', 'abc_welcome_screen_do_activation_redirect' );
function abc_welcome_screen_do_activation_redirect() {
    // Bail if no activation redirect
    if ( ! get_transient( '_abc_activation_redirect' ) )
        return;

    // Delete the redirect transient
    delete_transient( '_abc_activation_redirect' );

    // Bail if activating from network, or bulk
    if ( is_network_admin() || isset( $_GET['activate-multi'] ) )
        return;

    wp_safe_redirect( admin_url( 'index.php?page=abc-welcome-page' ) ); exit;
}

Is it possible to redirect the user to a welcome page after they update the plugin on the WordPress Plugins page? I haven't been able to find the answer to this anywhere!

Many thanks in advance!

Share Improve this question asked Feb 2, 2017 at 22:56 JohnJohn 131 silver badge4 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 1

There was a discussion a few years earlier about adding register_update_hook() but it has never been implemented on the core. The idea was to have a hook just like we have one for activation register_activation_hook.

I am not sure but I am assuming that when a plugin gets updated it deactivates itself beforehand and gets reactivated silently. If that is the case, then you probably would need to use the register_activation_hook function and create an option using add_option (see code below).

If my assumption is wrong try to force a plugin deactivation when the user clicks on the update button so you can use the hook.

register_activation_hook(__FILE__, 'my_plugin_update');
add_action('admin_init', 'my_plugin_redirect');

function my_plugin_update() {
 add_option('my_plugin_do_update_redirect', true);
}

function my_plugin_redirect() {
 if (get_option('my_plugin_do_update_redirect', false)) {
     delete_option('my_plugin_do_update_redirect');
     wp_redirect('index.php?page=abc-welcome-page');
 }
}

I think it is a very bad idea to redirect the user during a plugin update process. There are several potential problems I see with that.

If a plugin is updated via Ajax request, a response with a redirect will not load that page in the user's browser window.

I also guess that plugins are expected to not output stuff like redirect headers during the update process.

Modify $_REQUEST ['_wp_http_referer'] during update request, to redirect where you want, using available callbacks. Convenient callbacks are filters in function update_option (/wp-includes/option.php)

Why?

WordPress /wp-admin/options.php redirects back to plugin that submitted form to Options API.

File:/wp-admin/options.php Line 297, WordPress 5.0.3

    /**
     * Redirect back to the settings page that was submitted
     */
    $goback = add_query_arg( 'settings-updated', 'true',  wp_get_referer() );
    wp_redirect( $goback );
    exit;

wp_get_referer() will invoke wp_get_raw_referer()

function wp_get_raw_referer() {
    if ( ! empty( $_REQUEST['_wp_http_referer'] ) ) {
        return wp_unslash( $_REQUEST['_wp_http_referer'] );
    } else if ( ! empty( $_SERVER['HTTP_REFERER'] ) ) {
        return wp_unslash( $_SERVER['HTTP_REFERER'] );
    }

    return false;
}

How?

Replace

    wp_safe_redirect( admin_url( 'index.php?page=abc-welcome-page' ) ); exit;

in your example with

    add_filter( 'pre_update_option', 'abc_on_pre_update_set_redirect', 10, 2 );

and add function below

    function abc_on_pre_update_set_redirect( $value, $option ) {
        if ( $option === 'abc_option' ) {
            $_REQUEST['_wp_http_referer'] = admin_url( 'index.php?page=abc-welcome-page' )
        }
        return $value;
    }
发布评论

评论列表(0)

  1. 暂无评论