I created a plugin but I just came across a bug that I don’t really know how to solve.
When you activate my plugin, it creates a file in the active theme directory and when you deactivate it, it deletes that file.
The problem is, if you activate the plugin, and then switch the theme, the files won’t exist in the new theme’s directory. Is there a line of code I can add in my plugins functions file to deactivate the plugin before the theme is switched, and then activate it after the new theme has been activated?
Thanks, this is a great community so I know I’ll get a great answer. 🙂
There is a ‘switch_theme’ action that runs right after the theme is switched.
In WordPress 1.5 and greater the action you’re looking for is called switch theme.
You can see it in source in theme.php.
I faced the same issue and to target the old theme we use
get_option('theme_switched')
. This option holds the value of the last active theme.An almost complete example (only missing the deactivation hook):