函数文档

delete_option()

💡 云策文档标注

概述

delete_option() 是 WordPress 核心函数,用于根据名称删除选项,并自动保护受保护的 WordPress 选项不被删除。它处理数据库操作、缓存清理和钩子触发,确保数据一致性。

关键要点

  • 函数接受一个字符串参数 $option,表示要删除的选项名称,参数不应进行 SQL 转义。
  • 返回布尔值:成功删除返回 true,否则返回 false(如选项不存在或受保护)。
  • 内部调用 wp_protect_special_option() 防止删除受保护的 WordPress 选项。
  • 触发多个钩子:delete_option(删除前)、delete_option_{$option}(特定选项删除后)和 deleted_option(删除后)。
  • 自动清理缓存:根据选项的 autoload 状态更新 alloptions 或删除特定缓存,并维护 notoptions 缓存。
  • 与 wpdb 类交互执行数据库查询,确保操作安全高效。

代码示例

// 删除单个选项
delete_option('my_option');

// 在插件停用时批量删除选项
$settingOptions = array('plugin_status', 'export_status', 'notifications', 'label_settings');
foreach ($settingOptions as $settingName) {
    delete_option($settingName);
}

📄 原文内容

Removes an option by name. Prevents removal of protected WordPress options.

Parameters

$optionstringrequired
Name of the option to delete. Expected to not be SQL-escaped.

Return

bool True if the option was deleted, false otherwise.

Source

function delete_option( $option ) {
	global $wpdb;

	if ( is_scalar( $option ) ) {
		$option = trim( $option );
	}

	if ( empty( $option ) ) {
		return false;
	}

	wp_protect_special_option( $option );

	// Get the ID, if no ID then return.
	$row = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) );
	if ( is_null( $row ) ) {
		return false;
	}

	/**
	 * Fires immediately before an option is deleted.
	 *
	 * @since 2.9.0
	 *
	 * @param string $option Name of the option to delete.
	 */
	do_action( 'delete_option', $option );

	$result = $wpdb->delete( $wpdb->options, array( 'option_name' => $option ) );

	if ( ! wp_installing() ) {
		if ( in_array( $row->autoload, wp_autoload_values_to_autoload(), true ) ) {
			$alloptions = wp_load_alloptions( true );

			if ( is_array( $alloptions ) && isset( $alloptions[ $option ] ) ) {
				unset( $alloptions[ $option ] );
				wp_cache_set( 'alloptions', $alloptions, 'options' );
			}
		} else {
			wp_cache_delete( $option, 'options' );
		}

		$notoptions = wp_cache_get( 'notoptions', 'options' );

		if ( ! is_array( $notoptions ) ) {
			$notoptions = array();
		}
		$notoptions[ $option ] = true;

		wp_cache_set( 'notoptions', $notoptions, 'options' );
	}

	if ( $result ) {

		/**
		 * Fires after a specific option has been deleted.
		 *
		 * The dynamic portion of the hook name, `$option`, refers to the option name.
		 *
		 * @since 3.0.0
		 *
		 * @param string $option Name of the deleted option.
		 */
		do_action( "delete_option_{$option}", $option );

		/**
		 * Fires after an option has been deleted.
		 *
		 * @since 2.9.0
		 *
		 * @param string $option Name of the deleted option.
		 */
		do_action( 'deleted_option', $option );

		return true;
	}

	return false;
}

Hooks

do_action( ‘deleted_option’, string $option )

Fires after an option has been deleted.

do_action( ‘delete_option’, string $option )

Fires immediately before an option is deleted.

do_action( “delete_option_{$option}”, string $option )

Fires after a specific option has been deleted.

Changelog

Version Description
1.2.0 Introduced.

User Contributed Notes