delete_option()
Deletes settings (post from the wp_options table in the DB).
This function is created for the safe deletion of posts (settings) from the Database.
Used By: remove_theme_mods()
Hooks from the function
Returns
true|false. true if the setting was found and deleted and false otherwise.
Usage
delete_option( $name );
- $name(string) (required)
- The name of the option to be deleted.
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.
$wpdbWordPress database abstraction object.
Changelog
| Since 1.2.0 | Introduced. |