函数文档

delete_blog_option()

💡 云策文档标注

概述

delete_blog_option() 函数用于根据博客 ID 删除指定名称的选项,并防止删除受保护的 WordPress 选项。它通过 switch_to_blog() 和 restore_current_blog() 实现跨博客操作。

关键要点

  • 函数接受两个参数:$id(博客 ID,可为 null 表示当前博客)和 $option(选项名称,无需 SQL 转义)。
  • 返回值:如果选项删除成功返回 true,否则返回 false。
  • 内部逻辑:如果 $id 为空,则使用 get_current_blog_id() 获取当前博客 ID;如果当前博客 ID 与 $id 相同,则直接调用 delete_option();否则切换博客后删除选项。
  • 相关函数包括 delete_option()、switch_to_blog()、restore_current_blog() 和 get_current_blog_id()。

代码示例

function delete_blog_option( $id, $option ) {
    $id = (int) $id;

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

    if ( get_current_blog_id() === $id ) {
        return delete_option( $option );
    }

    switch_to_blog( $id );
    $return = delete_option( $option );
    restore_current_blog();

    return $return;
}

📄 原文内容

Removes an option by name for a given blog ID. Prevents removal of protected WordPress options.

Parameters

$idintrequired
A blog ID. Can be null to refer to the current blog.
$optionstringrequired
Name of option to remove. Expected to not be SQL-escaped.

Return

bool True if the option was deleted, false otherwise.

Source

function delete_blog_option( $id, $option ) {
	$id = (int) $id;

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

	if ( get_current_blog_id() === $id ) {
		return delete_option( $option );
	}

	switch_to_blog( $id );
	$return = delete_option( $option );
	restore_current_blog();

	return $return;
}

Changelog

Version Description
MU (3.0.0) Introduced.