函数文档

get_blog_option()

💡 云策文档标注

概述

get_blog_option() 函数用于在多站点网络中根据博客ID和选项名称检索选项值。如果选项不存在或没有值,则返回 false,常用于插件安装或升级检查。选项序列化时会自动反序列化返回。

关键要点

  • 函数用于多站点环境,基于博客ID和选项名获取选项值
  • 参数包括博客ID(可为null表示当前博客)、选项名(无需SQL转义)和默认值(可选,默认为false)
  • 返回值是选项设置的值,若不存在则返回false或默认值
  • 使用 blog_option_$option 过滤器,而不调用 get_option() 中的 option_$option 过滤器
  • 内部通过 switch_to_blog() 切换博客来获取选项,然后恢复当前博客

代码示例

function get_blog_option( $id, $option, $default_value = false ) {
    $id = (int) $id;

    if ( empty( $id ) ) {
        $id = get_current_blog_id();
    }

    if ( get_current_blog_id() === $id ) {
        return get_option( $option, $default_value );
    }

    switch_to_blog( $id );
    $value = get_option( $option, $default_value );
    restore_current_blog();

    /**
     * Filters a blog option value.
     *
     * The dynamic portion of the hook name, `$option`, refers to the blog option name.
     *
     * @since 3.5.0
     *
     * @param string  $value The option value.
     * @param int     $id    Blog ID.
     */
    return apply_filters( "blog_option_{$option}", $value, $id );
}

注意事项

  • 此函数仅在WordPress配置为多站点安装时可用,调用前应检查 function_exists() 或使用 is_multisite()
  • 选项名不应进行SQL转义,函数会处理安全性
  • 如果博客ID为null或空,函数会自动使用当前博客ID

📄 原文内容

Retrieves option value for a given blog id based on name of option.

Description

If the option does not exist or does not have a value, then the return value will be false. This is useful to check whether you need to install an option and is commonly used during installation of plugin options and to test whether upgrading is required.

If the option was serialized then it will be unserialized when it is returned.

Parameters

$idintrequired
A blog ID. Can be null to refer to the current blog.
$optionstringrequired
Name of option to retrieve. Expected to not be SQL-escaped.
$default_valuemixedoptional
Default value to return if the option does not exist.

Default:false

Return

mixed Value set for the option.

More Information

There is a filter called ‘blog_option_$option‘ with the $option being replaced with the option name. The filter takes two parameters: $value and $blog_id. It returns $value. The ‘option_$option‘ filter in get_option() is not called.

Source

function get_blog_option( $id, $option, $default_value = false ) {
	$id = (int) $id;

	if ( empty( $id ) ) {
		$id = get_current_blog_id();
	}

	if ( get_current_blog_id() === $id ) {
		return get_option( $option, $default_value );
	}

	switch_to_blog( $id );
	$value = get_option( $option, $default_value );
	restore_current_blog();

	/**
	 * Filters a blog option value.
	 *
	 * The dynamic portion of the hook name, `$option`, refers to the blog option name.
	 *
	 * @since 3.5.0
	 *
	 * @param string  $value The option value.
	 * @param int     $id    Blog ID.
	 */
	return apply_filters( "blog_option_{$option}", $value, $id );
}

Hooks

apply_filters( “blog_option_{$option}”, string $value, int $id )

Filters a blog option value.

Changelog

Version Description
MU (3.0.0) Introduced.

User Contributed Notes