web-dev-qa-db-fra.com

Y a-t-il un crochet pour changer de thème?

J'ai créé un plugin mais je viens de rencontrer un bug que je ne sais pas vraiment comment résoudre.

Lorsque vous activez mon plug-in, il crée un fichier dans le répertoire du thème actif et lorsque vous le désactivez, il supprime ce fichier.

Le problème est que, si vous activez le plug-in, puis changez de thème, les fichiers n'existeront pas dans le répertoire du nouveau thème. Y a-t-il une ligne de code que je peux ajouter dans mon fichier de fonctions de plug-in pour désactiver le plug-in avant que le thème ne soit commuté, puis l'activer après l'activation du nouveau thème?

Merci, cette communauté est géniale et je sais que je vais obtenir une excellente réponse. :)

5
Jared

Une action 'switch_theme' est exécutée juste après le changement de thème.

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_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 what you need from this.
    */
}
add_action('switch_theme', 'my_on_switch_theme');
7
prettyboymp

Dans WordPress 1.5 et supérieur, l'action que vous recherchez s'appelle changer de thème .

Vous pouvez le voir dans source dans theme.php .

2
editor

J'ai fait face au même problème et pour cibler le vieux thème que nous utilisons get_option('theme_switched'). Cette option contient la valeur du dernier thème actif.

Un exemple presque complet (il ne manque que le crochet de désactivation):

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 );
            } 
        } 
    } 
}
1
brasofilo