Come ottenere lo slug del tema attivo?
Posso ottenere alcune informazioni sul tema attivo utilizzando wp_get_theme()
. Ad esempio:
$theme = wp_get_theme();
echo $theme->get( 'TextDomain' ); // twentyfifteen
echo $theme->get( 'ThemeURI' ); // https://wordpress.org/themes/twentyfifteen/
Esiste un modo per ottenere lo slug del tema? In questo caso sarebbe twentyfifteen. Nota che lo slug del tema non sempre corrisponde al text domain del tema. Vorrei anche evitare, se possibile, di dover manipolare l'URL del tema.
Rif: https://codex.wordpress.org/Function_Reference/wp_get_theme
Risposta Breve: get_stylesheet();
Tecnicamente non esiste un valore 'slug' per un tema. Il nome della directory di un determinato tema è ciò che si desidera.
get_template();
…restituirà il nome della directory del tuo tema, o del tema genitore nel caso in cui il tuo tema corrente sia un tema figlio.
get_option('stylesheet');
Restituirà SEMPRE il nome della directory del tuo tema attivo, indipendentemente dal fatto che sia un tema figlio o meno.
get_stylesheet();
Restituirà SEMPRE il nome della directory del tuo tema attivo, indipendentemente dal fatto che sia un tema figlio o meno. Questa funzione è essenzialmente un wrapper per get_option('stylesheet');
, con la differenza che applica anche un filtro 'stylesheet'.
function get_stylesheet() {
/**
* Filtra il nome del foglio di stile corrente.
*
* @since 1.5.0
*
* @param string $stylesheet Nome del foglio di stile corrente.
*/
return apply_filters( 'stylesheet', get_option( 'stylesheet' ) );
}
Non sono sicuro di cosa faccia il filtro 'stylesheet'. Sembra che possa avere qualcosa a che fare con il personalizzatore.
Nella stragrande maggioranza dei casi, queste tre funzioni farebbero la stessa cosa, ma get_stylesheet();
sembra essere la scelta più sicura.

wp_get_theme
ottiene un oggetto WP_Theme per un tema.
$theme = wp_get_theme();
if ( 'Conj' === $theme->name || 'conj' === $theme->template ) {
// Fai qualcosa...
}

Ho scoperto che la cosa più vicina allo slug del tema è il nome della directory del tema. Questo può essere trovato utilizzando get_template()
:
echo get_template(); // twentyfifteen
Riferimento: https://codex.wordpress.org/Function_Reference/get_template

wp_get_active_and_valid_themes()
Ho trovato questo in wp-settings.php
// Carica le funzioni per il tema attivo, sia per il tema genitore che per il tema figlio se applicabile.
foreach ( wp_get_active_and_valid_themes() as $theme ) {
if ( file_exists( $theme . '/functions.php' ) ) {
include $theme . '/functions.php';
}
}
