函数文档

wp_set_option_autoload()

💡 云策文档标注

概述

wp_set_option_autoload() 函数用于设置数据库中单个选项的自动加载值,控制选项是否在 WordPress 启动时加载。它是 wp_set_option_autoload_values() 的包装器,后者可批量设置多个选项。

关键要点

  • 函数接受两个参数:$option(选项名称,无需 SQL 转义)和 $autoload(自动加载值,布尔类型,兼容已弃用的 'yes'/'no' 字符串)。
  • 返回布尔值:如果自动加载值被修改则返回 true,否则返回 false。
  • 自 WordPress 6.7.0 起,'yes' 和 'no' 字符串值已弃用,建议使用布尔值。
  • 函数内部调用 wp_set_option_autoload_values() 处理,适用于单个选项场景。

代码示例

function wp_set_option_autoload( $option, $autoload ) {
	$result = wp_set_option_autoload_values( array( $option => $autoload ) );
	if ( isset( $result[ $option ] ) ) {
		return $result[ $option ];
	}
	return false;
}

注意事项

  • 参数 $option 不应进行 SQL 转义,直接传递原始字符串。
  • 为保持向后兼容,$autoload 参数仍接受 'yes' 和 'no' 字符串,但自 6.7.0 版本起已弃用,推荐使用布尔值 true 或 false。
  • 此函数主要用于优化性能,通过控制选项自动加载减少数据库查询。

📄 原文内容

Sets the autoload value for an option in the database.

Description

This is a wrapper for wp_set_option_autoload_values(), which can be used to set the autoload value for multiple options at once.

See also

Parameters

$optionstringrequired
Name of the option. Expected to not be SQL-escaped.
$autoloadboolrequired
Autoload value to control whether to load the option when WordPress starts up.
For backward compatibility 'yes' and 'no' are also accepted, though using these values is deprecated.

Return

bool True if the autoload value was modified, false otherwise.

Source

function wp_set_option_autoload( $option, $autoload ) {
	$result = wp_set_option_autoload_values( array( $option => $autoload ) );
	if ( isset( $result[ $option ] ) ) {
		return $result[ $option ];
	}
	return false;
}

Changelog

Version Description
6.7.0 The autoload values 'yes' and 'no' are deprecated.
6.4.0 Introduced.