Difference between update_user_meta and update_user_option

Can anyone explain what is the difference between update_user_meta and update_user_option and in which scenarios the both can be used?


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.

Leave a Comment