函数文档

allow_subdomain_install()

💡 云策文档标注

概述

allow_subdomain_install() 是 WordPress 核心函数,用于检查是否允许基于子域的多站点安装。它返回布尔值,true 表示允许,false 表示不允许。

关键要点

  • 函数返回 bool 类型,指示是否允许子域安装。
  • 用于确定 WordPress 多站点网络是否支持子域模式(如 site1.example.com)而非仅子目录模式(如 example.com/site1/)。
  • 函数内部逻辑基于解析主站点 URL:如果 URL 包含路径、域名为 localhost 或 IP 地址,则返回 false。

代码示例

if (allow_subdomain_install()) {
    // 子域多站点安装被允许
    // 可进行子域多站点设置
} else {
    // 子域多站点安装不被允许
    // 只能设置子目录多站点
}

注意事项

  • 此函数自 WordPress 3.0.0 版本引入。
  • 常用于网络安装过程(如 network_step1() 和 network_step2())中检查安装选项。
  • 依赖 get_option('home') 获取主站点 URL,并解析其主机名和路径。

📄 原文内容

Allow subdomain installation

Return

bool Whether subdomain installation is allowed

Source

function allow_subdomain_install() {
	$home   = get_option( 'home' );
	$domain = parse_url( $home, PHP_URL_HOST );
	if ( parse_url( $home, PHP_URL_PATH ) || 'localhost' === $domain || preg_match( '|^[0-9]+.[0-9]+.[0-9]+.[0-9]+$|', $domain ) ) {
		return false;
	}

	return true;
}

Changelog

Version Description
3.0.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    The allow_subdomain_install() function is a WordPress function used to determine whether subdomain-based multisite installations are allowed on a WordPress site. This function returns a boolean value (true or false) indicating whether subdomain-based multisite installations are permitted.

    In a WordPress multisite network, you have the option to create either subdirectory-based or subdomain-based installations. Here’s what each option means:

    Subdirectory-Based Multisite: In this mode, sites within the network are structured as subdirectories of the main site’s domain. For example, if the main site’s domain is example.com, a subdirectory-based site might have the URL example.com/site1/.

    Subdomain-Based Multisite: In this mode, sites within the network are structured as subdomains of the main site’s domain. For example, if the main site’s domain is example.com, a subdomain-based site might have the URL site1.example.com.

    The allow_subdomain_install() function is used to check if subdomain-based multisite installations are allowed. The function returns true if subdomain-based installations are permitted, and false if they are not.

    Here’s a typical usage scenario:

    if (allow_subdomain_install()) {
        // Subdomain-based multisite is allowed.
        // You can proceed with subdomain-based multisite setup.
    } else {
        // Subdomain-based multisite is not allowed.
        // You may only set up subdirectory-based multisite.
    }