函数文档

wp_is_file_mod_allowed()

💡 云策文档标注

概述

wp_is_file_mod_allowed() 函数用于判断是否允许文件修改操作,基于 DISALLOW_FILE_MODS 常量定义和 'file_mod_allowed' 过滤器。它接受一个上下文参数,返回布尔值表示允许与否。

关键要点

  • 函数 wp_is_file_mod_allowed($context) 检查文件修改权限,返回 true 或 false。
  • 参数 $context 是必需的字符串,指定使用上下文。
  • 内部使用 apply_filters('file_mod_allowed', bool $file_mod_allowed, string $context) 过滤器,允许开发者自定义逻辑。
  • 默认行为取决于 DISALLOW_FILE_MODS 常量:如果未定义或为 false,则允许文件修改。
  • 自 WordPress 4.8.0 版本引入,常用于语言包安装、自动更新等场景。

代码示例

function wp_is_file_mod_allowed( $context ) {
    return apply_filters( 'file_mod_allowed', ! defined( 'DISALLOW_FILE_MODS' ) || ! DISALLOW_FILE_MODS, $context );
}

注意事项

  • 确保正确设置 DISALLOW_FILE_MODS 常量以控制文件修改权限。
  • 使用 'file_mod_allowed' 过滤器可以覆盖默认行为,适应特定需求。
  • 上下文参数 $context 可用于区分不同操作场景,如 'language_install' 或 'automatic_update'。

📄 原文内容

Determines whether file modifications are allowed.

Parameters

$contextstringrequired
The usage context.

Return

bool True if file modification is allowed, false otherwise.

Source

function wp_is_file_mod_allowed( $context ) {
	/**
	 * Filters whether file modifications are allowed.
	 *
	 * @since 4.8.0
	 *
	 * @param bool   $file_mod_allowed Whether file modifications are allowed.
	 * @param string $context          The usage context.
	 */
	return apply_filters( 'file_mod_allowed', ! defined( 'DISALLOW_FILE_MODS' ) || ! DISALLOW_FILE_MODS, $context );
}

Hooks

apply_filters( ‘file_mod_allowed’, bool $file_mod_allowed, string $context )

Filters whether file modifications are allowed.

Changelog

Version Description
4.8.0 Introduced.