Method for WordPress to get theme name based on parent theme

get_theme_name.php

<?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;
}
No comments yet.

Leave a Reply