The ms_is_switched() WordPress PHP function determines if the switch_to_blog()
function is currently in effect.
Usage
if (ms_is_switched()) { // Your code here }
Parameters
- None
More information
See WordPress Developer Resources: ms_is_switched()
Examples
Check if the blog is switched and display a message
Check if the blog is switched using ms_is_switched() and display a message accordingly.
if (ms_is_switched()) { echo '**Blog is switched**'; } else { echo 'Blog is not switched'; }
Display post count for the current blog and switched blog
Using ms_is_switched(), display the number of published posts for the current blog and switched blog.
$current_blog_id = get_current_blog_id(); $switched_blog_id = 2; // Replace with the target blog ID switch_to_blog($switched_blog_id); if (ms_is_switched()) { $current_post_count = wp_count_posts()->publish; restore_current_blog(); $switched_post_count = wp_count_posts()->publish; echo "Current blog (ID: $current_blog_id) has $current_post_count published posts."; echo "Switched blog (ID: $switched_blog_id) has $switched_post_count published posts."; }
Change blog title if blog is switched
Change the blog title if ms_is_switched() returns true.
if (ms_is_switched()) { update_option('blogname', 'Switched Blog'); }
Retrieve the switched blog’s option
Using ms_is_switched(), retrieve an option from the switched blog.
$switched_blog_id = 3; // Replace with the target blog ID $option_name = 'my_custom_option'; switch_to_blog($switched_blog_id); if (ms_is_switched()) { $switched_blog_option = get_option($option_name); restore_current_blog(); echo "The value of '$option_name' in the switched blog (ID: $switched_blog_id) is: $switched_blog_option"; }
Run a function only on the current blog
Execute a custom function only if ms_is_switched() returns false, meaning the current blog is not switched.
function my_custom_function() { // Your custom function logic here } if (!ms_is_switched()) { my_custom_function(); }