Skip to content
Tutorials Center
My learning

Topics

Lesson 59 of 68 ยท Performance, Tools and Scaling

Multisite Overview

Learn how WordPress Multisite runs many sites from one install: subdomain vs subdirectory, super admins, per-site tables, switch_to_blog and when to use it.

WordPress Multisite lets one installation run many websites. You get a single copy of the WordPress files, a single database and one network dashboard, but each site has its own content, settings, theme choices and URL. Universities with a site per department, news groups with a site per publication, and agencies running many near-identical sites are typical users.

Multisite is a powerful feature with real costs, so the most important skill is knowing when not to use it. This lesson explains the vocabulary, how requests find the right site, where the data lives, and how plugin code works across sites. Setting up a network needs a real server, so the WordPress code is read-only; the runnable examples are plain-PHP stand-ins for the table naming and the site resolution.

Networks and sites

A network is the whole installation: one WordPress codebase and one database. A site (also called a blog in older function names) is one website inside the network. Every site has a numeric ID; the first site, created when you install WordPress, is site 1 and is called the main site.

flowchart TD
    N["Network<br/>one codebase, one database"] --> S1["Site 1<br/>example.com"]
    N --> S2["Site 2<br/>news.example.com"]
    N --> S3["Site 3<br/>shop.example.com"]
    N --> SH[("Shared tables:<br/>users, usermeta, blogs, site, sitemeta")]
    S1 --> T1[("wp_posts, wp_options, ...")]
    S2 --> T2[("wp_2_posts, wp_2_options, ...")]
    S3 --> T3[("wp_3_posts, wp_3_options, ...")]

One shared set of user and network tables, plus a full set of content tables for every site.

Three kinds of people exist in a network:

  • Super Admin: manages the whole network (install plugins and themes, create sites, delete users). The capability is manage_network, and the check is is_super_admin().
  • Site administrators: administer a single site, with the usual Administrator capabilities, limited by network settings. For example, they usually cannot install plugins.
  • Users: accounts are shared across the network, but a user can have a different role on each site. Someone may be an Editor on site 2 and a Subscriber on site 3. The roles are stored per site, as described in Users, Roles and Capabilities.

Subdomains or subdirectories

When you create the network you choose how sites get their address:

Mode Example Notes
Subdomain news.example.com Needs wildcard DNS (*.example.com) and, for HTTPS, a wildcard certificate. Looks like separate sites.
Subdirectory example.com/news/ Simpler DNS and certificate. Needs pretty permalinks. Only available on fresh installs or very new sites.

You cannot cleanly switch modes later, so decide up front. Individual sites can also use their own domain (for example brand-b.com pointing at site 3), which WordPress supports natively with the site's URL settings plus DNS and server configuration.

Enabling Multisite goes through wp-config.php. First you allow it, then the Network Setup screen gives you the constants to paste:

PHP
// Step 1: allow the network setup screen to appear.
define( 'WP_ALLOW_MULTISITE', true );

// Step 2: added after you run Tools > Network Setup.
define( 'MULTISITE', true );
define( 'SUBDOMAIN_INSTALL', true );
define( 'DOMAIN_CURRENT_SITE', 'example.com' );
define( 'PATH_CURRENT_SITE', '/' );
define( 'SITE_ID_CURRENT_SITE', 1 );
define( 'BLOG_ID_CURRENT_SITE', 1 );

Early in the bootstrap (see How WordPress Handles a Request), WordPress compares the incoming host and path with the wp_blogs table to decide which site the request is for. After that, everything runs as if that site were the only one.

The first stand-in does that lookup for both modes. It is a simplified version of the idea: the real code also handles ports, www., redirects and unknown domains.

PHP Which site is this request for?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Where the data lives

All sites share one database, so WordPress separates them by table prefix. The main site keeps the plain prefix (wp_posts, wp_options), and every other site gets its ID in the prefix: site 2 uses wp_2_posts, wp_2_options, wp_2_postmeta, wp_2_comments, wp_2_terms and so on. Creating a site creates a whole set of tables, which is why a network with thousands of sites has an enormous number of tables.

