delete_option()删除选项

功能:

delete_option()函数的功能是删除wp_options表中指定的一条数据。

<?php delete_option( $option ); ?>

参数:

$option
(string) (必选的) 要删除的选项表记录名称
默认: 无

返回值:

(boolean)
如果成功删除记录就返回True,删除失败或者记录不存在就返回false

所在位置:

delete_option() 函数包含在 wp-includes/option.php 中。

源码:

delete_option()函数源码

/**
 * Removes option by name. Prevents removal of protected WordPress options.
 *
 * @since 1.2.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
 * @return bool True, if option is successfully deleted. False on failure.
 */
function delete_option( $option ) {
	global $wpdb;
 
	$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 ( 'yes' == $row->autoload ) {
			$alloptions = wp_load_alloptions();
			if ( is_array( $alloptions ) && isset( $alloptions[$option] ) ) {
				unset( $alloptions[$option] );
				wp_cache_set( 'alloptions', $alloptions, 'options' );
			}
		} else {
			wp_cache_delete( $option, '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;
}

示例:

<?php
    if(delete_option("wptoutiao")){    //将wp_options表中选项名为wptoutiao的记录删除,成功就返回true
        echo "删除成功";
    } 
?>

相关函数:

add_option()
get_option()
update_option()

(完)

版权声明WordPress头条正在使用WordPress文章版权插件
如未注明,均为原创,转载需注明出处
本文链接地址:http://www.wptoutiao.com/functions/152.html

数据存储的相关文章

选项表的相关文章

还可以分享文章给好友:

delete_option()删除选项”有1个评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注