3 * These functions are needed to load Multisite.
8 * @subpackage Multisite
12 * Whether a subdomain configuration is enabled.
16 * @return bool True if subdomain configuration is enabled, false otherwise.
18 function is_subdomain_install() {
19 if ( defined('SUBDOMAIN_INSTALL') )
20 return SUBDOMAIN_INSTALL;
22 return ( defined( 'VHOST' ) && VHOST == 'yes' );
26 * Returns array of network plugin files to be included in global scope.
28 * The default directory is wp-content/plugins. To change the default directory
29 * manually, define `WP_PLUGIN_DIR` and `WP_PLUGIN_URL` in `wp-config.php`.
34 * @return array Files to include.
36 function wp_get_active_network_plugins() {
37 $active_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
38 if ( empty( $active_plugins ) )
42 $active_plugins = array_keys( $active_plugins );
43 sort( $active_plugins );
45 foreach ( $active_plugins as $plugin ) {
46 if ( ! validate_file( $plugin ) // $plugin must validate as file
47 && '.php' == substr( $plugin, -4 ) // $plugin must end with '.php'
48 && file_exists( WP_PLUGIN_DIR . '/' . $plugin ) // $plugin must exist
50 $plugins[] = WP_PLUGIN_DIR . '/' . $plugin;
56 * Checks status of current blog.
58 * Checks if the blog is deleted, inactive, archived, or spammed.
60 * Dies with a default message if the blog does not pass the check.
62 * To change the default message when a blog does not pass the check,
63 * use the wp-content/blog-deleted.php, blog-inactive.php and
64 * blog-suspended.php drop-ins.
68 * @return true|string Returns true on success, or drop-in file to include.
70 function ms_site_check() {
73 * Filters checking the status of the current blog.
77 * @param bool null Whether to skip the blog status check. Default null.
79 $check = apply_filters( 'ms_site_check', null );
80 if ( null !== $check )
83 // Allow super admins to see blocked sites
84 if ( is_super_admin() )
89 if ( '1' == $blog->deleted ) {
90 if ( file_exists( WP_CONTENT_DIR . '/blog-deleted.php' ) )
91 return WP_CONTENT_DIR . '/blog-deleted.php';
93 wp_die( __( 'This site is no longer available.' ), '', array( 'response' => 410 ) );
96 if ( '2' == $blog->deleted ) {
97 if ( file_exists( WP_CONTENT_DIR . '/blog-inactive.php' ) ) {
98 return WP_CONTENT_DIR . '/blog-inactive.php';
100 $admin_email = str_replace( '@', ' AT ', get_site_option( 'admin_email', 'support@' . get_network()->domain ) );
102 /* translators: %s: admin email link */
103 sprintf( __( 'This site has not been activated yet. If you are having problems activating your site, please contact %s.' ),
104 sprintf( '<a href="mailto:%s">%s</a>', $admin_email )
110 if ( $blog->archived == '1' || $blog->spam == '1' ) {
111 if ( file_exists( WP_CONTENT_DIR . '/blog-suspended.php' ) )
112 return WP_CONTENT_DIR . '/blog-suspended.php';
114 wp_die( __( 'This site has been archived or suspended.' ), '', array( 'response' => 410 ) );
121 * Retrieve the closest matching network for a domain and path.
125 * @internal In 4.4.0, converted to a wrapper for WP_Network::get_by_path()
127 * @param string $domain Domain to check.
128 * @param string $path Path to check.
129 * @param int|null $segments Path segments to use. Defaults to null, or the full path.
130 * @return WP_Network|false Network object if successful. False when no network is found.
132 function get_network_by_path( $domain, $path, $segments = null ) {
133 return WP_Network::get_by_path( $domain, $path, $segments );
137 * Retrieves the closest matching site object by its domain and path.
139 * This will not necessarily return an exact match for a domain and path. Instead, it
140 * breaks the domain and path into pieces that are then used to match the closest
141 * possibility from a query.
143 * The intent of this method is to match a site object during bootstrap for a
144 * requested site address
147 * @since 4.7.0 Updated to always return a `WP_Site` object.
149 * @global wpdb $wpdb WordPress database abstraction object.
151 * @param string $domain Domain to check.
152 * @param string $path Path to check.
153 * @param int|null $segments Path segments to use. Defaults to null, or the full path.
154 * @return WP_Site|false Site object if successful. False when no site is found.
156 function get_site_by_path( $domain, $path, $segments = null ) {
157 $path_segments = array_filter( explode( '/', trim( $path, '/' ) ) );
160 * Filters the number of path segments to consider when searching for a site.
164 * @param int|null $segments The number of path segments to consider. WordPress by default looks at
165 * one path segment following the network path. The function default of
166 * null only makes sense when you know the requested path should match a site.
167 * @param string $domain The requested domain.
168 * @param string $path The requested path, in full.
170 $segments = apply_filters( 'site_by_path_segments_count', $segments, $domain, $path );
172 if ( null !== $segments && count( $path_segments ) > $segments ) {
173 $path_segments = array_slice( $path_segments, 0, $segments );
178 while ( count( $path_segments ) ) {
179 $paths[] = '/' . implode( '/', $path_segments ) . '/';
180 array_pop( $path_segments );
186 * Determine a site by its domain and path.
188 * This allows one to short-circuit the default logic, perhaps by
189 * replacing it with a routine that is more optimal for your setup.
191 * Return null to avoid the short-circuit. Return false if no site
192 * can be found at the requested domain and path. Otherwise, return
197 * @param null|bool|WP_Site $site Site value to return by path.
198 * @param string $domain The requested domain.
199 * @param string $path The requested path, in full.
200 * @param int|null $segments The suggested number of paths to consult.
201 * Default null, meaning the entire path was to be consulted.
202 * @param array $paths The paths to search for, based on $path and $segments.
204 $pre = apply_filters( 'pre_get_site_by_path', null, $domain, $path, $segments, $paths );
205 if ( null !== $pre ) {
206 if ( false !== $pre && ! $pre instanceof WP_Site ) {
207 $pre = new WP_Site( $pre );
214 * caching, etc. Consider alternative optimization routes,
215 * perhaps as an opt-in for plugins, rather than using the pre_* filter.
216 * For example: The segments filter can expand or ignore paths.
217 * If persistent caching is enabled, we could query the DB for a path <> '/'
218 * then cache whether we can just always ignore paths.
221 // Either www or non-www is supported, not both. If a www domain is requested,
222 // query for both to provide the proper redirect.
223 $domains = array( $domain );
224 if ( 'www.' === substr( $domain, 0, 4 ) ) {
225 $domains[] = substr( $domain, 4 );
229 'domain__in' => $domains,
230 'path__in' => $paths,
234 if ( count( $domains ) > 1 ) {
235 $args['orderby']['domain_length'] = 'DESC';
238 if ( count( $paths ) > 1 ) {
239 $args['orderby']['path_length'] = 'DESC';
242 $result = get_sites( $args );
243 $site = array_shift( $result );
253 * Identifies the network and site of a requested domain and path and populates the
254 * corresponding network and site global objects as part of the multisite bootstrap process.
256 * Prior to 4.6.0, this was a procedural block in `ms-settings.php`. It was wrapped into
257 * a function to facilitate unit tests. It should not be used outside of core.
259 * Usually, it's easier to query the site first, which then declares its network.
260 * In limited situations, we either can or must find the network first.
262 * If a network and site are found, a `true` response will be returned so that the
263 * request can continue.
265 * If neither a network or site is found, `false` or a URL string will be returned
266 * so that either an error can be shown or a redirect can occur.
271 * @global wpdb $wpdb WordPress database abstraction object.
272 * @global WP_Network $current_site The current network.
273 * @global WP_Site $current_blog The current site.
275 * @param string $domain The requested domain.
276 * @param string $path The requested path.
277 * @param bool $subdomain Optional. Whether a subdomain (true) or subdirectory (false) configuration.
279 * @return bool|string True if bootstrap successfully populated `$current_blog` and `$current_site`.
280 * False if bootstrap could not be properly completed.
281 * Redirect URL if parts exist, but the request as a whole can not be fulfilled.
283 function ms_load_current_site_and_network( $domain, $path, $subdomain = false ) {
284 global $wpdb, $current_site, $current_blog;
286 // If the network is defined in wp-config.php, we can simply use that.
287 if ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) ) {
288 $current_site = new stdClass;
289 $current_site->id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : 1;
290 $current_site->domain = DOMAIN_CURRENT_SITE;
291 $current_site->path = PATH_CURRENT_SITE;
292 if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
293 $current_site->blog_id = BLOG_ID_CURRENT_SITE;
294 } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated.
295 $current_site->blog_id = BLOGID_CURRENT_SITE;
298 if ( 0 === strcasecmp( $current_site->domain, $domain ) && 0 === strcasecmp( $current_site->path, $path ) ) {
299 $current_blog = get_site_by_path( $domain, $path );
300 } elseif ( '/' !== $current_site->path && 0 === strcasecmp( $current_site->domain, $domain ) && 0 === stripos( $path, $current_site->path ) ) {
301 // If the current network has a path and also matches the domain and path of the request,
302 // we need to look for a site using the first path segment following the network's path.
303 $current_blog = get_site_by_path( $domain, $path, 1 + count( explode( '/', trim( $current_site->path, '/' ) ) ) );
305 // Otherwise, use the first path segment (as usual).
306 $current_blog = get_site_by_path( $domain, $path, 1 );
309 } elseif ( ! $subdomain ) {
311 * A "subdomain" install can be re-interpreted to mean "can support any domain".
312 * If we're not dealing with one of these installs, then the important part is determining
313 * the network first, because we need the network's path to identify any sites.
315 if ( ! $current_site = wp_cache_get( 'current_network', 'site-options' ) ) {
316 // Are there even two networks installed?
317 $one_network = $wpdb->get_row( "SELECT * FROM $wpdb->site LIMIT 2" ); // [sic]
318 if ( 1 === $wpdb->num_rows ) {
319 $current_site = new WP_Network( $one_network );
320 wp_cache_add( 'current_network', $current_site, 'site-options' );
321 } elseif ( 0 === $wpdb->num_rows ) {
322 // A network not found hook should fire here.
327 if ( empty( $current_site ) ) {
328 $current_site = WP_Network::get_by_path( $domain, $path, 1 );
331 if ( empty( $current_site ) ) {
333 * Fires when a network cannot be found based on the requested domain and path.
335 * At the time of this action, the only recourse is to redirect somewhere
336 * and exit. If you want to declare a particular network, do so earlier.
340 * @param string $domain The domain used to search for a network.
341 * @param string $path The path used to search for a path.
343 do_action( 'ms_network_not_found', $domain, $path );
346 } elseif ( $path === $current_site->path ) {
347 $current_blog = get_site_by_path( $domain, $path );
349 // Search the network path + one more path segment (on top of the network path).
350 $current_blog = get_site_by_path( $domain, $path, substr_count( $current_site->path, '/' ) );
353 // Find the site by the domain and at most the first path segment.
354 $current_blog = get_site_by_path( $domain, $path, 1 );
355 if ( $current_blog ) {
356 $current_site = WP_Network::get_instance( $current_blog->site_id ? $current_blog->site_id : 1 );
358 // If you don't have a site with the same domain/path as a network, you're pretty screwed, but:
359 $current_site = WP_Network::get_by_path( $domain, $path, 1 );
363 // The network declared by the site trumps any constants.
364 if ( $current_blog && $current_blog->site_id != $current_site->id ) {
365 $current_site = WP_Network::get_instance( $current_blog->site_id );
368 // No network has been found, bail.
369 if ( empty( $current_site ) ) {
370 /** This action is documented in wp-includes/ms-settings.php */
371 do_action( 'ms_network_not_found', $domain, $path );
376 // During activation of a new subdomain, the requested site does not yet exist.
377 if ( empty( $current_blog ) && wp_installing() ) {
378 $current_blog = new stdClass;
379 $current_blog->blog_id = $blog_id = 1;
380 $current_blog->public = 1;
383 // No site has been found, bail.
384 if ( empty( $current_blog ) ) {
385 // We're going to redirect to the network URL, with some possible modifications.
386 $scheme = is_ssl() ? 'https' : 'http';
387 $destination = "$scheme://{$current_site->domain}{$current_site->path}";
390 * Fires when a network can be determined but a site cannot.
392 * At the time of this action, the only recourse is to redirect somewhere
393 * and exit. If you want to declare a particular site, do so earlier.
397 * @param object $current_site The network that had been determined.
398 * @param string $domain The domain used to search for a site.
399 * @param string $path The path used to search for a site.
401 do_action( 'ms_site_not_found', $current_site, $domain, $path );
403 if ( $subdomain && ! defined( 'NOBLOGREDIRECT' ) ) {
404 // For a "subdomain" install, redirect to the signup form specifically.
405 $destination .= 'wp-signup.php?new=' . str_replace( '.' . $current_site->domain, '', $domain );
406 } elseif ( $subdomain ) {
407 // For a "subdomain" install, the NOBLOGREDIRECT constant
408 // can be used to avoid a redirect to the signup form.
409 // Using the ms_site_not_found action is preferred to the constant.
410 if ( '%siteurl%' !== NOBLOGREDIRECT ) {
411 $destination = NOBLOGREDIRECT;
413 } elseif ( 0 === strcasecmp( $current_site->domain, $domain ) ) {
415 * If the domain we were searching for matches the network's domain,
416 * it's no use redirecting back to ourselves -- it'll cause a loop.
417 * As we couldn't find a site, we're simply not installed.
425 // Figure out the current network's main site.
426 if ( empty( $current_site->blog_id ) ) {
427 if ( $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) {
428 $current_site->blog_id = $current_blog->blog_id;
429 } elseif ( ! $current_site->blog_id = wp_cache_get( 'network:' . $current_site->id . ':main_site', 'site-options' ) ) {
430 $current_site->blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s",
431 $current_site->domain, $current_site->path ) );
432 wp_cache_add( 'network:' . $current_site->id . ':main_site', $current_site->blog_id, 'site-options' );
440 * Displays a failure message.
442 * Used when a blog's tables do not exist. Checks for a missing $wpdb->site table as well.
446 * @since 4.4.0 The `$domain` and `$path` parameters were added.
448 * @global wpdb $wpdb WordPress database abstraction object.
450 * @param string $domain The requested domain for the error to reference.
451 * @param string $path The requested path for the error to reference.
453 function ms_not_installed( $domain, $path ) {
456 if ( ! is_admin() ) {
460 wp_load_translations_early();
462 $title = __( 'Error establishing a database connection' );
464 $msg = '<h1>' . $title . '</h1>';
465 $msg .= '<p>' . __( 'If your site does not display, please contact the owner of this network.' ) . '';
466 $msg .= ' ' . __( 'If you are the owner of this network please check that MySQL is running properly and all tables are error free.' ) . '</p>';
467 $query = $wpdb->prepare( "SHOW TABLES LIKE %s", $wpdb->esc_like( $wpdb->site ) );
468 if ( ! $wpdb->get_var( $query ) ) {
469 $msg .= '<p>' . sprintf(
470 /* translators: %s: table name */
471 __( '<strong>Database tables are missing.</strong> This means that MySQL is not running, WordPress was not installed properly, or someone deleted %s. You really should look at your database now.' ),
472 '<code>' . $wpdb->site . '</code>'
475 $msg .= '<p>' . sprintf(
476 /* translators: 1: site url, 2: table name, 3: database name */
477 __( '<strong>Could not find site %1$s.</strong> Searched for table %2$s in database %3$s. Is that right?' ),
478 '<code>' . rtrim( $domain . $path, '/' ) . '</code>',
479 '<code>' . $wpdb->blogs . '</code>',
480 '<code>' . DB_NAME . '</code>'
483 $msg .= '<p><strong>' . __( 'What do I do now?' ) . '</strong> ';
484 /* translators: %s: Codex URL */
485 $msg .= sprintf( __( 'Read the <a href="%s" target="_blank">bug report</a> page. Some of the guidelines there may help you figure out what went wrong.' ),
486 __( 'https://codex.wordpress.org/Debugging_a_WordPress_Network' )
488 $msg .= ' ' . __( 'If you’re still stuck with this message, then check that your database contains the following tables:' ) . '</p><ul>';
489 foreach ( $wpdb->tables('global') as $t => $table ) {
490 if ( 'sitecategories' == $t )
492 $msg .= '<li>' . $table . '</li>';
496 wp_die( $msg, $title, array( 'response' => 500 ) );
500 * This deprecated function formerly set the site_name property of the $current_site object.
502 * This function simply returns the object, as before.
503 * The bootstrap takes care of setting site_name.
507 * @deprecated 3.9.0 Use get_current_site() instead.
509 * @param object $current_site
512 function get_current_site_name( $current_site ) {
513 _deprecated_function( __FUNCTION__, '3.9.0', 'get_current_site()' );
514 return $current_site;
518 * This deprecated function managed much of the site and network loading in multisite.
520 * The current bootstrap code is now responsible for parsing the site and network load as
521 * well as setting the global $current_site object.
527 * @global object $current_site
531 function wpmu_current_site() {
532 global $current_site;
533 _deprecated_function( __FUNCTION__, '3.9.0' );
534 return $current_site;
538 * Retrieve an object containing information about the requested network.
541 * @deprecated 4.7.0 Use `get_network()`
544 * @internal In 4.6.0, converted to use get_network()
546 * @param object|int $network The network's database row or ID.
547 * @return WP_Network|false Object containing network information if found, false if not.
549 function wp_get_network( $network ) {
550 _deprecated_function( __FUNCTION__, '4.7.0', 'get_network()' );
552 $network = get_network( $network );
553 if ( null === $network ) {