Advertisements

update_usermeta

Definition:
function update_usermeta( $user_id, $meta_key, $meta_value ) {}

Update metadata of user.
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.

Parameters

  • int $user_id: User ID
  • string $meta_key: Metadata key.
  • mixed $meta_value: Metadata value.

Return values

returns:True on successful update, false on failure.

Defined actions

  • update_usermeta
    do_action( 'update_usermeta', $cur->umeta_id, $user_id, $meta_key, $meta_value );
  • added_usermeta
    do_action( 'added_usermeta', $wpdb->insert_id, $user_id, $meta_key, $meta_value );
  • updated_usermeta
    do_action( 'updated_usermeta', $cur->umeta_id, $user_id, $meta_key, $meta_value );

Source code

function update_usermeta( $user_id, $meta_key, $meta_value ) {

	_deprecated_function( __FUNCTION__, '3.0', 'update_user_meta()' );

	global $wpdb;

	if ( !is_numeric( $user_id ) )

		return false;

	$meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);



	/** @todo Might need fix because usermeta data is assumed to be already escaped */

	if ( is_string($meta_value) )

		$meta_value = stripslashes($meta_value);

	$meta_value = maybe_serialize($meta_value);



	if (empty($meta_value)) {

		return delete_usermeta($user_id, $meta_key);

	}



	$cur = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s", $user_id, $meta_key) );



	if ( $cur )

		do_action( 'update_usermeta', $cur->umeta_id, $user_id, $meta_key, $meta_value );



	if ( !$cur )

		$wpdb->insert($wpdb->usermeta, compact('user_id', 'meta_key', 'meta_value') );

	else if ( $cur->meta_value != $meta_value )

		$wpdb->update($wpdb->usermeta, compact('meta_value'), compact('user_id', 'meta_key') );

	else

		return false;



	clean_user_cache( $user_id );

	wp_cache_delete( $user_id, 'user_meta' );



	if ( !$cur )

		do_action( 'added_usermeta', $wpdb->insert_id, $user_id, $meta_key, $meta_value );

	else

		do_action( 'updated_usermeta', $cur->umeta_id, $user_id, $meta_key, $meta_value );



	return true;

}

3209

Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: