default_site_option_{$option}
云策文档标注
概述
default_site_option_{$option} 是一个动态过滤器钩子,用于过滤特定默认网络选项的值。它允许开发者在选项不存在于数据库时自定义返回的默认值。
关键要点
- 这是一个动态钩子,$option 部分对应选项名称,例如 default_site_option_my_option。
- 主要参数包括 $default_value(选项不存在时的默认值)、$option(选项名称)和 $network_id(网络ID)。
- 钩子由 get_network_option() 函数调用,用于检索网络选项值。
- 从 WordPress 4.7.0 版本开始添加了 $network_id 参数,增强了多网络支持。
注意事项
- 使用此过滤器可能会阻止创建或更新选项,这可能是 WordPress 的一个 bug。
- 如果需要在选项不存在时使用默认值,但又要能保存选项,必须在更新选项前移除过滤器。
代码示例
// remove the filter to prevent failure to add new option.
remove_filter( 'default_site_option_my_network_option', 'setup_default_value', 10 );
update_site_option( 'my_network_option', 'My Option Value' );
原文内容
Filters the value of a specific default network option.
Description
The dynamic portion of the hook name, $option, refers to the option name.
Parameters
$default_valuemixed-
The value to return if the site option does not exist in the database.
$optionstring-
Option name.
$network_idint-
ID of the network.
Source
return apply_filters( "default_site_option_{$option}", $default_value, $option, $network_id );
Skip to note 2 content
Giulio Daprela
Using this filter will prevent you from creating or updating an option. This is likely a bug on WordPress.
If you want to be able to save your option but still want to use the default for when it doesn’t exist yet, then you must remove the filter before updating the option.
// remove the filter to prevent failure to add new option. remove_filter( 'default_site_option_my_network_option', 'setup_default_value', 10 ); update_site_option( 'my_network_option', 'My Option Value' );