WP user registration - can also choose his/her password right away

帅比萌擦擦* 提交于 2021-02-07 21:39:01

问题


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.


回答1:


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).



来源:https://stackoverflow.com/questions/32495899/wp-user-registration-can-also-choose-his-her-password-right-away

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!