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.
function my_on_switch_theme($new_theme) {
$current_themes = wp_get_themes(); /* Fixed deprecated function */
$new_theme_info = $current_themes[$new_theme];
/*
$new_theme_info should now be an associative array with the following:
$new_theme_info['Title'];
$new_theme_info['Version'];
$new_theme_info['Parent Theme'];
$new_theme_info['Template Dir'];
$new_theme_info['Stylesheet Dir'];
$new_theme_info['Template'];
$new_theme_info['Stylesheet'];
$new_theme_info['Screenshot'];
$new_theme_info['Description'];
$new_theme_info['Author'];
$new_theme_info['Tags'];
$new_theme_info['Theme Root'];
$new_theme_info['Theme Root URI'];
...so do what you need from this.
*/
}
add_action('switch_theme', 'my_on_switch_theme');