If you just want to update that one value, then you can use wp_update_site()
:
wp_update_site( 123, [
'domain' => 'new-slug.domain.com',
] );
But to really update the site URL/address, you’d need to update both the siteurl
and home
options for the site:
switch_to_blog( 123 );
update_option( 'siteurl', 'new URL here' );
update_option( 'home', 'new URL here' );
restore_current_blog();
And here’s a full working example which does both the above mentioned steps:
$main_net = get_network( get_main_network_id() );
$site_id = 123; // ID of the site you're updating
// Update the site domain.
$new_domain = 'enter-slug-here' . '.' . preg_replace( '|^www\.|', '', $main_net->domain );
$site_id2 = wp_update_site( $site_id, [
'domain' => $new_domain,
] );
// And then the site URLs.
if ( $site_id2 && ! is_wp_error( $site_id2 ) ) {
$details = get_site( $site_id );
$new_scheme = parse_url( get_network_option( $site_id, 'siteurl' ), PHP_URL_SCHEME );
$url = $new_scheme . '://' . $new_domain;
switch_to_blog( $site_id );
$url = untrailingslashit( esc_url_raw( $new_scheme . '://' . $details->domain . $details->path ) );
update_option( 'siteurl', $url );
update_option( 'home', $url );
restore_current_blog();
}
Additional Notes
-
Make sure the new domain isn’t a reserved word.Update Apr 04 2020: That’s actually just for sub-directory Multisite installation.
-
Make sure the new domain isn’t an existing username.Update Apr 04 2020: Please ignore that, I just didn’t really notice what
this
is for. Andwp_update_site()
would return an error anyway, if the
domain already exists. 🙂 -
This answer is for subdomain Multisite installation only.