如何构建具有错误处理功能的自定义登录/注册表单?

时间:2013-11-09 作者:Designer 17

因此,我正在开发一个插件,其中我正在创建一个自定义页面,其中包含一个登录表单和一个用于用户注册的表单;我已经建立并运行了表单,下面是我的代码。

<!-- Begin login form -->               
<form name="loginform" id="login_form" class="login_form" action="<?php echo esc_url( wp_login_url() ); ?>" method="post">

    <p>
        <input type="text" name="log" id="user_login" class="input" placeholder="Username" />
    </p>

    <p>
        <input type="password" name="pwd" id="user_pass" class="input" placeholder="Password" />
    </p>

    <button name="wp-submit" id="wp-submit" class="btn"><?php _e("Sign in", "shorti"); ?></button>

    <input type="hidden" name="redirect_to" value="<?php echo $_SERVER["REQUEST_URI"]; ?>" />

</form>
<!-- end login form -->

<!-- Begin registration form -->
<form method="post" id="register_form" class="wp-user-form" action="<?php echo site_url(\'wp-login.php?action=register\', \'login_post\') ?>">

    <p><!-- Username -->
        <input type="text" name="user_login" id="user_login" class="input" placeholder="username" />
    </p>

    <p><!-- Email to send p/w to -->
        <input type="email" name="user_email" id="user_email" class="input" placeholder="email address" />
    </p>

    <p class="small-text">You will receive an email with a generated password<br />(which you can change in your "user settings")</p>

    <?php do_action(\'register_form\'); ?>
    <button name="wp-submit" id="wp-submit" class="btn"><?php _e("Register!", "shorti"); ?></button>
    <?php $register = $_GET[\'register\']; if($register == true) { echo \'<p>Check your email for the password!</p>\'; } ?>
    <input type="hidden" name="redirect_to" value="<?php echo $_SERVER[\'REQUEST_URI\']; ?>?register=true" />
    <input type="hidden" name="user-cookie" value="1" />

</form><!-- end registration-form -->
这可以很好地工作,除了当出现错误消息时,它会将用户重定向到wp登录。php。我需要有表单的页面来处理AJAX的所有错误;从而提供更好的用户体验。做这件事最好的方法是什么?

2 个回复
SO网友:Clarus Dignus

查看打开的表单元素。action的值确定将表单数据发布到的位置。通过更改值,您可以简单地告诉表单重定向到自身,而不是另一个页面。

《食品法典》提供了另一种生成必要形式标记的方法:

http://codex.wordpress.org/Customizing_the_Login_Form#Make_a_Custom_Login_Page

这里有一个更深入的教程:

http://digwp.com/2010/12/login-register-password-code/

您可以使用或派生一个现有的流行的基于Ajax的登录插件:

http://wordpress.org/plugins/login-with-ajax/

SO网友:Ahmad Awais

这就是我所做的。使用登录表单创建登录https://codex.wordpress.org/Function_Reference/wp_login_form

下面是登录注销重定向和登录错误处理。这将帮助您解决登录问题。

/** Login Redirect
 * Redirect user after successful login.
 *
 * @param string $redirect_to URL to redirect to.
 * @param string $request URL the user is coming from.
 * @param object $user Logged user\'s data.
 * @return string
 */
function my_login_redirect( $redirect_to, $request, $user ) {
    //is there a user to check?
    global $user;
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
        //check for admins
        if ( in_array( \'administrator\', $user->roles ) ) {
            // redirect them to the default place
            return home_url(\'/wp-admin/\');
        } else {
            return home_url();
        }
    } else {
        return $redirect_to;
    }
}

/*Login Error Handle*/
add_action( \'wp_login_failed\', \'aa_login_failed\' ); // hook failed login

function aa_login_failed( $user ) {
    // check what page the login attempt is coming from
    $referrer = $_SERVER[\'HTTP_REFERER\'];

    // check that were not on the default login page
    if ( !empty($referrer) && !strstr($referrer,\'wp-login\') && !strstr($referrer,\'wp-admin\') && $user!=null ) {
        // make sure we don\'t already have a failed login attempt
        if ( !strstr($referrer, \'?login=failed\' )) {
            // Redirect to the login page and append a querystring of login failed
            wp_redirect( $referrer . \'?login=failed\');
        } else {
            wp_redirect( $referrer );
        }

        exit;
    }
}

/*Login Empty Fields Error handling*/
add_action( \'authenticate\', \'pu_blank_login\');

function pu_blank_login( $user ){
    // check what page the login attempt is coming from
    $referrer = $_SERVER[\'HTTP_REFERER\'];

    $error = false;

    if($_POST[\'log\'] == \'\' || $_POST[\'pwd\'] == \'\')
    {
        $error = true;
    }

    // check that were not on the default login page
    if ( !empty($referrer) && !strstr($referrer,\'wp-login\') && !strstr($referrer,\'wp-admin\') && $error ) {

        // make sure we don\'t already have a failed login attempt
        if ( !strstr($referrer, \'?login=failed\') ) {
            // Redirect to the login page and append a querystring of login failed
            wp_redirect( $referrer . \'?login=failed\' );
        } else {
            wp_redirect( $referrer );
        }

    exit;

    }
}

/*Logout Redirect*/
add_filter( \'login_redirect\', \'my_login_redirect\', 10, 3 );

function go_home(){
  wp_redirect( home_url(\'/login/\') );
  exit();
}
add_action(\'wp_logout\',\'go_home\');

结束

相关推荐

在自定义帖子类型下显示帖子的URL和名称的PHP代码

我在我的页脚(循环外)中展示了一篇来自自定义帖子类型的帖子,并使用高级自定义字段插件来实现这一点。这是我正在使用的代码:<h5>Featured Movie</h5> <?php query_posts(array( \'posts_per_page\' => 1, \'post_type\' => \'movies\', \'orderby\' => \'post_date\', \'meta_key\' =>