您可以修改标准的WordPress用户注册表,以要求比通常的用户名+电子邮件地址更多的数据。
add_action(\'register_form\',\'my_show_extra_fields\');
add_action(\'register_post\',\'my_check_fields\',10,3);
add_action(\'user_register\',\'my_register_extra_fields\',10,3);
function my_show_extra_fields(){
?>
<style>
#user_first, #user_last, #hzip {
background:none repeat scroll 0 0 #FBFBFB;
border:1px solid #E5E5E5;
font-size:24px;
margin-bottom:16px;
margin-right:6px;
margin-top:2px;
padding:3px;
width:97%;
}
</style>
<p><label>First Name<br/>
<input id="user_first" type="text" tabindex="20" size="25" value="<?php echo $_POST[\'first\']; ?>" name="first"/>
</label></p>
<p><label>Last Name<br/>
<input id="user_last" type="text" tabindex="20" size="25" value="<?php echo $_POST[\'last\']; ?>" name="last"/>
</label></p>
<p><label>Home ZIP Code<br/>
<input id="hzip" type="text" tabindex="20" size="25" maxlength="10" value="<?php echo $_POST[\'hzip\']; ?>" name="hzip"/>
</label></p>
<?php
}
function my_check_fields($login, $email, $errors) {
global $firstname, $lastname, $hzip;
if ($_POST[\'first\'] == \'\') {
$errors->add(\'empty_realname\', "<strong>ERROR</strong>: Please enter first name");
} else {
$firstname = $_POST[\'first\'];
}
if ($_POST[\'last\'] == \'\') {
$errors->add(\'empty_realname\', "<strong>ERROR</strong>: Please enter last name");
} else {
$lastname = $_POST[\'last\'];
}
if ($_POST[\'hzip\'] == \'\') {
$errors->add(\'empty_hzip\', "<strong>ERROR</strong>: Please enter home ZIP Code");
} else {
$hzip = $_POST[\'hzip\'];
}
}
function my_register_extra_fields($user_id, $password="", $meta=array()) {
$userdata = array();
$userdata[\'ID\'] = $user_id;
$userdata[\'first_name\'] = $_POST[\'first\'];
$userdata[\'last_name\'] = $_POST[\'last\'];
wp_update_user($userdata);
update_usermeta( $user_id, \'hzip\', $_POST[\'hzip\'] );
}
add_action( \'show_user_profile\', \'my_show_extra_profile_fields\' );
add_action( \'edit_user_profile\', \'my_show_extra_profile_fields\' );
function my_show_extra_profile_fields( $user ) {
$current_hzip = esc_attr( get_the_author_meta( \'hzip\', $user->ID ) );
?>
<h3>Extra profile information</h3>
<table class="form-table">
<tr>
<th><label for="hzip">Home ZIP Code</label></th>
<td>
<input type="text" name="hzip" id="hzip" size="10" maxlength="10" value="<?php echo $current_hzip ?>" class="regular-text" />
</td>
</tr>
</table>
<?php
}
add_action( \'personal_options_update\', \'my_save_extra_profile_fields\' );
add_action( \'edit_user_profile_update\', \'my_save_extra_profile_fields\' );
function my_save_extra_profile_fields($user_id) {
if ( !current_user_can(\'edit_user\', $user_id) ) {
return false;
}
update_usermeta( $user_id, \'hzip\', $_POST[\'hzip\'] );
}
将上述内容放入
functions.php.
显示可自定义用户列表的一个很棒的插件是AmR User Lists
(Edit: 添加了用户注册内容。)
(Edit 2: 添加了WordPress通常不保存的自定义数据。)