WP 用户注册 - 也可以立即选择他/她的密码 [英] WP user registration - can also choose his/her password right away

查看:56
本文介绍了WP 用户注册 - 也可以立即选择他/她的密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常简短的前端注册指南,但我的密码有一个小问题.

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

//不向注册用户发送通知邮件如果 (!function_exists('wp_new_user_notification')) :函数 wp_new_user_notification( $user_id, $notify = '' ) {//这里原来是密码生成+发送邮件//稍后添加问候邮件}万一;

  • 用户在注册时收不到电子邮件
  • 是的,它是由插件添加的,因为pluggable.php 只能被插件覆盖
<小时>

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

 <?php if( get_option('users_can_register') ) { ?><form name="registerform" id="registerform" action="<?php echo wp_registration_url(); ?>"方法=发布"><div class="form-group"><input type="text" name="user_login" id="user_login" class="form-control" placeholder="用户名">

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

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

<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' ); ?>"></表单><?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'); ?>"方法=发布">

为了安全起见,我强烈建议您在表单中添加它(它会生成隐藏输入用于检查操作是否已由用户发起的令牌:

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

然后在您的 functions.php 文件中使用 admin_post_nopriv_register_user 挂钩.

add_action('admin_post_nopriv_register_user', 'my_register_user');函数 my_register_user() {//检查表单有效性if (isset($_POST['user-front']) && wp_verify_nonce($_POST['user-front'], 'create-'.$_SERVER['REMOTE_ADDR'])) {//检查必填字段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');出口();}//检查两个密码是否匹配如果 ($_POST['user_pass'] != $_POST['user_confirm_pass']) {wp_redirect(home_url() .'?message=pass-dif');出口();}//检查用户是否存在if (email_exists($_POST['user_email']) |- username_exists($_POST['user_login']) {wp_redirect(home_url() .'?message=already-registered');出口();}//创建用户$user_id = wp_create_user($_POST['user_login'], $_POST['user_pass'], $_POST['email_user']);$user = new WP_User($user_id);$user->set_role('订阅者');//自动登录$creds = array();$creds['user_login'] = $_POST['user_login'];$creds['user_password'] = $_POST['user_pass'];$creds['记住'] = false;$user = wp_signon($creds, false);//重定向wp_redirect(home_url('account'));出口();}}

这是一个完整的注册过程示例,您可能希望根据您的需要(特别是如何处理错误)对其进行一些更改.

Here's a very short guide for front-end registration but Im having a small problem with password.

I disabled the email with password generation that gets sent if user registers:

//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;

  • User gets no email when registered
  • Yes, it's added by plugin because pluggable.php can only be overwritten from plugin

My front-end register form (don't mind that "repeat password" is missing - just for testing):

        <?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 } ?>

QUESTION: Why doesn't it save password on user registration?

  • Everything else besides password works - user gets registered & correct data is inserted
  • Do I use wrong keyword somewhere (user_pass)?
  • Could it be that password can't be set with original wp_registration_url()?
  • If that's the case, should/and how should I use wp insert user()?

I would also appriciate if someone threw in a link how to add WP password strength indicator because I didn't find any tuts.

解决方案

Your code shouldn't work at all, password or not. First thing, wp_registration_url return the url of the default registration form. For a custom registration form, you can submit your form to admin-post.php with as custom action name like register_user:

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

For security I highly recommend you to add this in your form (it will generate an hidden input with a token to check that the action has been initiated by the user:

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

Then in your functions.php file you hook on that with admin_post_nopriv_register_user.

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();
    }
}

This is a complete example of a registration process, you may want to change this a bit depending of your needs (specially how the errors are handled).

这篇关于WP 用户注册 - 也可以立即选择他/她的密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