update_user_meta

Update user meta field based on user ID.

Description

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

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.

Returns (int|bool)

Meta ID if the key didn't exist, true on successful update, false on failure.

Parameters (4)

0. $user_id (int)
The user id.
1. $meta_key (string)
The meta key.
2. $meta_value (mixed)
The meta value.
3. $prev_value — Optional. (string) => ''
Previous value to check before removing.

Usage

  1. if ( !function_exists( 'update_user_meta' ) ) { 
  2. require_once ABSPATH . WPINC . '/user.php'; 
  3.  
  4. // The user id. 
  5. $user_id = -1; 
  6.  
  7. // The meta key. 
  8. $meta_key = ''; 
  9.  
  10. // The meta value. 
  11. $meta_value = null; 
  12.  
  13. // Optional. Previous value to check before removing. 
  14. $prev_value = ''; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = update_user_meta($user_id, $meta_key, $meta_value, $prev_value); 
  18.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/user.php  
  1. function update_user_meta($user_id, $meta_key, $meta_value, $prev_value = '') { 
  2. return update_metadata('user', $user_id, $meta_key, $meta_value, $prev_value);