函数文档

get_subdirectory_reserved_names()

💡 云策文档标注

概述

get_subdirectory_reserved_names() 函数用于在子目录多站点安装中检索保留站点名称列表。它返回一个字符串数组,并可通过 subdirectory_reserved_names 过滤器进行修改。

关键要点

  • 函数返回一个包含默认保留名称的数组,如 'page'、'comments'、'blog'、'wp-admin' 等。
  • 使用 apply_filters() 钩子允许开发者通过 subdirectory_reserved_names 过滤器自定义保留名称列表。
  • 在 WordPress 4.4.0 版本中引入了 'wp-admin'、'wp-content'、'wp-includes'、'wp-json' 和 'embed' 到保留列表中。
  • 函数主要用于 wpmu_validate_blog_signup() 等处理新站点注册的场景。

代码示例

function change_subdirectory_reserved_names( $names ) {
    $position = array_search( 'blog', $names );
    if( $position ) {
        unset( $names[$position] );
    }
    return $names;
}
add_filter( 'subdirectory_reserved_names', 'change_subdirectory_reserved_names' );

注意事项

  • 修改保留名称时,确保函数返回数组并正确处理参数,避免覆盖其他插件或代码的更改。
  • 注意 WordPress 版本兼容性,例如 4.4.0 新增的保留名称。

📄 原文内容

Retrieves a list of reserved site on a sub-directory Multisite installation.

Return

string[] Array of reserved names.

Source

function get_subdirectory_reserved_names() {
	$names = array(
		'page',
		'comments',
		'blog',
		'files',
		'feed',
		'wp-admin',
		'wp-content',
		'wp-includes',
		'wp-json',
		'embed',
	);

	/**
	 * Filters reserved site names on a sub-directory Multisite installation.
	 *
	 * @since 3.0.0
	 * @since 4.4.0 'wp-admin', 'wp-content', 'wp-includes', 'wp-json', and 'embed' were added
	 *              to the reserved names list.
	 *
	 * @param string[] $subdirectory_reserved_names Array of reserved names.
	 */
	return apply_filters( 'subdirectory_reserved_names', $names );
}

Hooks

apply_filters( ‘subdirectory_reserved_names’, string[] $subdirectory_reserved_names )

Filters reserved site names on a sub-directory Multisite installation.

Changelog

Version Description
4.4.0 Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Allow “/blog” in multsite network :

    function mod_subdirectory_reserved_names() {
        $names = array(
            'page', 'comments', 'files', 'feed', 'wp-admin',
            'wp-content', 'wp-includes', 'wp-json', 'embed'
        );
    }
    add_filter( "subdirectory_reserved_names", "mod_subdirectory_reserved_names");

  2. Skip to note 4 content

    Here is better solution to modify this list of reserved names. Solution by daniyalahmedk not working for 2 reasons:

    1. The function not returning anything. There is no return state in the function and WordPress get null as result of this function (not array with words).
    2. Function not accepting args. This means all other plugins/code changes will be replaced.
    /**
     * Modify reserved names for sites.
     *
     * @param $names array List of reserved names for sites.
     *
     * @return array Updated list of reserved names for sites.
     */
    function change_subdirectory_reserved_names( $names ) {
    	$position = array_search( 'blog', $names );
    
    	if( $position ) {
    		unset( $names[$position] );
    	}
    
    	return $names;
    }
    add_filter( 'subdirectory_reserved_names', 'change_subdirectory_reserved_names' );