You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress新用户注册:如何通过邮件自动生成密码?

WordPress New User Registration: Auto-Generate Password via Email

Hey there! Let's walk through both the core details of auto-generating passwords for new registrations, and the step-by-step implementation for your specific flow (user enters email → WordPress sends auto-generated password).


Core Notes on Auto-Generated Passwords

  • Security First: WordPress's built-in wp_generate_password() creates strong, random passwords (mix of letters, numbers, and special characters) which are far more secure than most user-chosen weak passwords.
  • Simplified Registration: Cutting out the password field removes friction for new users—they only need to provide their email to get started. They can always change the password to something personal once they log in.
  • Email Reliability: Make sure your WordPress email setup is solid (use SMTP instead of the default mail function) to avoid these password emails ending up in spam folders.

Step-by-Step Implementation (Matching Your Flow)

1. Modify the Registration Form to Only Ask for Email

First, we'll strip out the default username and password fields from the registration form, leaving just the email input. Add this code to your theme's functions.php file (or a custom plugin, which is safer for theme updates):

// Remove default registration form fields
function custom_remove_default_reg_fields() {
    remove_action( 'register_form', 'wp_registration_form' );
}
add_action( 'init', 'custom_remove_default_reg_fields' );

// Custom registration form with only email field
function custom_registration_form() {
    ?>
    <form action="<?php echo esc_url( site_url( 'wp-login.php?action=register', 'login_post' ) ); ?>" method="post">
        <p>
            <label for="user_email">Email<br />
            <input type="email" name="user_email" id="user_email" class="input" value="<?php echo esc_attr( $_POST['user_email'] ?? '' ); ?>" size="25" required /></label>
        </p>
        <?php do_action( 'register_form' ); ?>
        <p id="reg_passmail">A secure password will be sent to your email address immediately.</p>
        <p class="submit"><input type="submit" name="wp-submit" id="wp-submit" class="button button-primary button-large" value="Register" /></p>
    </form>
    <?php
}
add_action( 'register_form', 'custom_registration_form' );

2. Handle Registration: Generate Password, Create User, Send Email

Next, we'll override the default registration logic to auto-generate a password, create the user account, and fire off the password email. Add this right after the previous code:

// Custom registration handler
function custom_handle_registration() {
    if ( isset( $_POST['user_email'] ) && !empty( $_POST['user_email'] ) ) {
        $email = sanitize_email( $_POST['user_email'] );
        
        // Check if email is already registered
        if ( email_exists( $email ) ) {
            wp_die( 'This email address is already linked to an account.' );
        }
        
        // Generate a strong 12-character password
        $password = wp_generate_password( 12, true, true );
        
        // Create a username from the email prefix (add random numbers if duplicate)
        $username = explode( '@', $email )[0];
        while ( username_exists( $username ) ) {
            $username .= rand( 100, 999 );
        }
        
        // Create the user account
        $user_id = wp_create_user( $username, $password, $email );
        
        // Handle user creation errors
        if ( is_wp_error( $user_id ) ) {
            wp_die( $user_id->get_error_message() );
        }
        
        // Send the password email
        $site_name = get_bloginfo( 'name' );
        $login_url = wp_login_url();
        
        $subject = "Your $site_name Account Details";
        $message = "Hi $username,

Your account on $site_name has been created successfully! Here's how to log in:

Username: $username
Password: $password

Log in here: $login_url

Once you're logged in, you can change your password to something you prefer in your profile settings.

Cheers,
The $site_name Team";
        
        wp_mail( $email, $subject, $message );
        
        // Redirect to login page with success notice
        wp_redirect( wp_login_url() . '?checkemail=registered' );
        exit;
    }
}
add_action( 'login_form_register', 'custom_handle_registration' );

Quick Tips for Smooth Operation

  • SMTP Setup: Use an SMTP plugin (or add custom code) to configure WordPress to send emails via a reliable service like Gmail, SendGrid, or Mailgun—this prevents password emails from being marked as spam.
  • Customize Messages: Tweak the email subject, body, and form text to match your site's brand voice.
  • Password Length: Adjust the 12 in wp_generate_password(12, true, true) to make passwords longer or shorter if needed.
  • Theme Safety: If you're using a custom theme, adding code to a child theme is better than the parent theme—so your changes won't get overwritten when the theme updates.

内容的提问来源于stack exchange,提问作者Leon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:25:57