• Resolved Guido

    (@guido07111975)


    Hi,

    I’m looking for a way to remove theme settings from database when uninstalling theme.

    For plugin it’s available. But it seems it still is not for themes?

    Who has a workaround / solution for this?

    I wish you all a very nice couple of days!

    Guido

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi Guido,

    You can query those options from options table (settings) through PHPMYADMIN, just select options table o your current installation, click search tab, and in option name type the theme prefix for example “twenty_twelve” followed by % and it should return all options with that name in the beginning..

    This code perform that action without the need to go to your db manager:

    global $wpdb;
    $options = $wpdb->prefix."options";
    $query = $wpdb->get_results( "SELECT * FROM $options WHERE option_name LIKE 'theme_prefix_%'");
    foreach( $query as $meta ) {
    	delete_option( $meta->option_name );
    }

    or simply delete_option( 'option_name' ) if you know the option names.

    You you can apply all of this in an switch_theme action hook: (which gets called when the theme is being deactivated)

    add_action('switch_theme', function() {
    	global $wpdb;
    	$options = $wpdb->prefix."options";
    	$query = $wpdb->get_results( "SELECT * FROM $options WHERE option_name LIKE 'theme_prefix_%'");
    	foreach( $query as $meta ) {
    		delete_option( $meta->option_name );
    	}
    });

    Have a nice day!

    Thread Starter Guido

    (@guido07111975)

    Hi,

    This is exactly what I was looking for, I knew about the delete_option (use it in my plugins too) but did not know about the switch_theme hook.

    Great, thanks!

    Guido

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Delete settings / options when uninstalling theme’ is closed to new replies.