admin管理员组

文章数量:1336645

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

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 asked Jan 22, 2011 at 22:15 JaredJared 3,8453 gold badges35 silver badges58 bronze badges 3
  • Ehm, context? I don't understand while plugin needs to mess with theme's directory rather than use its own or wp-content? – Rarst Commented Jan 22, 2011 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 Commented Jan 23, 2011 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 Commented Jan 23, 2011 at 11:21
Add a comment  | 

3 Answers 3

Reset to default 6

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

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

register_activation_hook( __FILE__, 'make_copy_wpse_7518' );

add_action( 'switch_theme', 'switching_theme_wpse_7518', 10, 2 );

/**
 * Theme switch
 */
function switching_theme_wpse_7518( $new_name, $new_theme )
{
    # Remove template from old theme
    $old_theme = get_option( 'theme_switched' );
    // I thought that get_theme_root would return the path to the old theme, but apparently not, hence the second $old_theme
    $template_path = get_theme_root( $old_theme ) . "/$old_theme/plugin-template-file.php";    
    if( file_exists( $template_path ) )
        unlink( $template_path );

    # Copy template to newly activated theme    
    make_copy_wpse_7518();
}

/**
 * Copy function, called on plugin activation and theme swap
 */
function make_copy_wpse_7518()
{
    $source = dirname( __FILE__ ) . "/includes/plugin-template-file.php";
    $destination = get_stylesheet_directory() . "/plugin-template-file.php";
    copy_page_template_wpse_7518( $source, $destination );
}

/**
 * Does the actual copy from plugin to template directory
 * From https://github/tommcfarlin/page-template-example/
 */
function copy_page_template_wpse_7518( $source, $destination )  
{
    // Check if template already exists. If so don't copy it; otherwise, copy if
    if( ! file_exists( $destination ) ) 
    {
        // Create an empty version of the file
        touch( $destination );

        // Read the source file starting from the beginning of the file
        if( null != ( $handle = @fopen( $source, 'r' ) ) ) 
        {
            // Read the contents of the file into a string. 
            // Read up to the length of the source file
            if( null != ( $content = fread( $handle, filesize( $source ) ) ) ) 
            {
                // Relinquish the resource
                fclose( $handle );
            } 
        } 

        // Now open the file for reading and writing
        if( null != ( $handle = @fopen( $destination, 'r+' ) ) ) 
        {
            // Attempt to write the contents of the string
            if( null != fwrite( $handle, $content, strlen( $content ) ) ) 
            {
                // Relinquish the resource
                fclose( $handle );
            } 
        } 
    } 
}

本文标签: plugin developmentIs there a hook for when you switch themes