update_blog_option()WP 3.0.0

Updates the option of the specified site in a WordPress Multisite network.

If you need to update the option of the current site, calls update_option(). If another site is specified, switches to it via switch_to_blog(), updates the option, and then returns back via restore_current_blog().

The function is intended for Multisite. However, it will also work in a regular installation similarly to the update_option() function.

No Hooks.

Returns

true|false.

  • true — if the option value was changed.
  • false — if the option value did not change or updating the option failed.

false as a result does not always mean an error. Since update_option() is used internally, the function will also receive false if the new value matches the old one.

Usage

update_blog_option( $id, $option, $value, $deprecated );
$id(int) (required)
ID of the site in a Multisite network.
$option(string) (required)
The name of the option that needs to be updated.
$value(mixed) (required)
New option value. The value can be passed without manual serialization — update_option() will do it itself if needed.
$deprecated(mixed)
Not used. If you pass a value other than null, WordPress will trigger a warning via _deprecated_argument().
Default: null

Examples

0

#1 Updating the site name by ID

Updates the blogname option for the site with ID 3.

$updated = update_blog_option( 3, 'blogname', 'New site' );

if ( $updated ) {
	echo 'Site name updated.';
}
0

#2 Updating a custom option for another site

Saves an array of settings to the option of the specified site.

$settings = [
	'color'   => 'blue',
	'layout'  => 'wide',
	'enabled' => true,
];

update_blog_option( 5, 'my_plugin_settings', $settings );
0

#3 Checking the result considering the unchanged value

Shows that false can mean not an error, but a lack of changes.

$site_id = 2;
$option  = 'blogdescription';
$value   = 'Site description';

$updated = update_blog_option( $site_id, $option, $value );

if ( $updated ) {
	echo 'The option was changed.';
} else {
	echo 'The option was not changed or the update failed.';
}
0

#4 Update Site Title by ID

Updates the blogname option for the site with ID 3.

$updated = update_blog_option( 3, 'blogname', 'New site' );

if ( $updated ) {
	echo 'Site title updated.';
}
0

#5 User Option Update for Another Site

Saves the settings array to the option of the specified site.

$settings = [
	'color'   => 'blue',
	'layout'  => 'wide',
	'enabled' => true,
];

update_blog_option( 5, 'my_plugin_settings', $settings );
0

#6 Check the result, taking into account the unchanged value

It shows that false may mean not an error, but a lack of changes.

$site_id = 2;
$option  = 'blogdescription';
$value   = 'Description of the site';

$updated = update_blog_option( $site_id, $option, $value );

if ( $updated ) {
	echo 'The option was changed.';
} else {
	echo 'The option did not change, or the update failed.';
}

Changelog

Since 3.0.0 Introduced.

update_blog_option() code WP 7.0.2

function update_blog_option( $id, $option, $value, $deprecated = null ) {
	$id = (int) $id;

	if ( null !== $deprecated ) {
		_deprecated_argument( __FUNCTION__, '3.1.0' );
	}

	if ( get_current_blog_id() === $id ) {
		return update_option( $option, $value );
	}

	switch_to_blog( $id );
	$return = update_option( $option, $value );
	restore_current_blog();

	return $return;
}