The is_child_theme() WordPress PHP function checks if a child theme is currently in use.
Usage
$is_child = is_child_theme();
Input: None
Output: Returns true if a child theme is in use, false otherwise.
Parameters
- None
More information
See WordPress Developer Resources: is_child_theme()
Important Note: This function uses the TEMPLATEPATH and STYLESHEETPATH constants internally. If calling from a plugin, such as during plugin activation, there’s a chance these will be undefined. It is better to use: get_template_directory() !== get_stylesheet_directory()
Examples
Display a message if a child theme is in use
This code displays a message on the website if a child theme is currently active.
if (is_child_theme()) {
echo 'This website is using a child theme.';
}
Enqueue child theme styles
This code enqueues the child theme’s style.css file if a child theme is in use.
function my_enqueue_styles() {
if (is_child_theme()) {
wp_enqueue_style('child-theme-style', get_stylesheet_uri());
}
}
add_action('wp_enqueue_scripts', 'my_enqueue_styles');
Add custom functionality for child themes
This code adds custom functionality only when a child theme is active.
if (is_child_theme()) {
function my_child_theme_function() {
// Custom functionality for child themes
}
add_action('init', 'my_child_theme_function');
}
Add a custom class to the body if a child theme is in use
This code adds a custom CSS class to the body element if a child theme is active.
function my_child_theme_body_class($classes) {
if (is_child_theme()) {
$classes[] = 'using-child-theme';
}
return $classes;
}
add_filter('body_class', 'my_child_theme_body_class');
Modify a template part for child themes
This code modifies a template part for child themes, replacing the parent theme’s template-parts/content with template-parts/content-child.
function my_child_theme_template_part() {
$template_part = 'template-parts/content';
if (is_child_theme()) {
$template_part = 'template-parts/content-child';
}
get_template_part($template_part);
}