函数文档

option_update_filter()

💡 云策文档标注

概述

option_update_filter() 是一个 WordPress 函数,用于通过 'allowed_options' 钩子刷新允许选项列表的值。它检查全局变量 $new_allowed_options 是否为数组,如果是,则调用 add_allowed_options() 函数来更新选项数组。

关键要点

  • 函数名:option_update_filter(),用于处理允许选项列表的更新。
  • 参数:$options(数组,必需),表示当前的允许选项列表。
  • 返回值:数组,返回更新后的允许选项列表。
  • 依赖全局变量:$new_allowed_options,用于存储新的允许选项。
  • 相关函数:add_allowed_options(),用于将新选项添加到允许列表中。
  • 历史变更:在 WordPress 5.5.0 中,$new_whitelist_options 被重命名为 $new_allowed_options,以促进更包容的代码实践。

代码示例

function option_update_filter( $options ) {
	global $new_allowed_options;

	if ( is_array( $new_allowed_options ) ) {
		$options = add_allowed_options( $new_allowed_options, $options );
	}

	return $options;
}

注意事项

  • 此函数主要用于内部处理,开发者通常通过 'allowed_options' 过滤器来操作允许选项列表。
  • 确保 $new_allowed_options 在调用前已正确设置,否则函数可能不会更新选项。
  • 遵循 WordPress 版本兼容性,注意变量名变更(如 5.5.0 中的重命名)。

📄 原文内容

Refreshes the value of the allowed options list available via the ‘allowed_options’ hook.

Description

See the ‘allowed_options’ filter.

Parameters

$optionsarrayrequired

Return

array

Source

function option_update_filter( $options ) {
	global $new_allowed_options;

	if ( is_array( $new_allowed_options ) ) {
		$options = add_allowed_options( $new_allowed_options, $options );
	}

	return $options;
}

Changelog

Version Description
5.5.0 $new_whitelist_options was renamed to $new_allowed_options.
Please consider writing more inclusive code.
2.7.0 Introduced.