1

Tried to find an or some hook to fire a function when theme is being delited, but didn't find any hook..

May be someone can tell if there's a way to fire my function on themes delete event?

I need it to delete options and drop tables on theme delete. ( for example )

Plugins do have uninstall.php or register_uninstall_hook().

Need same for themes! any help ?

My thoughts so far:

  1. create new WP_Filesystem_$module class and force WP to use my WP_Filesystem_$module class for deleting themes, where i can insert custom actions atc.

  2. insert custom action in deleted_site_transient action, checking if it was update_themes transient and check if theme was delited, wich theme was deleted etc..

Both methods has different questions like how to force my class or how to check wich theme was deleted in deleted_site_transient...

What's your thoughts on these ?

  • First of all. Are you sure that this code you have in your theme, really should be in your theme and not in a plugin? A theme should only include functions bound to the layout/theme functions. All other functionality should be put in a plugin. EDIT: I thin neither one or two sounds good. You should use WP functions when ever possible. And it sounds like the code you currently have in your theme is something that should be in a plugin. I would suggest you create a site-plugin for this code. And use the plugin-hooks. – Alex Sep 4 '15 at 5:04
  • thanks for your reply. I'm writing a plugin, like framework for themes, it will automatically be instantioned when plugin activates to create a theme instance ( from plugin ) and will create some database entries. Problem is that while my plugin is active i need to uninstall themes data from database if it is being delited.. I hope it's clear now why i need these.. I could trigger on plugin uninstall ( it's triggered already ), but i also need to trigger on theme uninstall – Maikal Sep 4 '15 at 10:58
  • Okay I understand now. Any way, sounds like you inventing the wheel once again. I think there must be a esier way to do what you are trying to do. But sorry, I think I can't help you with this :) – Alex Sep 4 '15 at 18:28
0

As long as you don't delete your theme by ftp, but use the regular way of switching to another theme in the admin, there is a hook to use, switch_theme. It works like this:

add_action('switch_theme', 'mytheme_setup_options');

function mytheme_setup_options () {
  delete_option('mytheme_option1');
  delete_option('mytheme_option2');
}

There's a sister hook called after_switch_theme, which you could use, for instance, to load default options when a theme is switched on.

  • Yeah, i know that one, but the problem here is that if i don't uninstall theme after switching.. i.e. i switched it not on porpouse or for some othere reason, but i don't want to loose all the data on switch.. only on uninstalling as if in case of switching it might be used later.. – Maikal Jun 19 '16 at 4:35
  • Right. In that case, the only way I can think of to keep code active that monitors this, would be to have a plugin that scans the list of available themes every day or so and undertakes a clean up when it doesn't find your theme in the directory anymore. – cjbj Jun 19 '16 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.