4

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 ?

3
  • 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
    Commented Sep 4, 2015 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
    Commented Sep 4, 2015 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
    Commented Sep 4, 2015 at 18:28

3 Answers 3

2

Since 5.8.0 you can use the deleted_theme hook together with the stylesheet name:

add_action('deleted_theme', function($stylesheet, $deleted){
    
    // do something
});

You will have to run this filter from outside the theme that you are trying to delete. In the OP case since it is for a plugin it should work.

Resources

https://github.com/WordPress/WordPress/blob/748a5d9919986a6281df4e44d176501b29483450/wp-admin/includes/theme.php#L96

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.

2
  • 2
    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
    Commented Jun 19, 2016 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
    Commented Jun 19, 2016 at 12:53
0

WordPress 5.8 introduced two new hooks: delete_theme and deleted_theme.

do_action( ‘delete_theme’, string $stylesheet )

Fires immediately before a theme deletion attempt.

do_action( ‘deleted_theme’, string $stylesheet, bool $deleted )

Fires immediately after a theme deletion attempt.

You are likely looking for the delete_theme hook as it runs before the theme is deleted, and not after when the theme files wouldn't exist anymore and your hooked function couldn't be executed.

Implementation would be as it follows:

function on_theme_deletion ( $stylesheet ) {
    if ( 'my_theme_stylesheet' === $stylesheet ) {
        // TODO: Delete your options and drop your DB tables.
        delete_option( 'my_theme_option' );
        global $wpdb;
        $wpdb->query( "DROP TABLE IF EXISTS MyThemeTable" );
    }
}

add_action( 'delete_theme', 'on_theme_deletion' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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