WordPress注册自动登录和自定义重定向

时间:2017-02-21 09:13:07

标签: php wordpress

我希望我的用户注册,自动登录并重定向到用户在标题中抛出的链接,例如www.example.com/register/?redirect_to=/to/some/link&reauth=1

我在\wp-content\plugins\theme-my-login-custom.php

中制作了一个文件

代码如下: -

function auto_login_new_user( $user_id ) {
    wp_set_current_user($user_id);
    wp_set_auth_cookie($user_id);
    if(isset($_REQUEST['redirect_to']))
    {
        wp_redirect( '/'.$_REQUEST['redirect_to'] );
    }
    else
    wp_redirect( '/profile' );
    exit;
}
add_action( 'user_register', 'auto_login_new_user' );

自动登录工作正常,但我无法抓住变量redirect_to 重定向到请求的页面 ..

任何帮助将不胜感激..

-Thanks

1 个答案:

答案 0 :(得分:1)

你应该使用适当的钩子。试试https://codex.wordpress.org/Plugin_API/Filter_Reference/registration_redirect

UPDATE2:

将此行添加到您的注册表单中:

<input type="hidden" name="redirect_to" value="/link/to/location/">

其中value是您的目标网页。