How to add password fields to a default WordPress registration?

≡放荡痞女 提交于 2020-03-20 06:41:05

问题


I woulrd like to enable users to choose their own passord, but keep a verification email(without reseting password link)?

I am adding an input field for the password in wp-login:

<input type="password" name="user_password" value="" placeholder="Password" id="user_password" class="input" />

Than, I have a custom plugin thats sets user's password:

function myplugin_registration_change_pass( $user_id ) 

{if ( isset( $_POST['user_password'] ) )
    wp_set_password( $_POST['user_password'], $user_id );}

For some reason I keep getting verification email asking the user to set a password. ANy ideas how I can solve this?


回答1:


<form name="registerform" id="registerform" action="<?php echo esc_url( site_url( 'wp-login.php?action=register', 'login_post' ) ); ?>" method="post" novalidate="novalidate">
<p>
    <label for="user_login"><?php _e('*First Name') ?><br />
    <input type="text" name="user_login" id="user_login" class="input" value="<?php echo esc_attr(wp_unslash($user_login)); ?>" size="25" /></label>
</p>
<p>
    <label for="user_login"><?php _e('*Last Name') ?><br />
    <input type="text" name="user_login" id="user_login" class="input" value="<?php echo esc_attr(wp_unslash($user_login)); ?>" size="25" /></label>
</p>


<p>

    <label for="user_login"><?php _e('*Username') ?><br />
    <input type="text" name="user_login" id="user_login" class="input" value="<?php echo esc_attr(wp_unslash($user_login)); ?>" size="25" /></label>
</p>
<p>
    <label for="user_email"><?php _e('*Email address') ?><br />
    <input type="email" name="user_email" id="user_email" class="input" value="<?php echo esc_attr( wp_unslash( $user_email ) ); ?>" size="25" /></label>
</p>

<p>
    <label for="user_login"><?php _e('*Choose a password') ?><br />
    <input type="text" name="user_password" id="user_password" class="input" value="<?php echo esc_attr(wp_unslash($user_login)); ?>" size="25" /></label>
</p>
<p>
    <label for="user_login"><?php _e('*Confirm password') ?><br />
    <input type="text" name="user_password" id="user_password" class="input" value="<?php echo esc_attr(wp_unslash($user_login)); ?>" size="25" /></label>
</p>
<p>
    <input type="checkbox" name="vehicle" value="Bike" onclick="return false" checked >I agree to the terms and conditions<br>
</p>

When you go to wp-login.php at some point you have these fields for user registration. I added first, last name and password.I also created a small plugin that sets the password from the input field:

function myplugin_set_pass( $user_id ) 
{if ( isset( $_POST['user_password'] ) )
wp_set_password( $_POST['user_password'], $user_id );}
add_action('user_register','myplugin_set_pass')

With the line line I am saying that on user registration the function from the plugin should be performed. In other words the password should be set. I am not really sure if that is the right track.




回答2:


Try this code

  function frontend_register(){     

    $gfemail        = $_POST['user_email'];
    $firstname      = $_POST['user_firstname'];
    $lastname       = $_POST['user_lastname'];
    $password       = $_POST['user_password'];      


    if (email_exists( $gfemail ) === false) {

      $password             =   esc_attr( $password );
      $email                =   sanitize_email( $gfemail );
      $first_name               =   sanitize_text_field( $firstname );
      $last_name                =   sanitize_text_field( $lastname );

      $userdata = array(
          'user_login'    =>        $email,
          'user_email'    =>    $email,
          'user_pass'     =>    $password,
          'role'                =>   'subscriber',
          'first_name'    =>    $first_name,
          'last_name'     =>    $last_name,
          );
      $new_user = wp_insert_user ( $userdata );       

      if(!is_wp_error($new_user)){          

        $headers = 'Content-type: text/html;charset=utf-8' . "\r\n";
        $headers .= 'From: Your SiteName <test@test.com>' . "\r\n";

        $verifyCode = sha1($new_user.$email);

        update_user_meta( $new_user, 'email_confirmation_done', 'No');
        update_user_meta( $new_user, 'email_confirmation_code', $verifyCode);

        $subject = 'Please verify your account' . "\r\n";       

        $mkUrl = sprintf('<a href="%s?verifycode=%s&user_login=%s">click here</a>', $wpgsloginpage, $verifyCode, $email);
        $message = sprintf('<p>Dear %s,</p><br> ', $first_name);
        $message .= sprintf('<hr>');
        $message .= sprintf('<p><strong>Please verify your account by clicking on this link %s </strong></p>', $mkUrl);
        $message .= sprintf('<hr>');            
        $message .= sprintf('<br><p style="font-size:12.8px">Thank you for registering<br><br>');
        wp_mail($email, $subject, $message, $headers);  
        die("success");
      }
    }else{
        die("failure");
    }       

}


if ( ! function_exists( 'wp_new_user_notification' ) ) :
function wp_new_user_notification( $user_id, $deprecated = null, $notify = '' ) {
    return;
}
endif;


来源:https://stackoverflow.com/questions/47258956/how-to-add-password-fields-to-a-default-wordpress-registration

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