Wordpress - Difference between update_user_meta and update_user_option
In layman terms there is no major difference! update_user_option()
uses update_user_meta()
internally. The only difference is update_user_option()
prefix the option name with database table prefix + blog ID if you are in multisite and just table prefix if you are in single site installation.
Take a look at the code of update_user_option()
/**
* Update user option with global blog capability.
*
* User options are just like user metadata except that they have support for
* global blog options. If the 'global' parameter is false, which it is by default
* it will prepend the WordPress table prefix to the option name.
*
* Deletes the user option if $newvalue is empty.
*
* @since 2.0.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param int $user_id User ID.
* @param string $option_name User option name.
* @param mixed $newvalue User option value.
* @param bool $global Optional. Whether option name is global or blog specific.
* Default false (blog specific).
* @return int|bool User meta ID if the option didn't exist, true on successful update,
* false on failure.
*/
function update_user_option( $user_id, $option_name, $newvalue, $global = false ) {
global $wpdb;
if ( !$global )
$option_name = $wpdb->get_blog_prefix() . $option_name;
return update_user_meta( $user_id, $option_name, $newvalue );
}
Your option name is prefixed with table prefix + blog ID (Only when ID is other than 1 and 0).
If you set the last parameter $global
to true
it has no difference with update_user_meta()
.
Purpose of update_user_option()
function
Unlike other tables, WordPress does not create separate table for usermeta for each site. It saves user information in one usermeta table for all of the blogs (in multisite). It just prefix the key name for each site with blog prefix
e.g. for blog ID 4 wp_capabilities
is stored as wp_4_capabilities
.
So whatever information you will save using update_user_option()
, for example key_name_abc
will become wp_key_name_abc
for main site in multisite or in single site installation. In future if you convert your single site to multisite the information will be available only in main site.
Use this function when you think some information is depended on site + user as well. Not like name, email etc because these information belongs to user and site independent.
Both write their data in the “usermeta” table. User options stored in the usermeta table retain the wordpress table prefix e.g. wp_ whereas the user meta also stored in the usermeta table doesn't.
User options support blog-specific options, useful in multisite. The user meta is based on the user id specific meta data like profile information.
The parameters are quite different in fact. User option has $user_id, $option_name, $newvalue, $global and user meta has $user_id, $meta_key, $meta_value, $prev_value.
Here are some values for both options and user usermeta.
Options
- wp_user_level
- wp_user-settings
- wp_capabilities
- wp_user-settings-time
User
- first_name
- last_name
- nickname
- rich_editing
- show_admin_bar_front
- admin_color
https://codex.wordpress.org/Function_Reference/update_user_option#Parameters
https://codex.wordpress.org/Function_Reference/update_user_meta#Parameters
The codex pages examples provide real world use.