How to trigger function on theme delete?

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 ?

1 Answer
1

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.

Leave a Comment