把以下代码放到主题函数Functions.php里:
<?
add_filter( ‘user_contactmethods’, ‘wpgbc_add_contact_fields’ );
function wpgbc_add_contact_fields( $contactmethods ) {
$contactmethods[‘wx_hm’] = ‘微信号码’;
$contactmethods[‘sj_hm’] = ‘手机号码’;
$contactmethods[‘jz_cs’] = ‘居住城市’;
return $contactmethods;
}
add_action( ‘register_form’, ‘additional_profile_fields’ );
function additional_profile_fields() { ?>
<p>
<label><?php _e(‘微信号码’) ?><br />
<input type=”text” name=”wx_hm” id=”wx_hm” class=”input” size=”25″ tabindex=”20″ /></label>
</p>
<p>
<label><?php _e(‘手机号码’) ?><br />
<input type=”text” name=”sj_hm” id=”sj_hm” class=”input” size=”25″ tabindex=”20″ /></label>
</p>
<p>
<label><?php _e(‘居住城市’) ?><br />
<input type=”text” name=”jz_cs” id=”jz_cs” class=”input” size=”25″ tabindex=”20″ /></label>
</p>
<?php }
// 检测表单字段是否为空,如果为空显示提示信息
add_action( ‘register_post’, ‘add_register_field_validate_wx_hm’, 10, 3 );
function add_register_field_validate_wx_hm( $sanitized_user_login, $user_email, $errors) {
if (!isset($_POST[ ‘wx_hm’ ]) || empty($_POST[ ‘wx_hm’ ])) {
return $errors->add( ‘firstnameempty’, ‘<strong>错误</strong>: 请输入微信号码.’ );
}
}
add_action( ‘register_post’, ‘add_register_field_validate_sj_hm’, 10, 3 );
function add_register_field_validate_sj_hm( $sanitized_user_login, $user_email, $errors) {
if (!isset($_POST[ ‘sj_hm’ ]) || empty($_POST[ ‘sj_hm’ ])) {
return $errors->add( ‘lastnameempty’, ‘<strong>错误</strong>: 请输入手机号码.’ );
}
}
add_action( ‘register_post’, ‘add_register_field_validate_jz_cs’, 10, 3 );
function add_register_field_validate_jz_cs( $sanitized_user_login, $user_email, $errors) {
if (!isset($_POST[ ‘jz_cs’ ]) || empty($_POST[ ‘jz_cs’ ])) {
return $errors->add( ‘lastnameempty’, ‘<strong>错误</strong>: 请输入居住城市.’ );
}
}
// 将用户填写的字段内容保存到数据库中
add_action( ‘user_register’, ‘insert_register_fields’ );
function insert_register_fields( $user_id ) {
$wx_hm = apply_filters(‘pre_user_wx_hm’, $_POST[‘wx_hm’]);
$sj_hm = apply_filters(‘pre_user_sj_hm’, $_POST[‘sj_hm’]);
$jz_cs = apply_filters(‘pre_user_jz_cs’, $_POST[‘jz_cs’]);
// 以下的 ‘first_name’ 和 ‘last_name’ 是“我的个人资料”中已有的字段
update_user_meta( $user_id, ‘wx_hm’, $wx_hm );
update_user_meta( $user_id, ‘sj_hm’, $sj_hm );
update_user_meta( $user_id, ‘jz_cs’, $jz_cs );
}?>