Skip to main content

Get Theme Status

PropertyValue
descriptionCheck whether a theme or parent theme is active by slug.
tagslib, php, wp, oop
rating

Overview

Check whether a theme is active by slug. The helper matches the active theme and its parent theme, so it works for child theme installs as well.

Use the generic helper for any theme slug, or the explicit helpers for common themes used in our builds.

Variants

<?php
/**
* Theme status helpers.
*/

/**
* Check if a theme is active (child or parent).
*
* @param string $slug Theme slug.
* @return bool
*/
if ( ! function_exists( 'mac_is_theme_active' ) ) {

function mac_is_theme_active( string $slug ): bool {

$slug = sanitize_key( $slug );

if ( $slug === '' ) {
return false;
}

/** @var array<string,bool> $cache */
static $cache = [];

if ( isset( $cache[ $slug ] ) ) {
return $cache[ $slug ];
}

$theme = wp_get_theme();

if ( ! $theme instanceof WP_Theme ) {
return $cache[ $slug ] = false;
}

if (
$theme->stylesheet === $slug ||
$theme->template === $slug
) {
return $cache[ $slug ] = true;
}

$parent = $theme->parent();

if ( $parent instanceof WP_Theme ) {

if (
$parent->stylesheet === $slug ||
$parent->template === $slug
) {
return $cache[ $slug ] = true;
}
}

return $cache[ $slug ] = false;
}
}

/**
* Explicit theme status helpers.
*/

// Bricks
if ( ! function_exists( 'mac_is_bricks_theme_active' ) ) {

function mac_is_bricks_theme_active(): bool {
return mac_is_theme_active( 'bricks' );
}
}

// Etch
if ( ! function_exists( 'mac_is_etch_theme_active' ) ) {

function mac_is_etch_theme_active(): bool {
return mac_is_theme_active( 'etch-theme' );
}
}