Paramètres

$stylesheet

(string) (Optionnel) Nom du dossier du thème choisi.

Valeur par défaut : ''

$theme_root

(string) (Optionnel) Chemin absolu de la racine du thème à chercher.

Valeur par défaut : ''

Retourne

(WP_Theme) Retourne l'objet WP_Theme. Si vous avez besoin de confirmer l'existence du thème, vérifier que l'objet existe avec la méthode WP_Theme->exists().

Structure de la fonction wp_get_theme()

Définie dans le fichier wp-includes/theme.php à la ligne 115 :

function wp_get_theme( $stylesheet = '', $theme_root = '' ) {
    global $wp_theme_directories;

    if ( empty( $stylesheet ) ) {
        $stylesheet = get_stylesheet();
    }

    if ( empty( $theme_root ) ) {
        $theme_root = get_raw_theme_root( $stylesheet );
        if ( false === $theme_root ) {
            $theme_root = WP_CONTENT_DIR . '/themes';
        } elseif ( ! in_array( $theme_root, (array) $wp_theme_directories, true ) ) {
            $theme_root = WP_CONTENT_DIR . $theme_root;
        }
    }

    return new WP_Theme( $stylesheet, $theme_root );
}

Fonctions utilisées par wp_get_theme()

get_stylesheet()

Récupère le nom de la feuille de style actuelle.

get_raw_theme_root()

Génère la racine originale du thème relative au dossier wp-content.

Où trouver la fonction wp_get_theme() dans le CMS Wordpress

Exemples

Affiche le nom d'un thème installé :
$my_theme = wp_get_theme( 'twentytwelve' );
if ( $my_theme->exists() )
    echo esc_html( $my_theme );
Résultat :
object(WP_Theme)[916]
  public 'update' => boolean false
  private 'theme_root' => string 'home/path/wp-content/themes' (length=77)
  private 'headers' => 
    array (size=11)
      'Name' => string 'mytheme' (length=7)
      'ThemeURI' => string 'http://example.com/' (length=22)
      'Description' => string 'Description' (length=11)
      'Author' => string 'Something Here' (length=14)
      'AuthorURI' => string 'http://example.com/' (length=22)
      'Version' => string '1.0.0' (length=5)
      'Template' => string '' (length=0)
      'Status' => string '' (length=0)
      'Tags' => string 'custom-background, custom-logo, custom-menu, featured-images, threaded-comments, translation-ready' (length=98)
      'TextDomain' => string 'mytheme' (length=7)
      'DomainPath' => string '' (length=0)
  private 'headers_sanitized' => null
  private 'name_translated' => null
  private 'errors' => null
  private 'stylesheet' => string 'mytheme' (length=7)
  private 'template' => string 'mytheme' (length=7)
  private 'parent' => null
  private 'theme_root_uri' => null
  private 'textdomain_loaded' => null
  private 'cache_hash' => string 'ca9dd01f01f2a5cb4616a776eff52690' (length=32)
obtenir d'autres données : domaine de texte et l'URI du thème
$my_theme = wp_get_theme();
echo esc_html( $my_theme->get( 'TextDomain' ) );
echo esc_html( $my_theme->get( 'ThemeURI' ) );

Sources

Codex Wordpress : wp_get_theme()

Autres fonctions dans le même fichier : wp-includes/theme.php

Retour