函数文档

ms_upload_constants()

💡 云策文档标注

概述

ms_upload_constants() 函数用于定义 WordPress 多站点(Multisite)环境中的上传相关常量,主要处理文件路径设置,并兼容旧版文件服务。

关键要点

  • 函数通过 add_filter 设置默认选项 ms_files_rewriting 为 true,但仅在非 SHORTINIT 模式下生效。
  • 如果 ms_files_rewriting 选项未启用,函数会提前返回,不执行后续常量定义。
  • 定义 UPLOADBLOGSDIR 常量,指定上传目录的基础路径(相对于 ABSPATH)。
  • 定义 UPLOADS 常量,基于当前站点 ID 动态生成上传路径,并处理主站点的特殊情况。
  • 在特定条件下定义 BLOGUPLOADDIR 常量,提供上传目录的绝对路径。

代码示例

function ms_upload_constants() {
    // This filter is attached in ms-default-filters.php but that file is not included during SHORTINIT.
    add_filter( 'default_site_option_ms_files_rewriting', '__return_true' );

    if ( ! get_site_option( 'ms_files_rewriting' ) ) {
        return;
    }

    // Base uploads dir relative to ABSPATH.
    if ( ! defined( 'UPLOADBLOGSDIR' ) ) {
        define( 'UPLOADBLOGSDIR', 'wp-content/blogs.dir' );
    }

    /*
     * Note, the main site in a post-MU network uses wp-content/uploads.
     * This is handled in wp_upload_dir() by ignoring UPLOADS for this case.
     */
    if ( ! defined( 'UPLOADS' ) ) {
        $site_id = get_current_blog_id();

        define( 'UPLOADS', UPLOADBLOGSDIR . '/' . $site_id . '/files/' );

        // Uploads dir relative to ABSPATH.
        if ( 'wp-content/blogs.dir' === UPLOADBLOGSDIR && ! defined( 'BLOGUPLOADDIR' ) ) {
            define( 'BLOGUPLOADDIR', WP_CONTENT_DIR . '/blogs.dir/' . $site_id . '/files/' );
        }
    }
}

注意事项

  • 此函数主要用于向后兼容,特别是针对通过 wp-includes/ms-files.php(或 MU 中的 wp-content/blogs.php)提供文件的旧版多站点设置。
  • 在 SHORTINIT 模式下,相关过滤器可能不会加载,需注意常量定义的依赖条件。

📄 原文内容

Defines Multisite upload constants.

Description

Exists for backward compatibility with legacy file-serving through wp-includes/ms-files.php (wp-content/blogs.php in MU).

Source

function ms_upload_constants() {
	// This filter is attached in ms-default-filters.php but that file is not included during SHORTINIT.
	add_filter( 'default_site_option_ms_files_rewriting', '__return_true' );

	if ( ! get_site_option( 'ms_files_rewriting' ) ) {
		return;
	}

	// Base uploads dir relative to ABSPATH.
	if ( ! defined( 'UPLOADBLOGSDIR' ) ) {
		define( 'UPLOADBLOGSDIR', 'wp-content/blogs.dir' );
	}

	/*
	 * Note, the main site in a post-MU network uses wp-content/uploads.
	 * This is handled in wp_upload_dir() by ignoring UPLOADS for this case.
	 */
	if ( ! defined( 'UPLOADS' ) ) {
		$site_id = get_current_blog_id();

		define( 'UPLOADS', UPLOADBLOGSDIR . '/' . $site_id . '/files/' );

		// Uploads dir relative to ABSPATH.
		if ( 'wp-content/blogs.dir' === UPLOADBLOGSDIR && ! defined( 'BLOGUPLOADDIR' ) ) {
			define( 'BLOGUPLOADDIR', WP_CONTENT_DIR . '/blogs.dir/' . $site_id . '/files/' );
		}
	}
}

Changelog

Version Description
3.0.0 Introduced.