Lesson 7 of 9 · 24m
Multisite: Network Activation, Site Switching, and Data Isolation
What multisite actually changes: table prefixes, site options, network admin, activation scope, and the switch_to_blog pitfalls that corrupt data quietly.
Multisite is not just “multiple sites in one install.” It is a different operational topology: shared users, per-site tables for most content, network-wide settings, network activation rules, and code paths where the current site can change under your feet. Plugins that ignore it mostly work in testing and then corrupt or leak data in production networks.
Know which database you are touching
On multisite, core content tables use blog-specific prefixes such as wp_3_posts, while users remain shared in wp_users / wp_usermeta. $wpdb->prefix follows the current blog. $wpdb->base_prefix is the network base prefix. If your custom table is per-site, create it with $wpdb->prefix. If it is truly global, use $wpdb->base_prefix and document that decision.
$table = $wpdb->prefix . 'acme_inventory'; // per site
$network_table = $wpdb->base_prefix . 'acme_network_keys'; // sharedAccidentally mixing these is how tenant A sees tenant B's inventory.
Network activation changes lifecycle semantics
is_network_admin() tells you where the current request is running. is_plugin_active_for_network() tells you how the plugin was activated. Activation hooks receive $network_wide, but that is only a hint about activation intent, not permission to loop over ten thousand sites synchronously.
A safer pattern is to record that a network upgrade is required and process sites in batches:
if ($network_wide && is_multisite()) {
update_site_option('acme_pending_schema_upgrade', ACME_SCHEMA_VERSION);
}Use get_sites() with pagination and number limits. Never load every site object into memory on a large network.
switch_to_blog is a scoped loan, not a new process
switch_to_blog($site_id);
try {
// work using this site's tables, options, and cache context
} finally {
restore_current_blog();
}Everything inside the switch must be treated as temporary. Hooks, globals, object cache keys, and user permissions can all shift in surprising ways. Always restore, including exception paths. In PHP before finally existed, WordPress code often leaked the switched context; the result was content written to the wrong site.
Permissions do not magically become network-global. A super administrator has broad powers, but ordinary users can have different roles per site. current_user_can() inside a switched blog may produce a different answer than outside it.
Options have two layers
get_option()/update_option()— current site.get_site_option()/update_site_option()— network-level, stored inwp_sitemetaon multisite.
Use network options for license keys, network defaults, and centrally managed integrations. Use per-site options for tenant-configurable behavior. If both exist, define precedence clearly: network locked setting, network default, or per-site override. Ambiguity becomes a support ticket.
Network admin UI is a separate surface
Menus registered for network_admin_menu appear in the network dashboard, not individual site dashboards. That is usually where network-wide configuration belongs. Do not expose tenant-level operational controls there unless the action genuinely applies across the network.
Field notes
- Test with subdomain and subdirectory installs if URL generation matters.
- Object cache groups may need
globalor per-blog behavior; wrong cache scoping can leak data across sites. wp multisitecompatibility is a feature operators check before purchase. Say explicitly whether you support it.- On uninstall, provide a WP-CLI command for large networks; web requests are the wrong tool for deleting thousands of per-site tables.
Was this lesson helpful?