同样,如果您知道如何添加文本字段,您也知道如何添加任何其他表单字段,例如:
显示字段:
//hooks
add_action( \'show_user_profile\', \'Add_user_fields\' );
add_action( \'edit_user_profile\', \'Add_user_fields\' );
function Add_user_fields( $user ) { ?>
<h3>Extra fields</h3>
<table class="form-table">
<tr>
<th><label for="text">text</label></th>
<td>
<?php
// get test saved value
$saved = esc_attr( get_the_author_meta( \'user_text\', $user->ID ) );
?>
<input type="text" name="user_text" id="user_text" value="<?php echo $saved; ?>" class="regular-text" /><br />
<span class="description">Simple text field</span>
</td>
</tr>
<tr>
<th><label for="dropdown">dropdown Select field</label></th>
<td>
<?php
//get dropdown saved value
$selected = get_the_author_meta( \'user_select\', $user->ID ) );
?>
<select name="user_select" id="user_select">
<option value="value1" <?php echo ($selected == "value1")? \'selected="selected"\' : \'\' ?>>Value One label</option>
<option value="value2" <?php echo ($selected == "value2")? \'selected="selected"\' : \'\' ?>>Value Two label</option>
<span class="description">Simple text field</span>
</td>
</tr>
</table>
<?php }
保存字段//挂钩
add_action( \'personal_options_update\', \'save_user_fields\' );
add_action( \'edit_user_profile_update\', \'save_user_fields\' );
function save_user_fields( $user_id ) {
if ( !current_user_can( \'edit_user\', $user_id ) )
return false;
//save text field
update_usermeta( $user_id, \'user_text\', $_POST[\'user_text\'] );
//save dropdown
update_usermeta( $user_id, \'user_select\', $_POST[\'user_select\'] );
}
在这两种情况下,您可以看到,我使用相同的函数将数据存储在数据库中,并从数据库中获取数据,无论它是什么类型的字段。