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' );
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.
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 infunction 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).
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:
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).
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!)
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.