4

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' );
2

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.

0

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).

0

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).

0

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!)

0

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.

  • I'm not sure if it's a good idea to do it this way. You don't check permissions anywhere (you don't check nonce either), so I'm pretty sure someone without access rights could delete options and the theme will be still on place. – Krzysiek Dróżdż Nov 26 '13 at 20:28
  • @KrzysiekDróżdż woothemes following same naming convension.and about plugin you can read under README.md on github that it's under production :). – Ravinder Kumar Nov 26 '13 at 20:38
  • But you won't be able to check this things doing it this way (checking it on init). And if theme is successfully deleted, then redirection occurs, so you can't check it after. – Krzysiek Dróżdż Nov 26 '13 at 21:06
  • 1
    i'm getting you security points and error handling points and i test it with localhost with Standard theme. – Ravinder Kumar Nov 26 '13 at 21:11

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.