delete_option()
Removes option by name. Prevents removal of protected WordPress options.
Used By: remove_theme_mods()
Hooks from the function
Return
true|false
. True if the option was deleted, false otherwise.
Usage
delete_option( $option );
- $option(string) (required)
- Name of the option to delete. Expected to not be SQL-escaped.
Examples
#1 Basic example
Remove the myoption
option
delete_option( 'myoption' );
#2 Let's delete several options at once and make sure they are removed
Here we will try to remove the following options: is_installed
, my_plugin_version
, my_option
.
$deleted = delete_my_options( 'is_installed', 'my_plugin_version', 'my_option' ); if( $deleted ){ echo 'The settings have been deleted!'; } else { echo 'Deleting settings caused an error. The settings could not be removed!'; } function delete_my_options() { $args = func_get_args(); $num = count( $args ); if( $num === 1 ){ return ( delete_option( $args[0] ) ? true : false ); } if( $num > 1 ){ foreach( $args as $option ){ if( ! delete_option( $option ) ){ return false; } } return true; } return false; }
#3 Delete all settings
On Plugin deactivation if want to delete all settings.
$to_delete = array( 'plugin_status', 'export_status', 'notifications', 'label_settings', // etc ); // Clear up our settings foreach ( $to_delete as $name ) { delete_option( $name ); }
Notes
- Global. wpdb. $wpdb WordPress database abstraction object.
Changelog
Since 1.2.0 | Introduced. |