wp_determine_option_autoload_value()
云策文档标注
概述
wp_determine_option_autoload_value() 函数用于根据输入确定选项的自动加载值。它处理显式提供的 autoload 参数,或通过启发式规则返回标准化的自动加载状态。
关键要点
- 函数接受 $option、$value、$serialized_value 和 $autoload 参数,返回标准化的 autoload 字符串。
- 支持 'on'、'off'、'auto-on'、'auto-off' 和 'auto' 作为返回值,并向后兼容 'yes' 和 'no'。
- 当 $autoload 为布尔值时,true 返回 'on',false 返回 'off'。
- 如果未提供显式 autoload 值,函数会应用 wp_default_autoload_value 过滤器来决定默认值。
- 过滤器允许开发者自定义默认 autoload 行为,返回 true、false 或 null 分别对应 'auto-on'、'auto-off' 或 'auto'。
代码示例
function wp_determine_option_autoload_value( $option, $value, $serialized_value, $autoload ) {
// Check if autoload is a boolean.
if ( is_bool( $autoload ) ) {
return $autoload ? 'on' : 'off';
}
switch ( $autoload ) {
case 'on':
case 'yes':
return 'on';
case 'off':
case 'no':
return 'off';
}
/**
* Allows to determine the default autoload value for an option where no explicit value is passed.
*
* @since 6.6.0
*
* @param bool|null $autoload The default autoload value to set. Returning true will be set as 'auto-on' in the
* database, false will be set as 'auto-off', and null will be set as 'auto'.
* @param string $option The passed option name.
* @param mixed $value The passed option value to be saved.
*/
$autoload = apply_filters( 'wp_default_autoload_value', null, $option, $value, $serialized_value );
if ( is_bool( $autoload ) ) {
return $autoload ? 'auto-on' : 'auto-off';
}
return 'auto';
}注意事项
- 函数在 WordPress 6.6.0 版本中引入,主要用于 update_option() 和 add_option() 函数内部。
- 使用 wp_default_autoload_value 过滤器时,确保正确处理返回的布尔值或 null 以避免意外行为。
原文内容
Determines the appropriate autoload value for an option based on input.
Description
This function checks the provided autoload value and returns a standardized value (‘on’, ‘off’, ‘auto-on’, ‘auto-off’, or ‘auto’) based on specific conditions.
If no explicit autoload value is provided, the function will check for certain heuristics around the given option.
It will return auto-on to indicate autoloading, auto-off to indicate not autoloading, or auto if no clear decision could be made.
Parameters
$optionstringrequired-
The name of the option.
$valuemixedrequired-
The value of the option to check its autoload value.
$serialized_valuemixedrequired-
The serialized value of the option to check its autoload value.
$autoloadbool|nullrequired-
The autoload value to check.
Accepts<code>'on'|true to enable or<code>'off'|false to disable, or'auto-on','auto-off', or'auto'for internal purposes.
Any other autoload value will be forced to either'auto-on','auto-off', or'auto'.
'yes'and'no'are supported for backward compatibility.
Source
function wp_determine_option_autoload_value( $option, $value, $serialized_value, $autoload ) {
// Check if autoload is a boolean.
if ( is_bool( $autoload ) ) {
return $autoload ? 'on' : 'off';
}
switch ( $autoload ) {
case 'on':
case 'yes':
return 'on';
case 'off':
case 'no':
return 'off';
}
/**
* Allows to determine the default autoload value for an option where no explicit value is passed.
*
* @since 6.6.0
*
* @param bool|null $autoload The default autoload value to set. Returning true will be set as 'auto-on' in the
* database, false will be set as 'auto-off', and null will be set as 'auto'.
* @param string $option The passed option name.
* @param mixed $value The passed option value to be saved.
*/
$autoload = apply_filters( 'wp_default_autoload_value', null, $option, $value, $serialized_value );
if ( is_bool( $autoload ) ) {
return $autoload ? 'auto-on' : 'auto-off';
}
return 'auto';
}
Hooks
- apply_filters( ‘wp_default_autoload_value’, bool|null $autoload, string $option, mixed $value )
-
Allows to determine the default autoload value for an option where no explicit value is passed.
Changelog
| Version | Description |
|---|---|
| 6.6.0 | Introduced. |