Skip to content

Instantly share code, notes, and snippets.

@tripflex
Created September 10, 2015 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tripflex/b9b08d94730e2ee16434 to your computer and use it in GitHub Desktop.
Save tripflex/b9b08d94730e2ee16434 to your computer and use it in GitHub Desktop.
Method for WordPress to get theme name based on parent theme
<?php
/**
* Get current site Theme Name
*
* This method will get the theme name by default from parent theme, and
* if not set it will return the textdomain.
*
*
* @since 1.3.5
*
* @param bool|TRUE $parent Whether or not to use the parent theme if current theme is child theme
* @param bool|TRUE $return_all Should the name and textdomain be returned in an array
* @param null $return If return_all is false, provide the string variable value to return (name or textdomain)
*
* @return array|string
*/
public static function get_theme_name( $parent = TRUE, $return_all = TRUE, $return = null ){
$theme = wp_get_theme();
// Set theme object to parent theme, if the current theme is a child theme
$theme_obj = $theme->parent() && $parent ? $theme->parent() : $theme;
$name = $theme_obj->get( 'Name' );
$textdomain = $theme_obj->get( 'TextDomain' );
$version = $theme_obj->get( 'Version' );
// Use name if possible, otherwise use textdomain
$theme_name = isset($name) && ! empty($name) ? strtolower( $name ) : strtolower( $textdomain );
if( $return_all ) $return_array = array( 'name' => strtolower( $name ), 'textdomain' => strtolower( $textdomain ), 'version' => $theme_obj->get( 'Version' ), 'theme_name' => $theme_name, 'author' => $theme_obj->get('Author'), 'object' => $theme_obj );
if( $return_all ) return $return_array;
// If return is set to one of vars above (name, textdomain), and is set, return that value
if( ! empty( $return ) && is_string( $return ) && isset( $$return ) ) return $$return;
return $theme_name;
}
@damiencarbery
Copy link

Thanks for this - it was a helpful start. I used $theme->template to get the theme name to get a string without spaces e.g. "Hello Elementor" would return "hello elementor" in your code and "hello-elementor" in mine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment