{转载} www.chinablogs.org/1644/

 

也许是为了防止垃圾评论,WordPress默认的注册页面只有用户名和邮箱,提交之后,WordPress会向你的邮箱里发送了个随机密码,之后你就可以使用这个密码登陆,但有些主机并不支持邮件发送函数,造成用户永远收不到邮件,最终造成注册失败。本文向大家介绍如何在注册页面添加密码输入的技术,同时分享本站为大家制作的插件。

WordPressregpass: {}-爱程序网

如果你是代码控,请按下面步骤折腾:

英文原版技术来源:http://slobodanmanic.com/249/wordpress-set-password-during-registration/

1. 通过register_form action向注册表单添加密码、重复密码和防机器人的验证输入框,防止机器人注册的方法是要求用户填写要注册的网站的名称。

<?php
// Add Password, Repeat Password and Are You Human fields to WordPress registration form
// http://wp.me/p1Ehkq-gn

add_action( 'register_form', 'ts_show_extra_register_fields' );
function ts_show_extra_register_fields(){
?>
	<p>
		<label for="password">Password<br/>
		<input id="password" type="password" tabindex="30" size="25" value="" name="password" />
		</label>
	</p>
	<p>
		<label for="repeat_password">Repeat password<br/>
		<input id="repeat_password" type="password" tabindex="40" size="25" value="" name="repeat_password" />
		</label>
	</p>
	<p>
		<label for="are_you_human" style="font-size:11px">Sorry, but we must check if you are human. What is the name of website you are registering for?<br/>
		<input id="are_you_human" type="text" tabindex="40" size="25" value="" name="are_you_human" />
		</label>
	</p>
<?php
}

// Check the form for errors
add_action( 'register_post', 'ts_check_extra_register_fields', 10, 3 );
function ts_check_extra_register_fields($login, $email, $errors) {
	if ( $_POST['password'] !== $_POST['repeat_password'] ) {
		$errors->add( 'passwords_not_matched', "<strong>ERROR</strong>: Passwords must match" );
	}
	if ( strlen( $_POST['password'] ) < 8 ) {
		$errors->add( 'password_too_short', "<strong>ERROR</strong>: Passwords must be at least eight characters long" );
	}
	if ( $_POST['are_you_human'] !== get_bloginfo( 'name' ) ) {
		$errors->add( 'not_human', "<strong>ERROR</strong>: Your name is Bot? James Bot? Check bellow the form, there's a Back to [sitename] link herbal viagra." );
	}
}
?>

2. 检查用户的输入,两次输入的密码是否一致,是否正确填写网站名称:

// Check the form for errors
add_action( 'register_post', 'ts_check_extra_register_fields', 10, 3 );
function ts_check_extra_register_fields($login, $email, $errors) {
    if ( $_POST['password'] !== $_POST['repeat_password'] ) {
        $errors->add( 'passwords_not_matched', "ERROR: Passwords must match" );
    }
    if ( strlen( $_POST['password'] ) < 8 ) {
        $errors->add( 'password_too_short', "ERROR: Passwords must be at least eight characters long" );
    }
    if ( $_POST['are_you_human'] !== get_bloginfo( 'name' ) ) {
        $errors->add( 'not_human', "ERROR: Your name is Bot? James Bot? Check bellow the form, there's a Back to [sitename] link." );
    }
}

3. 存储用户输入的密码,如果用户没有填写密码,什么也不做,让WordPress自动生成密码。

// Storing WordPress user-selected password into database on registration
// http://wp.me/p1Ehkq-gn
add_action( 'user_register', 'ts_register_extra_fields', 100 );
function ts_register_extra_fields( $user_id ){
    $userdata = array();
    $userdata['ID'] = $user_id;
    if ( $_POST['password'] !== '' ) {
        $userdata['user_pass'] = $_POST['password'];
    }
    $new_user_id = wp_update_user( $userdata );
}

4. 最后就要处理 “A password will be e-mailed to you” 提示:

// Editing WordPress registration confirmation message
// http://wp.me/p1Ehkq-gn
add_filter( 'gettext', 'ts_edit_password_email_text' );
function ts_edit_password_email_text ( $text ) {
    if ( $text == 'A password will be e-mailed to you.' ) {
        $text = 'If you leave password fields empty one will be generated for you. Password must be at least eight characters long.';
    }
    return $text;
}

把上面的四段代码都放入主题目录下的functions.php文件中即可。

为了方便,我们已经把这个文件做成了插件,并做了翻译包。可以直接安装使用,插件只有一个文件,演示请参考本站注册。