update_user_meta()更新一个用户的meta自定义附加信息

目录

描述

Update user meta field based on user ID.

Use the $prev_value parameter to differentiate between meta fields with the same key and user ID.

If the meta field for the user does not exist, it will be added.

update_user_meta() 描述

用法

<?php update_user_meta$user_id$meta_key$meta_value$prev_value ); ?>

update_user_meta() 用法

参数

$user_id

(integer) (必填) User ID.

默认值: None

$meta_key

(string) (必填) The meta_key in the wp_usermeta table for the meta_value to be updated.

默认值: None

$meta_value

(mixed) (必填) The new desired value of the meta_key, which must be different from the existing value. Arrays and objects will be automatically serialized. Note that using objects may cause this bug to popup.

默认值: None

$prev_value

(mixed) (可选) Previous value to check before removing.

默认值: ''

update_user_meta() 参数

返回值

(int/boolean

Meta ID if the key didn't exist; true on successful update; false on failure or if $meta_value is the same as the existing meta value in the database.

update_user_meta() 返回值

示例

Below is an example showing how to update a user's Website profile field

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<?php
$user_id = 1;
$website = 'http://wordpress.org';
update_user_meta($user_id, 'user_url', $website);
?>

Below is an example showing how to check for errors:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

$user_id = 1;
$new_value = 'some new value';

// will return false if the previous value is the same as $new_value
update_user_meta( $user_id, 'some_meta_key', $new_value );

// so check and make sure the stored value matches $new_value
if ( get_user_meta($user_id,  'some_meta_key', true ) != $new_value )
	wp_die('An error occurred');
?>

update_user_meta() 示例

注意

Changes in behavior from the now deprecated update_usermeta:

  • update_user_meta does not delete the meta if the new value is empty.
  • The 动作s are different.

update_user_meta() 注意

历史

添加于 版本: 3.0.0

update_user_meta() 历史

源文件

update_user_meta() 函数的代码位于 wp-includes/user.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Update user meta field based on user ID.
 *
 * Use the $prev_value parameter to differentiate between meta fields with the
 * same key and user ID.
 *
 * If the meta field for the user does not exist, it will be added.
 *
 * @since 3.0.0
 * @link https://codex.wordpress.org/Function_Reference/update_user_meta
 *
 * @param int    $user_id    User ID.
 * @param string $meta_key   Metadata key.
 * @param mixed  $meta_value Metadata value.
 * @param mixed  $prev_value Optional. Previous value to check before removing.
 * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure.
 */
function update_user_meta($user_id, $meta_key, $meta_value, $prev_value = '') {
	return update_metadata('user', $user_id, $meta_key, $meta_value, $prev_value);
}

update_user_meta() 源文件

相关