Remove settings if theme is deleted?

Is it possible to perform an action only if the theme is deleted (not deactivated) from the theme directory ?

For example, I want to remove the theme settings from database after the theme is removed. So I want to run this when the theme is deleted:

delete_option( 'mysettings' );

Related posts

5 comments

  1. As the other answers seem to imply, there is no built-in way to do this. There is no theme uninstall API similar to that for plugins. There is actually a long-standing ticket to get this feature added to WordPress core, so that themes can uninstall themselves. The consensus from the lead developers there seems to be that this isn’t a feature that want themes to have. I’m not sure what the rationale for that is. Maybe you can help explain to them the benefits of it.

  2. The only action/filter hook that I’ve found in this part of code is in

    do_action( 'delete_site_transient_' . $transient, $transient );

    It’s called inside delete_site_transient('update_themes'); which is called in function delete_theme($stylesheet, $redirect = '') after the theme is successfully deleted.

    So I would do this adding my action to delete_site_transient_update_themes.
    In this action you can check some conditions (name of theme) and then delete some options.

    Of course this code is called when the theme is inactive and after its directory is deleted from server. So you can’t add this action inside that theme (it would be nice) and you have to place it in some plugin. (I’m not convinced if installing additional plugin just to be sure that theme options are deleted after that theme is deleted is a great idea).

  3. I’m not sure if I understand the question, so I’m going to rephrase it first, but if I do, I have done a workaround for this.

    Understanding the question as:

    If the theme is deactivated, the site options are preserved so if activated again they don’t need to be re-entered, but when deleted they are completely removed.

    This is not a full answer because it speaks to the plugin deactivate versus uninstall hooks rather than theme, but assuming theme has something similar, what I did was this:

    1. On deactivate, I pushed the option settings into a small backup table.
    2. On activate, check to see if the backup table exists and if so, restore the option settings from the backup and drop the table (or just check for the row and delete the row). Make sure you name the table so it’s clearly tied to the theme like the above notes on woocommerce. And always delete the settings after they’re used so that you ensure the most recent settings are available.
    3. On delete, behave normally (which purges the option settings permanently).

    This way the user doesn’t have to redo the option settings if they have to deactivate to check for plugin and theme conflicts (as I recently had to do with a Woo theme).

  4. it really totally depends on theme author I think…some do better garbage collection than others. I just got done going completely through my wp_options table trying to locate a cause…and in doing so, I found a TON of theme options for themes long since deleted and gone from the server. Luckily, the ones with the most options that I wanted to get rid of and clean up my table, the author had put a nice prefix on all ‘his’ files so they could be easily identified. Go into your table, and see if this is a case for you. If it is, then you can write an appropriate query.

    Also, transients and site_transients are supposed to remove themselves after a certain time passes…this is not always the case either. I deleted pages and pages of those manually also.

    I don’t know if that answers the question. It looks like you want to generate a php function…I would do a mySql query and do it that way. (with db backups first, of course!)

  5. It is very complex to know which option was generated by your theme settings.

    But if you follow name convension for example Woocommerce (All options created by Woocomerce have a name like woocommerce_* and woocommerce as the plugin directory name). It makes easy to search and delete theme options.

    So for the theme options you could look for this:

    If the current theme is called Deepfocus, it’s option name should be like like deepfocus_*

    If the current theme is Twenty Twelve, it’s option name should be like like twentytwelve_*

    Note:
    I created a simple plugin ( WP-Delete-Theme-Option ) which follows these conventions to delete theme specific option.

    Plugin generates option name from theme’s directory name.

Comments are closed.