注册表表单对自定义字段有不同的挂钩。
我希望这个源代码示例对您有所帮助。
/**
* Add custom field to registration form
*/
add_action( \'register_form\', \'fb_show_first_name_field\' );
add_action( \'register_post\', \'fb_check_fields\', 10, 3 );
add_action( \'user_register\', \'fb_register_extra_fields\' );
function fb_show_first_name_field() {
?>
<p>
<label>Twitter<br/>
<input id="twitter" type="text" tabindex="30" size="25" value="<?php echo $_POST[\'twitter\']; ?>" name="twitter" />
</label>
</p>
<?php
}
function fb_check_fields ( $login, $email, $errors ) {
global $twitter;
if ( \'\' === $_POST[\'twitter\'] )
$errors->add( \'empty_realname\', "<strong>ERROR</strong>: Please Enter your twitter handle" );
else
$twitter = $_POST[\'twitter\'];
}
function fb_register_extra_fields ( $user_id, $password = "", $meta = array() ) {
update_user_meta( $user_id, \'twitter\', $_POST[\'twitter\'] );
}
我认为,将字段添加到rpofile页面以更改和查看字段内容也是很有用的。
/**
* Add additional custom field
*/
add_action( \'show_user_profile\', \'fb_show_extra_profile_fields\' );
add_action( \'edit_user_profile\', \'fb_show_extra_profile_fields\' );
function fb_show_extra_profile_fields( $user ) {
?>
<h3>Extra profile information</h3>
<table class="form-table">
<tr>
<th><label for="twitter">Twitter</label></th>
<td>
<input type="text" name="twitter" id="twitter" value="<?php echo esc_attr( get_the_author_meta( \'twitter\', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description">Please enter your Twitter username.</span>
</td>
</tr>
</table>
<?php
}
add_action( \'personal_options_update\', \'fb_save_extra_profile_fields\' );
add_action( \'edit_user_profile_update\', \'fb_save_extra_profile_fields\' );
function fb_save_extra_profile_fields( $user_id ) {
if ( ! current_user_can( \'edit_user\', $user_id ) )
return FALSE;
/* Copy and paste this line for additional fields. Make sure to change \'twitter\' to the field ID. */
update_user_meta( $user_id, \'twitter\', $_POST[\'twitter\'] );
}