我如何更新用户'个人资料之外的用户元数据?我提供了用于显示的代码


WordPress - How can I update user meta data outside of the user's profile? I've provided the code I'm using to display

使用用户元数据,我正在运行一个函数,该函数隐藏在用户的配置文件中,允许他们拥有一个检查表。我使用主题我的登录,不包括清单上的用户的个人资料页面,当他们编辑/查看他们的个人资料。

我试图在它自己的页面上显示检查表。我可以让它显示,但似乎不能让提交按钮工作。有人能给我指个正确的方向吗?

谢谢!

functions.php中的代码示例:

function my_show_extra_profile_fields( $user ) { ?>
<div><input type="checkbox" name="checkbox1" id=" checkbox1 " value="yes" <?php if (esc_attr( get_the_author_meta( "checkbox1", $user->ID )) == "yes") echo "checked"; ?> />
Checkbox 1
</div>
<button type="submit">Save</button>
<?php }
?>
<?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 ) {
if ( !current_user_can( 'edit_user', $user_id ) )
    return false;
update_usermeta( $user_id, checkbox1, $_POST['checkbox1'] );
}
?>
在我的页面模板上显示额外的配置文件字段的代码示例:
<?php my_show_extra_profile_fields(); ?>

编辑:忘了我的",把它们加回

你有$_POST[checkbox1]它应该是$_POST['checkbox1']

假设您已经创建了该字段,您可以使用codex中的wp_update_user。

$user_id = wp_update_user( array( 'ID' => $user_id, 'checkbox1' => $_POST['checkbox1']  ) );
if ( is_wp_error( $user_id ) ) {
    // There was an error, probably that user doesn't exist.
} else {
    // Success!
}