Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress delete_option(); Wildcard capability?

How would one go about deleting all option names in a WordPress database beginning with a specific prefix?

I would assume we need to specify a prefix, get all options that begin with that prefix, and then delete each option found.

Here is a sample of the prefix and WP functions for getting and deleting options in the database.

<?php
$prefix = 'cpt_';
$getOpt = get_option($prefix);
foreach($getOpt as $toDelete){
    $deleteOpt = delete_option($prefix);
    if(!$deleteOpt){
        echo 'Failure.';
    }
    if($deleteOpt){
        echo 'Success.';
    }
}
?>

Resources:

  • http://codex.wordpress.org/Function_Reference/delete_option
  • http://codex.wordpress.org/Function_Reference/get_option
like image 330
Michael Ecklund Avatar asked Dec 22 '22 04:12

Michael Ecklund


1 Answers

You could run this query:

global $wpdb;
$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE 'cpt_%'" );

or put it in a function like so:

function delete_options_prefixed( $prefix ) {
    global $wpdb;
    $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '{$prefix}%'" );
}
delete_options_prefixed( 'cpt_' );
like image 76
tschutter Avatar answered Dec 24 '22 01:12

tschutter