Some tables are shared across the network, with no site ID in their name:

  • wp_users and wp_usermeta (accounts, and each user's role per site as a meta key like wp_2_capabilities)
  • wp_blogs and wp_site/wp_sitemeta (the list of sites, and network-wide settings)
  • wp_signups, wp_registration_log, wp_blog_versions

Uploads are separated too: the main site uses wp-content/uploads/, other sites use wp-content/uploads/sites/<id>/.

You get the right table name from $wpdb->prefix (which changes with the current site) rather than hard-coding wp_. For tables that must be shared, use $wpdb->base_prefix instead.

PHP
global $wpdb;

// Per-site table: follows the current site.
$table = $wpdb->prefix . 'myplugin_log';      // wp_myplugin_log on site 1, wp_2_myplugin_log on site 2

// Network-wide table: always the same.
$shared = $wpdb->base_prefix . 'myplugin_licenses';   // wp_myplugin_licenses

The resolver below implements the naming rule, with a list of shared tables.

PHP A per-site table prefix resolver

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Working across sites: switch_to_blog

Plugins sometimes need data from another site: a network-wide "latest posts" widget, or a report across all sites. switch_to_blog( $id ) points WordPress at another site, so functions like get_option(), WP_Query and get_post() read that site's tables. You must call restore_current_blog() afterwards. Switches are stacked, so every switch needs a matching restore.

PHP
$latest = array();

foreach ( get_sites( array( 'number' => 20, 'public' => 1 ) ) as $site ) {
    switch_to_blog( (int) $site->blog_id );

    $posts = get_posts( array( 'numberposts' => 1, 'post_status' => 'publish' ) );
    if ( $posts ) {
        $latest[] = array(
            'site'  => get_bloginfo( 'name' ),
            'title' => get_the_title( $posts[0] ),
            'url'   => get_permalink( $posts[0] ),
        );
    }

    restore_current_blog(); // always, even if nothing was found
}

Useful companions: get_current_blog_id(), is_multisite(), get_sites() (list sites), get_blog_option() and update_blog_option() (read or write one option without switching), and the site option functions get_site_option() and update_site_option(), which store network-wide settings in wp_sitemeta. is_main_site() and is_network_admin() help you branch behavior.

Be careful with switching: it is slow in loops over hundreds of sites (each switch changes table prefixes and cache groups), it can leave data from the wrong site in memory if you forget to restore, and some plugins misbehave when their data is read from another site. For big networks, cache the aggregated result as shown in Caching and WP-Cron instead of rebuilding it on each request.

The second stand-in implements the stack behavior. It shows which tables a "query" hits at each moment, and what a forgotten restore does.

PHP switch_to_blog and restore_current_blog

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Plugins, themes and updates in a network

  • Network-activated plugins run on every site and are managed by the Super Admin. A plugin's header can say Network: true to force network activation. Plugins can also be activated per site.
  • Themes are installed network-wide by the Super Admin and then enabled for the whole network or for specific sites; each site activates one.
  • Must-use plugins (wp-content/mu-plugins/) load on every site and cannot be deactivated from the dashboard, which makes them useful for network policies.
  • Core, plugin and theme updates happen once for the whole network, but database upgrades run for each site, so a big network needs time for them. WP-CLI helps: wp site list, wp site create --slug=news --title="News", and wp --url=news.example.com plugin list (see WP-CLI).
  • A plugin that creates tables or options on activation must handle both cases: network activation (loop over all sites, or create tables lazily) and single-site activation. This is one of the most common multisite bugs.

When to use Multisite

Multisite is a good fit when the sites are similar and managed by one team: the same theme family, the same set of plugins, shared users, and shared policies. The benefits are one update path, one place to add features, and shared login.

Choose separate installs instead when:

  • Clients or teams need different plugins, PHP versions or complete isolation.
  • Sites have very different traffic: one busy site slows or crashes the rest, since they share a database and PHP workers.
  • You want to move, back up or sell a site independently. Extracting one site from a network is possible but takes work.
  • A security issue in any plugin affects the entire network.

If you are unsure, start with separate installs and manage them with WP-CLI or a management tool. Moving later is easier than untangling a network that should not have been one.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How are the content tables of site 5 named in a standard network with the prefix wp_?

    The main site keeps the plain wp_ prefix. Every other site adds its ID, giving wp_5_posts, wp_5_options and so on.
  2. 2. Which tables are shared by every site in a network?

    User accounts are network-wide, along with the tables that describe the sites and network settings. A user's role on each site is stored in usermeta under a site-specific key.
  3. 3. What must you always do after calling switch_to_blog()?

    Switches are kept on a stack. Without restore_current_blog(), the rest of the request keeps reading and writing the other site's data.
  4. 4. Which is a good reason to avoid Multisite?

    Multisite shares code, database and PHP resources. Sites that need isolation or very different plugin sets are better as separate installations.

Summary

  • A network is one WordPress install; each site inside it has an ID and its own content, settings and URL (subdomain, subdirectory, or its own domain).
  • The Super Admin manages the network; users are shared, with a different role per site.
  • Site 1 uses the plain prefix; other sites use wp_<id>_; users and network tables are shared. Use $wpdb->prefix for per-site tables and $wpdb->base_prefix for shared ones.
  • switch_to_blog() reads another site's data and needs a matching restore_current_blog(); site options live in wp_sitemeta.
  • Use Multisite for many similar sites run by one team, and separate installs when isolation matters.

Next, look at the biggest WordPress plugin of all: WooCommerce Basics.