update_usermeta

Update metadata of user.

Description

update_usermeta( (int) $user_id, (string) $meta_key, (mixed) $meta_value ); 

There is no need to serialize values, they will be serialized if it is needed. The metadata key can only be a string with underscores. All else will be removed.

Will remove the metadata, if the meta value is empty.

Parameters (3)

0. $user_id (int)
The user id.
1. $meta_key (string)
The meta key.
2. $meta_value (mixed)
The meta value.

Usage

  1. if ( !function_exists( 'update_usermeta' ) ) { 
  2. require_once ABSPATH . WPINC . '/deprecated.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. // NOTICE! Understand what this does before running. 
  14. $result = update_usermeta($user_id, $meta_key, $meta_value); 
  15.  

Defined (1)

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

/wp-includes/deprecated.php  
  1. function update_usermeta( $user_id, $meta_key, $meta_value ) { 
  2. _deprecated_function( __FUNCTION__, '3.0.0', 'update_user_meta()' ); 
  3. global $wpdb; 
  4. if ( !is_numeric( $user_id ) ) 
  5. return false; 
  6. $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key); 
  7.  
  8. /** @todo Might need fix because usermeta data is assumed to be already escaped */ 
  9. if ( is_string($meta_value) ) 
  10. $meta_value = stripslashes($meta_value); 
  11. $meta_value = maybe_serialize($meta_value); 
  12.  
  13. if (empty($meta_value)) { 
  14. return delete_usermeta($user_id, $meta_key); 
  15.  
  16. $cur = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s", $user_id, $meta_key) ); 
  17.  
  18. if ( $cur ) 
  19. do_action( 'update_usermeta', $cur->umeta_id, $user_id, $meta_key, $meta_value ); 
  20.  
  21. if ( !$cur ) 
  22. $wpdb->insert($wpdb->usermeta, compact('user_id', 'meta_key', 'meta_value') ); 
  23. elseif ( $cur->meta_value != $meta_value ) 
  24. $wpdb->update($wpdb->usermeta, compact('meta_value'), compact('user_id', 'meta_key') ); 
  25. else 
  26. return false; 
  27.  
  28. clean_user_cache( $user_id ); 
  29. wp_cache_delete( $user_id, 'user_meta' ); 
  30.  
  31. if ( !$cur ) 
  32. do_action( 'added_usermeta', $wpdb->insert_id, $user_id, $meta_key, $meta_value ); 
  33. else 
  34. do_action( 'updated_usermeta', $cur->umeta_id, $user_id, $meta_key, $meta_value ); 
  35.  
  36. return true;