php - WP 用户注册 - 也可以马上选择他/她的密码

标签 php wordpress registration

这是一个非常简短的前端注册指南,但我在密码方面遇到了一个小问题。

我禁用了在用户注册时发送的带有密码生成的电子邮件:

//Don't Send Notification Email To Registered User
if (!function_exists('wp_new_user_notification')) :
function wp_new_user_notification( $user_id, $notify = '' ) {

//Here's originally password generation + sending email
//Add greeting email later

}
endif;
  • 用户注册后不会收到电子邮件
  • 是的,它是由插件添加的,因为 pluggable.php 只能从插件中覆盖

我的前端注册表单(不要介意缺少“重复密码” - 仅用于测试):

        <?php if( get_option('users_can_register') ) { ?>

                    <form name="registerform" id="registerform" action="<?php echo wp_registration_url(); ?>" method="post">

                        <div class="form-group">
                            <input type="text" name="user_login" id="user_login" class="form-control" placeholder="Username">
                        </div>

                        <div class="form-group">
                            <input type="text" name="user_email" id="user_email" class="form-control" placeholder="Email">
                        </div>

                        <div class="form-group">
                            <input type="password" name="user_pass" id="user_pass" class="form-control" placeholder="Password">
                        </div>  

                        <input type="hidden" name="redirect_to" value="<?php echo site_url(); ?>?user-register=registered">
                        <input type="submit" name="wp-submit-registration" id="wp-submit-registration" value="<?php _e( 'Register', 'tt' ); ?>">
                    </form>

        <?php } ?>

问题:为什么在用户注册时不保存密码?

  • 除密码外其他一切都有效 - 用户注册并插入正确的数据
  • 我是否在某处使用了错误的关键字 (user_pass)?
  • 会不会是原来的wp_registration_url()不能设置密码?
  • 如果是这样,我应该/应该如何使用 wp insert user()?

如果有人提供了如何添加 WP 密码强度指示器的链接,我也会很感激,因为我没有找到任何 tuts。

最佳答案

无论密码与否,您的代码根本不起作用。首先,wp_registration_url返回默认注册表单的 url。对于自定义注册表单,您可以将表单提交到 admin-post.php,并使用自定义操作名称,例如 register_user:

<form name="registerform" id="registerform" action="<?php echo admin_url('admin-post.php?action=register_user'); ?>" method="post">

为了安全起见,我强烈建议您将此添加到您的表单中(它将生成 an hidden input 并带有一个 token 以检查该操作是否已由用户启动:

wp_nonce_field('create-'.$_SERVER['REMOTE_ADDR'], 'user-front', false);

然后在您的 functions.php 文件中使用 admin_post_nopriv_register_user Hook 它。

add_action('admin_post_nopriv_register_user', 'my_register_user');
function my_register_user() {
    // Check the form validity
    if (isset($_POST['user-front']) && wp_verify_nonce($_POST['user-front'], 'create-'.$_SERVER['REMOTE_ADDR'])) {

        // Check the required field
        if (!isset($_POST['user_login']) && !isset($_POST['user_email']) || !isset($_POST['user_pass']) || !isset($_POST['user_confirm_pass']) || !is_email($_POST['user_email'])
            ) {
            wp_redirect(home_url() . '?message=wrong-input');
            exit();
        }

        // Check if both password match
        if ($_POST['user_pass'] != $_POST['user_confirm_pass']) {
            wp_redirect(home_url() . '?message=pass-dif');
            exit();
        }

        // Check if user exists
        if (email_exists($_POST['user_email']) |- username_exists($_POST['user_login']) {
            wp_redirect(home_url() . '?message=already-registered');
            exit();
        }

        // Create the user
        $user_id = wp_create_user($_POST['user_login'], $_POST['user_pass'], $_POST['email_user']);
        $user = new WP_User($user_id);
        $user->set_role('subscriber');

        // Automatic loggin
        $creds = array();
        $creds['user_login'] = $_POST['user_login'];
        $creds['user_password'] = $_POST['user_pass'];
        $creds['remember'] = false;
        $user = wp_signon($creds, false);

        // Redirection
        wp_redirect(home_url('account'));
        exit();
    }
}

这是一个完整的注册过程示例,您可能希望根据您的需要(特别是错误的处理方式)稍微更改一下。

关于php - WP 用户注册 - 也可以马上选择他/她的密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32495899/

相关文章:

php - Laravel 有许多关系无法正常工作

php - 在mysql中按状态值1排序和按名称asc记录排序

php - wordpress 数据库返回 A

php - 如何确认成员(member)注册?

javascript - Node.js 注册表

python - 向 django-userena 表单添加额外字段

php - 我应该使用什么 : inner join or 3 different queries?

php - 将 foreach 与可能不是数组的变量一起使用

php - 在被黑网站中隐藏重定向?

php wordpress 查询