How to test if your theme is a Child Theme

wordpress-icon
You may need to know which of your themes are child themes, or in fact if the current theme you’re using is a parent or a child theme.

Here’s how you can test both options.

The following code snippet will iterate through all themes that are currently installed and displays the title and if it is a child theme or not:

$allThemes = wp_get_themes();
echo '<ol>';
foreach ($allThemes as $theme) {
	echo '<li>';

	// print the theme title
	echo $theme->get('Name');

	// determine whether it's a child theme or not
	if ($theme->parent() == false) {
		echo ' is not a Child Theme.</li>';
	} else {
		echo ' is a Child Theme</li>';
	}
}
echo '</ol>';

First we grab an array of installed themes using wp_get_themes(). Each item is an object of WP_Theme and has many helpful methods. Its method parent() will return false for non-child themes, or the parent theme if it is in fact a child theme.

Next we test if the output is false, and if so print a status accordingly.

Is the current theme a child theme?

Here’s how we can check it:

// is the current theme a child theme?
$currentTheme = wp_get_theme();
if ($currentTheme->parent() == false) {
	echo 'The current theme is not a child theme.';
} else {
	echo 'The current theme is a child theme';
}

Here we employ very much the same, except for the first line in which we grab only a single object which defaults to the current theme.

Check out all the other bits of info this class can provide.





Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.