Network / Multisite Functions

$sites = get_sites([
    'number' => 0, // Get all sites
    'network_id' => 1 // Main network
]);
switch_to_blog(2); // Switch to site ID 2
// Perform site-specific operations
restore_current_blog();
if (is_multisite()) {
    // Multisite-specific code
}
wpmu_create_blog(
    'new-site.example.com', // Domain
    '/',                    // Path
    'New Site',             // Title
    get_current_user_id(),  // Admin ID
    ['public' => 1],        // Options
    1                       // Network ID
);
wp_delete_site(123); // Site ID
update_blog_option(2, 'blogdescription', 'New Tagline');
add_user_to_blog(2, $user_id, 'editor');
$user_blogs = get_blogs_of_user(get_current_user_id());
$network_settings = get_network_option(null, 'network_admins');
if (is_super_admin()) {
    // Network admin capabilities
}
add_action('rest_api_init', function() {
    register_rest_route('network/v1', '/stats', [
        'methods' => 'GET',
        'callback' => 'get_network_stats',
        'permission_callback' => 'is_super_admin'
    ]);
});
add_action('rest_api_init', function() {
    register_rest_route('network/v1', '/stats', [
        'methods' => 'GET',
        'callback' => 'get_network_stats',
        'permission_callback' => 'is_super_admin'
    ]);
});
if (ms_is_switched()) {
    // Currently in switched state
}
$network_id = get_current_network_id();
if (current_user_can_for_blog(2, 'edit_posts')) {
    // User has permissions on site ID 2
}

⚠️ Key Considerations

  1. Cache Invalidation
    Always clear cache after switch_to_blog()
  2. Performance
    Batch operations when querying across many sites
  3. Security
    Use current_user_can_for_blog() for permission checks:

For complex operations like site cloning mentioned in the forums 9, consider combining these APIs with wp_insert_post and wpdb class for database operations.