wordpress 自定义注册界面

 

首先在functions.php中建立如下函数

/**
* start of custom registration module fucntion
*
**/
if (!isset($_SESSION)) {
 	session_start();
	session_regenerate_id(TRUE);
}

/**
 *Admin registration module,to add a registration form and to update the notification to the users
 */
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;

	// you can change the format in the Email
	$message  = sprintf(__('Username: %s'), $user_login) . "\r\n";
	$message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n";
	$message .= 'The login url is : ' . wp_login_url() . "\r\n";

	// sprintf(__('[%s] Your username and password'), $blogname) is the email subject
	wp_mail($user_email, sprintf(__('[%s] Your username and password'), $blogname), $message);
}
endif;

/* Update the registration form */
function ludou_show_password_field() {
	define('LCR_PLUGIN_URL', plugin_dir_url( __FILE__ ));
?>
<p>
	<label>Password(at least 6 characters )<br/>
		<input id="user_pwd1" class="input" type="password" tabindex="31" size="20" value="<?php echo $_POST['user_pass']; ?>" name="user_pass"/>
	</label>
</p>
<p>
	<label>Confirm Password<br/>
		<input id="user_pwd2" class="input" type="password" tabindex="32" size="20" value="<?php echo $_POST['user_pass2']; ?>" name="user_pass2" />
	</label>
</p>
<p style="margin:0 0 10px;">
   <label>Choose your role:
		<select name="user_role" tabindex="33" id="user_role">
			<option value="">Select your role</option>
			<option value="year7" <?php if($_POST['user_role'] == 'year7') echo 'selected="selected"';?>>Year 7</option>
			<option value="year8" <?php if($_POST['user_role'] == 'year8') echo 'selected="selected"';?>>Year 8</option>
			<option value="year9" <?php if($_POST['user_role'] == 'year9') echo 'selected="selected"';?>>Year 9</option>
			<option value="year10" <?php if($_POST['user_role'] == 'year10') echo 'selected="selected"';?>>Year 10</option>
			<option value="year11" <?php if($_POST['user_role'] == 'year11') echo 'selected="selected"';?>>Year 11</option>
			<option value="year12" <?php if($_POST['user_role'] == 'year12') echo 'selected="selected"';?>>Year 12</option>
		</select>
	</label>
	<br />
</p>

<?php
}

/* to handle with the submitted data */
function ludou_check_fields($login, $email, $errors) {	 
  if(strlen($_POST['user_pass']) < 6)
     $errors->add('password_length', "<strong>ERROR</strong>: Password should be at least 6 characters");
  elseif($_POST['user_pass'] != $_POST['user_pass2'])
     $errors->add('password_error', "<strong>ERROR</strong>: Please check your confirm password");

   if($_POST['user_role'] != 'year7' && $_POST['user_role'] != 'year8' && $_POST['user_role'] != 'year9' && $_POST['user_role'] != 'year10' && $_POST['user_role'] != 'year11' && $_POST['user_role'] != 'year12')
   $errors->add('role_error', "<strong>ERROR</strong>: Please choose one correct role");
}

/* TO save the submitted data */
function ludou_register_extra_fields($user_id, $password="", $meta=array()) {
	$userdata = array();
	$userdata['ID'] = $user_id;
	$userdata['user_pass'] = $_POST['user_pass'];
	$userdata['role'] = $_POST['user_role'];

	wp_new_user_notification( $user_id, $_POST['user_pass'], 1 );
	wp_update_user($userdata);
}

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');

//end of custom registration module fucntion

 

然后下载如下插件

SI CAPTCHA Anti-Spam

 

设置生效后 就可以得到你想要的结果了

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值