is_child_theme()WP 3.0.0

Checks if a child theme is being used. Conditional tag.

The function returns true if the current theme is a child theme, meaning it has a parent theme on which it is based. Otherwise, it returns false.

The function compares the path to the current active theme (get_template_directory()) with the path to the child theme (get_stylesheet_directory()). If they differ — it is a child theme.

Where it is used:

  • Often used to load different files or styles depending on whether a child theme is being used.
  • Can help in the development of plugins or themes where behavior depends on the presence of a child theme.
  • A simple way to check if the theme has been overridden.
1 time — 0.00001 sec (speed of light) | 50000 times — 0.01 sec (speed of light) | PHP 7.0.8, WP 4.6

No Hooks.

Returns

true|false.

  • true if a child theme is being used.
  • false if the parent theme is used.

Usage

if( is_child_theme() ){
	// child theme
}

Examples

2

#1 Displaying a warning for the developer

if ( ! is_child_theme() ) {
	echo '<div class="notice">You are not using a child theme. Changes may be lost during updates.</div>';
}
0

#2 Run code for the child theme only

Suppose we are writing a theme and we only need to execute PHP code when our theme is used as a parent theme, i.e. a child theme is defined and used:

if( is_child_theme() ){
	echo 'The code is for the child themes, not the parent;
}
0

#3 File upload only from the child theme

if ( is_child_theme() ) {
	require get_stylesheet_directory() . '/child-functions.php';
}

Notes

  • Global. String. $wp_stylesheet_path Path to current theme's stylesheet directory.
  • Global. String. $wp_template_path Path to current theme's template directory.

Changelog

Since 3.0.0 Introduced.
Since 6.5.0 Makes use of global template variables.

is_child_theme() code WP 6.9

function is_child_theme() {
	global $wp_stylesheet_path, $wp_template_path;

	return $wp_stylesheet_path !== $wp_template_path;
}