我目前正在尝试为我的Wordpress用户添加一些自定义用户配置文件字段.
我已将以下代码添加到我的functions.php中但由于某种原因输入的数据未保存...
//** CUSTOM USER META **//
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 ) { ?>
<h3>Extra profile information</h3>
<table class="form-table">
<tr>
<th><label for="club">Club You Support</label></th>
<td>
<input type="text" name="club" id="club" value="<?php echo esc_attr( get_the_author_meta( 'club', $user->ID ) ); ?>" class="regular-text" /><br />
</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 ) {
update_usermeta( $user_id, 'club', sanitize_text_field( $_POST['club']) );
}
Run Code Online (Sandbox Code Playgroud)
关于为什么这些数据不坚持的任何想法?
有一种更简单、更正确的方法可以在 Wordpress 中创建新的配置文件字段。根据上面的代码,尝试将以下代码拖放到functions.php主题的文件中:
function my_show_extra_profile_fields {
$user_contact_method['club'] = 'Club You Support';
return $user_contact_method;
}
add_filter( 'user_contactmethods', 'my_show_extra_profile_fields' );
Run Code Online (Sandbox Code Playgroud)
这将自动在您的个人资料页面上创建新字段,并相应地将它们作为用户的自定义字段(元)保存到数据库中。
您可以使用以下方式在您的主题上显示此信息the_author_meta('club');
| 归档时间: |
|
| 查看次数: |
1135 次 |
| 最近记录: |