做一个插件ludou-custom-register.php
<?php
/*
Plugin Name: Ludou Custom User Register
Plugin URI: http://www.ludou.org/wordpress-ludou-custom-user-register.html
Description: 修改默认的后台用户注册表单,用户可以自行输入密码,不必用Email接收密码,跳过Email验证。
Version: 2.0.4
Author: Ludou
Author URI: http://www.ludou.org
*/
/**
* 后台注册模块,添加注册表单,修改新用户通知。
*/
if ( !function_exists('wp_new_user_notification') ) :
/**
* Notify the blog admin of a new user, normally via email.
*
* @since 2.0
*
* @param int $user_id User ID
* @param string $plaintext_pass Optional. The user's plaintext password
*/
function wp_new_user_notification($user_id, $plaintext_pass = '', $flag='') {
if(func_num_args() > 1 && $flag !== 1)
return;
$user = new WP_User($user_id);
$user_login = stripslashes($user->user_login);
$user_email = stripslashes($user->user_email);
// The blogname option is escaped with esc_html on the way into the database in sanitize_option
// we want to reverse this for the plain text arena of emails.
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
$message = sprintf(__('New user registration on your site %s:'), $blogname) . "\r\n\r\n";
$message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
$message .= sprintf(__('E-mail: %s'), $user_email) . "\r\n";
@wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), $blogname), $message);
if ( empty($plaintext_pass) )
return;
// 你可以在此修改发送给用户的注册通知Email
$message = sprintf(__('Username: %s'), $user_login) . "\r\n";
$message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n";
$message .= '登陆网址: ' . wp_login_url() . "\r\n";
// sprintf(__('[%s] Your username and password'), $blogname) 为邮件标题
wp_mail($user_email, sprintf(__('[%s] Your username and password'), $blogname), $message);
}
endif;
/* 修改注册表单 */
function ludou_show_password_field() {
// 安全修复:使用 esc_attr 防止 XSS
$user_pass = isset($_POST['user_pass']) ? $_POST['user_pass'] : '';
$user_pass2 = isset($_POST['user_pass2']) ? $_POST['user_pass2'] : '';
?>
<p>
<label>密码(至少6位)<br/>
<input id="user_pwd1" class="input" type="password" tabindex="21" size="25" value="<?php echo esc_attr($user_pass); ?>" name="user_pass"/>
</label>
</p>
<p>
<label>重复密码<br/>
<input id="user_pwd2" class="input" type="password" tabindex="22" size="25" value="<?php echo esc_attr($user_pass2); ?>" name="user_pass2" />
</label>
</p>
<?php
}
/* 处理表单提交的数据 */
function ludou_check_fields($login, $email, $errors) {
global $wpdb;
$last_reg = $wpdb->get_var("SELECT `user_registered` FROM `$wpdb->users` ORDER BY `user_registered` DESC LIMIT 1");
if ( (time() - strtotime($last_reg)) < 60 )
$errors->add('anti_spam', "<strong>错误</strong>:先歇会,稍后再注册,谢谢您的理解");
// 修复:取消注释密码长度检查
if(strlen($_POST['user_pass']) < 6)
$errors->add('password_length', "<strong>错误</strong>:密码长度至少6位");
elseif($_POST['user_pass'] != $_POST['user_pass2'])
$errors->add('password_error', "<strong>错误</strong>:两次输入的密码必须一致");
}
/* 保存表单提交的数据 - 修复版本 */
function ludou_register_extra_fields($user_id, $password="", $meta=array()) {
if (!isset($_POST['user_pass']) || empty($_POST['user_pass'])) {
return;
}
$userdata = array();
$userdata['ID'] = $user_id;
$userdata['user_pass'] = $_POST['user_pass']; // 直接使用用户输入的密码
// 先更新用户密码,再发送通知
$result = wp_update_user($userdata);
if (!is_wp_error($result)) {
// 密码更新成功后再发送通知
wp_new_user_notification($user_id, $_POST['user_pass'], 1);
}
}
function remove_default_password_nag() {
global $user_ID;
delete_user_setting('default_password_nag', $user_ID);
update_user_option($user_ID, 'default_password_nag', false, true);
}
add_action('admin_init', 'remove_default_password_nag');
add_action('register_form','ludou_show_password_field');
add_action('register_post','ludou_check_fields',10,3);
add_action('user_register', 'ludou_register_extra_fields');
// 新增:确保在用户创建时设置密码
add_filter('pre_user_user_pass', 'ludou_pre_user_password', 10, 1);
function ludou_pre_user_password($password) {
if (isset($_POST['user_pass']) && !empty($_POST['user_pass'])) {
return $_POST['user_pass'];
}
return $password;
}
?>