当你切换主题时,有没有一个挂钩?

时间:2011-01-22 作者:Jared

我创建了一个插件,但我遇到了一个我真的不知道如何解决的bug。

当你激活我的插件时,它会在活动主题目录中创建一个文件,当你停用它时,它会删除该文件。

问题是,如果激活插件,然后切换主题,文件将不存在于新主题的目录中。是否有一行代码可以添加到我的插件函数文件中,在切换主题之前停用插件,然后在激活新主题后激活插件?

谢谢,这是一个很棒的社区,所以我知道我会得到一个很好的答案。:)

3 个回复
最合适的回答,由SO网友:prettyboymp 整理而成

切换主题后会立即运行“switch\\u theme”操作。

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

SO网友:editor

在WordPress 1.5及更高版本中,您要查找的操作被调用switch theme.

您可以在中的源代码中看到它theme.php.

SO网友:brasofilo

我也面临同样的问题,并针对我们使用的旧主题get_option(\'theme_switched\'). 此选项保存最后一个活动主题的值。

一个几乎完整的示例(仅缺少停用挂钩):

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.com/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 );
            } 
        } 
    } 
}

结束

相关推荐