How to update_site_option for specific site within network?

I would like to add or update site options (like those shown in the screenshot) using update_site_option, but I have no idea (1) how to choose the correct site by its id? And where do I find the available (2) slugs for the options array?

site options

1 Answer
1

update_site_option() updates an option that’s set for the entire network. If you’re trying to update a specific site’s option, eg blogname, you’ll need to do update_option() instead.

If you’re not sure of a site’s ID, you can get its details using the site’s slug with get_blog_details().

For example, if I wanted to change the admin_email and some_other_option options of the site at example.com/site-3:

$site_object = get_blog_details( 'site-3' );
if ( ! empty( $site_object ) ) {
    switch_to_blog( $site_object->blog_id );
    update_option( 'admin_email', '[email protected]' );
    update_option( 'some_other_option', 'Some Other Option Value' );
    restore_current_blog();
}

The confusion arises because when WordPress Multisite was initially developed, the terminology spoke of a site of blogs; later, though, it was updated to be a network of sites. The original terms still exist in function names like update_site_option() and switch_to_blog().

References

  • update_site_option()
  • update_option()
  • get_blog_details()
  • switch_to_blog()
  • restore_current_blog()

Leave a Comment