update_blog_option()

I was dealing with network DB option filtering few weeks back and I was unable to find a proper way to use update_option, update_site_option or any relevant function to update the main site’s option value.

I just found update_blog_option which is pretty cool and it does the following inside:

[php]


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

[/php]

Since the option filtering is triggered per update option, I had to hack it with recursively switching the blog and resetting it after the second callback:

[php]
public function get_network_trzapi_key( $option ) {
$site_id = get_current_blog_id();

if ( $site_id !== 1 ) {
switch_to_blog( 1 );
$option = get_option( ‘trzapi_key’ );

}
switch_to_blog( $site_id );

return $option;
}
[/php]

Feeling much better now and I can cross out that problem off the list.

2 thoughts on “update_blog_option()”

  1. Judy Alex says: September 13, 2019 at 10:22 pm

    I tried to upload your mention code on the website of design here londonlogodesigns.co.uk but i found these some error while uploading these codes to my blog section. IS anyone is there to guide me that how to get out through these errors.

  2. Mario says: September 14, 2019 at 9:43 pm

    I would not advise you to perform changes on your site/server if you don’t have enough experience. You can easily bring your site down entirely with a single ommission of a semicolon.

    Also, this snippet is of use for a multisite environment, and I presume your website is a single site setup. I suspect you’ll do fine with the basic API functions that WordPress provides out of the box.

Leave a Reply

Your email address will not be published. Required fields are marked *