Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
Ehm, context? I don't understand while plugin needs to mess with theme's directory rather than use its own or wp-content? – Rarst Jan 22 '11 at 22:37
@Rarst Because the plugin that I created adds 1 file to the active theme directory, and that file is a custom page template for a squeeze page. It's actually quite useful for marketers. No need for an extra theme/WP installation. – Jared Jan 23 '11 at 3:02
hmmmm, interesting... I wonder if it's possible to hook in named page template that isn't in theme's folder... – Rarst Jan 23 '11 at 11:21

2 Answers

up vote 4 down vote accepted

There is a 'switch_theme' action that runs right after the theme is switched.

function my_on_switch_theme($new_theme) {
    $current_themes = get_themes();
    $new_theme_info = $current_themes[$new_theme];
    /*
    $new_theme_info bhould now be an associative array with the following:
    $new_them_info['Title'];
    $new_them_info['Version'];
    $new_them_info['Parent Theme'];
    $new_them_info['Template Dir'];
    $new_them_info['Stylesheet Dir'];
    $new_them_info['Template'];
    $new_them_info['Stylesheet'];
    $new_them_info['Screenshot'];
    $new_them_info['Description'];
    $new_them_info['Author'];
    $new_them_info['Tags'];
    $new_them_info['Theme Root'];
    $new_them_info['Theme Root URI'];
    ...so do you what you need from this.
    */
}
add_action('switch_theme', 'my_on_switch_theme');
share|improve this answer
Thanks, that is what I am looking for, but there's not much documentation on it. Do you know how to use this, like could you give me an example? – Jared Jan 23 '11 at 3:01

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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