wp_protect_special_option()
云策文档标注
概述
wp_protect_special_option() 函数用于保护 WordPress 的特殊选项不被修改,如果尝试修改受保护的选项,函数会终止执行并显示错误消息。
关键要点
- 函数检查 $option 是否为受保护选项,包括 'alloptions' 和 'notoptions'。
- 如果 $option 是受保护选项,函数调用 wp_die() 终止执行,并输出错误消息。
- 该函数在多个核心函数中被调用,如 update_option() 和 delete_option(),以防止对受保护选项的修改。
代码示例
function wp_protect_special_option( $option ) {
if ( 'alloptions' === $option || 'notoptions' === $option ) {
wp_die(
sprintf(
/* translators: %s: Option name. */
__( '%s is a protected WP option and may not be modified' ),
esc_html( $option )
)
);
}
}注意事项
- 受保护选项是 WordPress 内部使用的关键选项,修改它们可能导致系统不稳定。
- 函数自 WordPress 2.2.0 版本引入,是核心功能的一部分。
原文内容
Protects WordPress special option from being modified.
Description
Will die if $option is in protected list. Protected options are ‘alloptions’ and ‘notoptions’ options.
Parameters
$optionstringrequired-
Option name.
Source
function wp_protect_special_option( $option ) {
if ( 'alloptions' === $option || 'notoptions' === $option ) {
wp_die(
sprintf(
/* translators: %s: Option name. */
__( '%s is a protected WP option and may not be modified' ),
esc_html( $option )
)
);
}
}
Changelog
| Version | Description |
|---|---|
| 2.2.0 | Introduced. |