使用用户元数据

介绍

WordPress 的users表格旨在仅包含有关用户的基本信息。

笔记:
从 WP 4.7开始,该表包含:ID、、、、、、、、、和。user_login``user_pass``user_nicename``user_email``user_url``user_registered``user_activation_key``user_status``display_name

因此,为了存储额外的数据,usermeta引入了表,它可以存储关于用户的任意数量的数据。

ID两个表使用基于表中的一对多关系绑定在一起users

操纵用户元数据

有两种主要的方式来操作用户元数据。

  1. 用户个人资料屏幕中的表单字段。
  2. 以编程方式,通过函数调用。

通过表单域

表单字段选项适用于用户可以访问 WordPress 管理区域的情况,他可以在其中查看和编辑配置文件。

在我们深入研究示例之前,了解流程中涉及的挂钩以及它们存在的原因很重要。

show_user_profile

每当用户编辑其自己的用户配置文件时,都会触发此操作挂钩。

**请记住,**没有能力编辑自己的个人资料的用户不会触发此挂钩。

edit_user_profile

只要用户编辑其他人的用户配置文件,就会触发此操作挂钩。

**请记住,**没有能力编辑 3rd 方配置文件的用户不会触发此挂钩。

示例表单域

在下面的示例中,我们将向所有个人资料屏幕添加一个生日字段。在配置文件更新时将其保存到数据库中。

/**
 * The field on the editing screens.
 *
 * @param $user WP_User user object
 */
function wporg_usermeta_form_field_birthday( $user ) {
    ?>
    <h3>It's Your Birthday</h3>
    <table class="form-table">
        <tr>
            <th>
                <label for="birthday">Birthday</label>
            </th>
            <td>
                <input type="date"
                       class="regular-text ltr"
                       id="birthday"
                       name="birthday"
                       value="<?= esc_attr( get_user_meta( $user->ID, 'birthday', true ) ) ?>"
                       title="Please use YYYY-MM-DD as the date format."
                       pattern="(19[0-9][0-9]|20[0-9][0-9])-(1[0-2]|0[1-9])-(3[01]|[21][0-9]|0[1-9])"
                       required>
                <p class="description">
                    Please enter your birthday date.
                </p>
            </td>
        </tr>
    </table>
    <?php
}
 
/**
 * The save action.
 *
 * @param $user_id int the ID of the current user.
 *
 * @return bool Meta ID if the key didn't exist, true on successful update, false on failure.
 */
function wporg_usermeta_form_field_birthday_update( $user_id ) {
    // check that the current user have the capability to edit the $user_id
    if ( ! current_user_can( 'edit_user', $user_id ) ) {
        return false;
    }
 
    // create/update user meta for the $user_id
    return update_user_meta(
        $user_id,
        'birthday',
        $_POST['birthday']
    );
}
 
// Add the field to user's own profile editing screen.
add_action(
    'show_user_profile',
    'wporg_usermeta_form_field_birthday'
);
 
// Add the field to user profile editing screen.
add_action(
    'edit_user_profile',
    'wporg_usermeta_form_field_birthday'
);
 
// Add the save action to user's own profile editing screen update.
add_action(
    'personal_options_update',
    'wporg_usermeta_form_field_birthday_update'
);
 
// Add the save action to user profile editing screen update.
add_action(
    'edit_user_profile_update',
    'wporg_usermeta_form_field_birthday_update'
);

以编程方式

此选项适用于您正在构建自定义用户区域和/或计划禁用对 WordPress 管理区域的访问的情况。

可用于操作用户元数据的函数有:add_user_meta()update_user_meta()和。delete_user_meta()``get_user_meta()

添加

add_user_meta(
    int $user_id,
    string $meta_key,
    mixed $meta_value,
    bool $unique = false
);

add_user_meta()有关所用参数的完整说明,请参阅有关的功能参考。

更新

update_user_meta(
    int $user_id,
    string $meta_key,
    mixed $meta_value,
    mixed $prev_value = ''
);

update_user_meta()有关所用参数的完整说明,请参阅有关的功能参考。

删除

delete_user_meta(
    int $user_id,
    string $meta_key,
    mixed $meta_value = ''
);

delete_user_meta()有关所用参数的完整说明,请参阅有关的功能参考。

得到

get_user_meta(
    int $user_id,
    string $key = '',
    bool $single = false
);

get_user_meta()有关所用参数的完整说明,请参阅有关的功能参考。

请注意,如果您仅传递$user_id,该函数将检索所有元数据作为关联数组。

您可以在插件或主题中的任何位置呈现用户元数据。