I'm able to get certain info about the active theme using wp_get_theme()
. For example:
$theme = wp_get_theme();
echo $theme->get( 'TextDomain' ); // twentyfifteen
echo $theme->get( 'ThemeURI' ); // /
Is there a way to get the theme's slug? In this case it'd be twentyfifteen. Please note the theme's slug isn't always the same as the theme's text domain. I'd also like to avoid performing string replacement on the theme's URL if possible.
Ref:
I'm able to get certain info about the active theme using wp_get_theme()
. For example:
$theme = wp_get_theme();
echo $theme->get( 'TextDomain' ); // twentyfifteen
echo $theme->get( 'ThemeURI' ); // https://wordpress/themes/twentyfifteen/
Is there a way to get the theme's slug? In this case it'd be twentyfifteen. Please note the theme's slug isn't always the same as the theme's text domain. I'd also like to avoid performing string replacement on the theme's URL if possible.
Ref: https://codex.wordpress/Function_Reference/wp_get_theme
You can get the slug in the options
table, stored under the name stylesheet
.
echo get_option('stylesheet');
Short Answer: get_stylesheet();
There is technically no 'slug' value for a theme. The name of a given theme's directory is what you want.
get_template();
…will return the directory name of your theme, or the parent theme in the case that your current theme is a child theme.
get_option('stylesheet');
Will ALWAYS return the directory name of your active theme, whether or not it is a child theme.
get_stylesheet();
Will ALWAYS return the directory name of your active theme, whether or not it is a child theme. This function is essentially a wrapper for get_option('stylesheet');
, except that it also applies a 'stylesheet' filter.
function get_stylesheet() {
/**
* Filters the name of current stylesheet.
*
* @since 1.5.0
*
* @param string $stylesheet Name of the current stylesheet.
*/
return apply_filters( 'stylesheet', get_option( 'stylesheet' ) );
}
I'm not sure what the 'stylesheet' filter does. Looks like it might have something to do with the customizer.
In the vast majority of cases, these three functions would do the same thing, but get_stylesheet();
seems like the safest bet.
wp_get_theme
gets a WP_Theme object for a theme.
$theme = wp_get_theme();
if ( 'Conj' === $theme->name || 'conj' === $theme->template ) {
// Do something...
}
I found the closest thing to the theme's slug is the theme's directory name. This can be found using get_template()
:
echo get_template(); // twentyfifteen
Ref: https://codex.wordpress/Function_Reference/get_template
wp_get_active_and_valid_themes()
i found this in wp-settings.php
// Load the functions for the active theme, for both parent and child theme if applicable.
foreach ( wp_get_active_and_valid_themes() as $theme ) {
if ( file_exists( $theme . '/functions.php' ) ) {
include $theme . '/functions.php';
}
}
Yo can get it by get_template_directory_uri()