请知悉:本文最近一次更新为 2年 前,文中内容可能已经过时。

有时我们需要额外给用户资料增加一些其他的资料,比如性别选择框。

那么,具体在用户资料编辑页增加性别选择框如何实现呢?

示例代码如下:

<?php
add_action( 'show_user_profile', 'show_extra_profile_fields' );
add_action( 'edit_user_profile', 'show_extra_profile_fields' );

function show_extra_profile_fields( $user ) { ?>
<h3>Extra profile information</h3>
<table class="form-table">
<tr>
<th><label for="gender">Gender</label></th>
<td>
<select name="gender" id="gender" >
<option value="Male" <?php selected( 'Male', get_the_author_meta( 'gender', $user->ID ) ); ?>>Male</option>
<option value="Female" <?php selected( 'Female', get_the_author_meta( 'gender', $user->ID ) ); ?>>Female</option>
</select>
</td>
</tr>
</table>
<?php }

add_action( 'personal_options_update', 'save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_profile_fields' );

function save_extra_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;

//typo fix
update_user_meta( $user_id, 'gender', $_POST['gender'] );
}
?>

参考资料

Extra profile field as select box?


如您从本文得到了有价值的信息或帮助,请考虑扫描文末二维码捐赠和鼓励。

尊重他人劳动成果。转载请务必附上原文链接,我将感激不尽。


与《WordPress 如何在用户资料页增加选择框》相关的博文:


留言

avatar
😀
😀😁😂😅😭🤭😋😘🤔😰😱🤪💪👍👎🤝🌹👌