使用正面的注册表成功注册后重定向

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

我正在尝试在注册和登录失败和成功后重定向用户。除了成功注册后的重定向之外,其他一切都很正常。

现在,我被重定向到http://mysite.com/register/?login=failed. 我不明白为什么我没有被重定向到http://mysite/com/login/?redirect_to=mysite.com/change-password 我在底部使用add_action(\'registration_redirect\',\'redirect_after_success_registration\');

这是我的代码:

// 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( add_query_arg(\'login\', \'failed\', $referrer) );         
    //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;   
    }
}

function redirect_after_success_registration () {
$redirect_url=get_bloginfo(\'url\').\'/login/?redirect_to=\'.get_bloginfo(\'url\').\'/change-password\';
return $redirect_url;
}
add_filter(\'registration_redirect\',\'redirect_after_success_registration\');
我非常感谢您在这方面的想法和解决方案!非常感谢。

1 个回复
SO网友:Shawn H

registration\\u redirect是一个过滤器挂钩,而不是一个操作挂钩,因此您的\\u success\\u registration功能实际上从未运行过。

请参见如何使用筛选器重定向的示例:

How to redirect a sucessful registration to a page template?

结束

相关推荐

Front-End Post Submission

我正在尝试添加一个表单,用户可以从前端提交帖子。我正在学习本教程:http://wpshout。com/wordpress从前端提交帖子/我正在做的是添加this code 到我的一个页面模板。表单显示正常,但当我单击“提交”按钮时,它会显示“Page not found error“”许多评论者说这不起作用。谁能给我指出正确的方向吗?代码是否不完整?有缺陷吗?我做错什么了吗?谢谢Towfiq I。