如果$_Server[‘REQUEST_URI’]为/wp-login.php?check Email=REGISTED,则重定向

时间:2014-03-22 作者:SPi

这个问题扩展了这个问题Redirect to custom url when registration fails?:

他在评论中指出。。

要在成功注册时重定向用户,必须add_action 对于login_head 并检查是否$_SERVER[\'REQUEST_URI\']/wp-login.php?checkemail=registered, 并重定向到正确的。可能有一个名为registration_redirect, 但它不起作用

我正在尝试将其转换为代码,下面是我得到的:

function redirect_after_success() {

  if ($_SERVER[\'REQUEST_URI\']==\'/wp-login.php?checkemail=registered\'){

     $redirect_url = get_bloginfo(\'url\') . \'/register\';
     wp_redirect( $redirect_url );
  exit;
}
}
add_action(\'login_head\',\'redirect_after_success\');
此函数似乎不正确。我仍然被重定向到http://../register/?login=failed 即使注册成功。

以下是处理不完整/无效注册的其他函数:

 // hook failed login
add_action(\'wp_login_failed\', \'my_frontend_login_fail\'); 

function my_frontend_login_fail($username){
    // Get the reffering page, where did the post submission come from?
    $referrer = add_query_arg(\'login\', false, $_SERVER[\'HTTP_REFERER\']);

    // if there\'s a valid referrer, and it\'s not the default log-in screen
    if(!empty($referrer) && !strstr($referrer,\'wp-login\') && !strstr($referrer,\'wp-admin\')){
        // let\'s append some information (login=failed) to the URL for the theme to use
        wp_redirect($referrer . \'?login=failed\'); 
    exit;
    }
}

//hook empty login submit
add_action( \'login_head\', \'my_frontend_login_no_pass_no_username\' );

function my_frontend_login_no_pass_no_username(){
    $referrer = add_query_arg(\'login\', false, $_SERVER[\'HTTP_REFERER\']);
    if ( (!isset($_REQUEST[\'user_login\']) || ( isset( $_REQUEST[\'user_login\'] ) && trim( $_REQUEST[\'user_login\'] ) == \'\' ) ) || (!isset($_REQUEST[\'user_pass\']) || ( isset( $_REQUEST[\'user_pass\'] ) && trim( $_REQUEST[\'user_pass\'] ) == \'\' ) ) ){
        wp_redirect( add_query_arg(\'login\', \'failed\', $referrer) ); 
        exit; 
    }   
}


// unsuccessfull registration
add_action(\'register_post\', \'binda_register_fail_redirect\', 99, 3);

function binda_register_fail_redirect( $sanitized_user_login, $user_email, $errors ){
    //this line is copied from register_new_user function of wp-login.php
    $errors = apply_filters( \'registration_errors\', $errors, $sanitized_user_login, $user_email );
    //this if check is copied from register_new_user function of wp-login.php
    if ( $errors->get_error_code() ){
        //setup your custom URL for redirection
        $redirect_url = get_bloginfo(\'url\') . \'/register\';
        //add error codes to custom redirection URL one by one
        foreach ( $errors->errors as $e => $m ){
            $redirect_url = add_query_arg( $e, \'1\', $redirect_url );    
        }
        //add finally, redirect to your custom page with all errors in attributes
        wp_redirect( $redirect_url );
        exit;   
    }
}

2 个回复
SO网友:skim-

http://codex.wordpress.org/Plugin_API/Action_Reference/user_register

您引用的问题使用了“register\\u post”挂钩,该挂钩就在用户进入数据库之前。如果注册成功,那就是您想要重定向的时候。因此,使用在用户输入数据库后立即触发的钩子。

TL;DR使用“user\\u register”挂钩而不是“login\\u head”

SO网友:Mateusz Hajdziony

您是否尝试使用template_redirect 行动我总是在没有其他动作可以插入时使用它。

add_action( \'template_redirect\', \'wpse138854_register_redirect\' );
function wpse138854_register_redirect() {
    if ( isset( $_GET[\'checkemail\'] ) && \'registered\' == $_GET[\'checkmail\'] ) {
        wp_redirect( get_bloginfo(\'url\') . \'/register\' );
    }
}

结束