函数文档

wp_autoload_values_to_autoload()

💡 云策文档标注

概述

wp_autoload_values_to_autoload() 函数返回触发选项表自动加载的值数组。该函数通过过滤器允许开发者移除默认值,但无法添加新值。

关键要点

  • 函数返回一个字符串数组,包含触发自动加载的选项值,默认包括 'yes'、'on'、'auto-on' 和 'auto'。
  • 使用 wp_autoload_values_to_autoload 过滤器可以移除默认值,但受 array_intersect() 限制,无法添加新值。
  • 该函数在 WordPress 6.6.0 版本中引入,用于支持 wp_load_alloptions()、update_option() 等相关函数。

代码示例

function wp_autoload_values_to_autoload() {
    $autoload_values = array( 'yes', 'on', 'auto-on', 'auto' );
    $filtered_values = apply_filters( 'wp_autoload_values_to_autoload', $autoload_values );
    return array_intersect( $filtered_values, $autoload_values );
}

注意事项

  • 过滤器 wp_autoload_values_to_autoload 仅用于移除默认值,不能扩展数组,因为 array_intersect() 会强制返回与初始数组的交集。
  • 开发者应了解此限制,避免在自定义代码中尝试添加新值,以免功能失效。

📄 原文内容

Returns the values that trigger autoloading from the options table.

Return

string[] The values that trigger autoloading.

Source

function wp_autoload_values_to_autoload() {
	$autoload_values = array( 'yes', 'on', 'auto-on', 'auto' );

	/**
	 * Filters the autoload values that should be considered for autoloading from the options table.
	 *
	 * The filter can only be used to remove autoload values from the default list.
	 *
	 * @since 6.6.0
	 *
	 * @param string[] $autoload_values Autoload values used to autoload option.
	 *                               Default list contains 'yes', 'on', 'auto-on', and 'auto'.
	 */
	$filtered_values = apply_filters( 'wp_autoload_values_to_autoload', $autoload_values );

	return array_intersect( $filtered_values, $autoload_values );
}

Hooks

apply_filters( ‘wp_autoload_values_to_autoload’, string[] $autoload_values )

Filters the autoload values that should be considered for autoloading from the options table.

Changelog

Version Description
6.6.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Note that it is impossible to add values to the returned array with the wp_autoload_values_to_autoload filter, because of array_intersect(). That forces the filter only to further restrict the values in the returned array from what is initially defined in $autoload_values.