update_user_option()
Updates the user option (meta-field). Designed for MU mode. A wrapper for update_user_meta(), only adds the database prefix to the meta-field name.
How it works:
- If $global = false (default), adds the database prefix (from wp-config.php) to the meta-field name.
- If $global = true, it is identical to update_user_meta().
Uses: update_user_meta()
No Hooks.
Returns
Int|true|false.
- ID of the meta-field if it did not exist before and was created
- true on successful value update
- false on error.
Usage
update_user_option( $user_id, $option_name, $newvalue, $global );
- $user_id(integer) (required)
- User ID.
- $option_name(string) (required)
- Name of the option.
- $newvalue(mixed) (required)
- Value of the option.
- $global(boolean)
Determines whether the option is global or relates to the local site.
When $global=true it is identical to update_user_meta().
Default: false
Examples
#1 Add option prefix_test_option for user with ID 1
update_user_option( 1, 'test_option', 'my_value' ); // User 1 will have a wp_test_option meta-field with the value my_value // wp_ here is the database prefix
#2 Add option test_option for user with ID 1
update_user_option( 1, 'test_option', 'my_value', true ); // analogue of update_user_meta() - user 1 will have // test_option meta-field with my_value
#3 Hide the admin bar for a user on the front end of the site:
update_user_option( $user_id, 'show_admin_bar_front', false );
When multisite is installed, the $global parameter can be used to set the user option for the whole network, instead of just the current site:
update_user_option( $user_id, 'show_admin_bar_front', false, true );
Notes
- Global. wpdb. $wpdb WordPress database abstraction object.
Changelog
| Since 2.0.0 | Introduced. |
update_user_option() update user option code WP 6.8.3
function update_user_option( $user_id, $option_name, $newvalue, $is_global = false ) {
global $wpdb;
if ( ! $is_global ) {
$option_name = $wpdb->get_blog_prefix() . $option_name;
}
return update_user_meta( $user_id, $option_name, $newvalue );
}