validate_current_theme

The timeline below displays how wordpress function validate_current_theme has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Checks that the active theme has the required files.
 *
 * Standalone themes need to have a `templates/index.html` or `index.php` template file.
 * Child themes need to have a `Template` header in the `style.css` stylesheet.
 *
 * Does not initially check the default theme, which is the fallback and should always exist.
 * But if it doesn't exist, it'll fall back to the latest core default theme that does exist.
 * Will switch theme to the fallback theme if active theme does not validate.
 *
 * You can use the {@see 'validate_current_theme'} filter to return false to disable
 * this functionality.
 *
 * @since 1.5.0
 * @since 6.0.0 Removed the requirement for block themes to have an `index.php` template.
 *
 * @see WP_DEFAULT_THEME
 *
 * @return bool
 */
function validate_current_theme()
{
    /**
     * Filters whether to validate the active theme.
     *
     * @since 2.7.0
     *
     * @param bool $validate Whether to validate the active theme. Default true.
     */
    if (wp_installing() || !apply_filters('validate_current_theme', true)) {
        return true;
    }
    if (!file_exists(get_template_directory() . '/templates/index.html') && !file_exists(get_template_directory() . '/block-templates/index.html') && !file_exists(get_template_directory() . '/index.php')) {
        // Invalid.
    } elseif (!file_exists(get_template_directory() . '/style.css')) {
        // Invalid.
    } elseif (is_child_theme() && !file_exists(get_stylesheet_directory() . '/style.css')) {
        // Invalid.
    } else {
        // Valid.
        return true;
    }
    $default = wp_get_theme(WP_DEFAULT_THEME);
    if ($default->exists()) {
        switch_theme(WP_DEFAULT_THEME);
        return false;
    }
    /**
     * If we're in an invalid state but WP_DEFAULT_THEME doesn't exist,
     * switch to the latest core default theme that's installed.
     *
     * If it turns out that this latest core default theme is our current
     * theme, then there's nothing we can do about that, so we have to bail,
     * rather than going into an infinite loop. (This is why there are
     * checks against WP_DEFAULT_THEME above, also.) We also can't do anything
     * if it turns out there is no default theme installed. (That's `false`.)
     */
    $default = WP_Theme::get_core_default_theme();
    if (false === $default || get_stylesheet() == $default->get_stylesheet()) {
        return true;
    }
    switch_theme($default->get_stylesheet());
    return false;
}

WordPress Version: 5.5

/**
 * Checks that the current theme has 'index.php' and 'style.css' files.
 *
 * Does not initially check the default theme, which is the fallback and should always exist.
 * But if it doesn't exist, it'll fall back to the latest core default theme that does exist.
 * Will switch theme to the fallback theme if current theme does not validate.
 *
 * You can use the {@see 'validate_current_theme'} filter to return false to disable
 * this functionality.
 *
 * @since 1.5.0
 *
 * @see WP_DEFAULT_THEME
 *
 * @return bool
 */
function validate_current_theme()
{
    /**
     * Filters whether to validate the current theme.
     *
     * @since 2.7.0
     *
     * @param bool $validate Whether to validate the current theme. Default true.
     */
    if (wp_installing() || !apply_filters('validate_current_theme', true)) {
        return true;
    }
    if (!file_exists(get_template_directory() . '/index.php')) {
        // Invalid.
    } elseif (!file_exists(get_template_directory() . '/style.css')) {
        // Invalid.
    } elseif (is_child_theme() && !file_exists(get_stylesheet_directory() . '/style.css')) {
        // Invalid.
    } else {
        // Valid.
        return true;
    }
    $default = wp_get_theme(WP_DEFAULT_THEME);
    if ($default->exists()) {
        switch_theme(WP_DEFAULT_THEME);
        return false;
    }
    /**
     * If we're in an invalid state but WP_DEFAULT_THEME doesn't exist,
     * switch to the latest core default theme that's installed.
     *
     * If it turns out that this latest core default theme is our current
     * theme, then there's nothing we can do about that, so we have to bail,
     * rather than going into an infinite loop. (This is why there are
     * checks against WP_DEFAULT_THEME above, also.) We also can't do anything
     * if it turns out there is no default theme installed. (That's `false`.)
     */
    $default = WP_Theme::get_core_default_theme();
    if (false === $default || get_stylesheet() == $default->get_stylesheet()) {
        return true;
    }
    switch_theme($default->get_stylesheet());
    return false;
}

WordPress Version: 4.6

/**
 * Checks that current theme files 'index.php' and 'style.css' exists.
 *
 * Does not initially check the default theme, which is the fallback and should always exist.
 * But if it doesn't exist, it'll fall back to the latest core default theme that does exist.
 * Will switch theme to the fallback theme if current theme does not validate.
 *
 * You can use the {@see 'validate_current_theme'} filter to return false to
 * disable this functionality.
 *
 * @since 1.5.0
 * @see WP_DEFAULT_THEME
 *
 * @return bool
 */
function validate_current_theme()
{
    /**
     * Filters whether to validate the current theme.
     *
     * @since 2.7.0
     *
     * @param bool $validate Whether to validate the current theme. Default true.
     */
    if (wp_installing() || !apply_filters('validate_current_theme', true)) {
        return true;
    }
    if (!file_exists(get_template_directory() . '/index.php')) {
        // Invalid.
    } elseif (!file_exists(get_template_directory() . '/style.css')) {
        // Invalid.
    } elseif (is_child_theme() && !file_exists(get_stylesheet_directory() . '/style.css')) {
        // Invalid.
    } else {
        // Valid.
        return true;
    }
    $default = wp_get_theme(WP_DEFAULT_THEME);
    if ($default->exists()) {
        switch_theme(WP_DEFAULT_THEME);
        return false;
    }
    /**
     * If we're in an invalid state but WP_DEFAULT_THEME doesn't exist,
     * switch to the latest core default theme that's installed.
     * If it turns out that this latest core default theme is our current
     * theme, then there's nothing we can do about that, so we have to bail,
     * rather than going into an infinite loop. (This is why there are
     * checks against WP_DEFAULT_THEME above, also.) We also can't do anything
     * if it turns out there is no default theme installed. (That's `false`.)
     */
    $default = WP_Theme::get_core_default_theme();
    if (false === $default || get_stylesheet() == $default->get_stylesheet()) {
        return true;
    }
    switch_theme($default->get_stylesheet());
    return false;
}

WordPress Version: 4.5

/**
 * Checks that current theme files 'index.php' and 'style.css' exists.
 *
 * Does not initially check the default theme, which is the fallback and should always exist.
 * But if it doesn't exist, it'll fall back to the latest core default theme that does exist.
 * Will switch theme to the fallback theme if current theme does not validate.
 *
 * You can use the 'validate_current_theme' filter to return false to
 * disable this functionality.
 *
 * @since 1.5.0
 * @see WP_DEFAULT_THEME
 *
 * @return bool
 */
function validate_current_theme()
{
    /**
     * Filter whether to validate the current theme.
     *
     * @since 2.7.0
     *
     * @param bool $validate Whether to validate the current theme. Default true.
     */
    if (wp_installing() || !apply_filters('validate_current_theme', true)) {
        return true;
    }
    if (!file_exists(get_template_directory() . '/index.php')) {
        // Invalid.
    } elseif (!file_exists(get_template_directory() . '/style.css')) {
        // Invalid.
    } elseif (is_child_theme() && !file_exists(get_stylesheet_directory() . '/style.css')) {
        // Invalid.
    } else {
        // Valid.
        return true;
    }
    $default = wp_get_theme(WP_DEFAULT_THEME);
    if ($default->exists()) {
        switch_theme(WP_DEFAULT_THEME);
        return false;
    }
    /**
     * If we're in an invalid state but WP_DEFAULT_THEME doesn't exist,
     * switch to the latest core default theme that's installed.
     * If it turns out that this latest core default theme is our current
     * theme, then there's nothing we can do about that, so we have to bail,
     * rather than going into an infinite loop. (This is why there are
     * checks against WP_DEFAULT_THEME above, also.) We also can't do anything
     * if it turns out there is no default theme installed. (That's `false`.)
     */
    $default = WP_Theme::get_core_default_theme();
    if (false === $default || get_stylesheet() == $default->get_stylesheet()) {
        return true;
    }
    switch_theme($default->get_stylesheet());
    return false;
}

WordPress Version: 4.4

/**
 * Checks that current theme files 'index.php' and 'style.css' exists.
 *
 * Does not initially check the default theme, which is the fallback and should always exist.
 * But if it doesn't exist, it'll fall back to the latest core default theme that does exist.
 * Will switch theme to the fallback theme if current theme does not validate.
 *
 * You can use the 'validate_current_theme' filter to return false to
 * disable this functionality.
 *
 * @since 1.5.0
 * @see WP_DEFAULT_THEME
 *
 * @return bool
 */
function validate_current_theme()
{
    /**
     * Filter whether to validate the current theme.
     *
     * @since 2.7.0
     *
     * @param bool true Validation flag to check the current theme.
     */
    if (wp_installing() || !apply_filters('validate_current_theme', true)) {
        return true;
    }
    if (!file_exists(get_template_directory() . '/index.php')) {
        // Invalid.
    } elseif (!file_exists(get_template_directory() . '/style.css')) {
        // Invalid.
    } elseif (is_child_theme() && !file_exists(get_stylesheet_directory() . '/style.css')) {
        // Invalid.
    } else {
        // Valid.
        return true;
    }
    $default = wp_get_theme(WP_DEFAULT_THEME);
    if ($default->exists()) {
        switch_theme(WP_DEFAULT_THEME);
        return false;
    }
    /**
     * If we're in an invalid state but WP_DEFAULT_THEME doesn't exist,
     * switch to the latest core default theme that's installed.
     * If it turns out that this latest core default theme is our current
     * theme, then there's nothing we can do about that, so we have to bail,
     * rather than going into an infinite loop. (This is why there are
     * checks against WP_DEFAULT_THEME above, also.) We also can't do anything
     * if it turns out there is no default theme installed. (That's `false`.)
     */
    $default = WP_Theme::get_core_default_theme();
    if (false === $default || get_stylesheet() == $default->get_stylesheet()) {
        return true;
    }
    switch_theme($default->get_stylesheet());
    return false;
}

WordPress Version: 3.8

/**
 * Checks that current theme files 'index.php' and 'style.css' exists.
 *
 * Does not check the default theme, which is the fallback and should always exist.
 * Will switch theme to the fallback theme if current theme does not validate.
 * You can use the 'validate_current_theme' filter to return false to
 * disable this functionality.
 *
 * @since 1.5.0
 * @see WP_DEFAULT_THEME
 *
 * @return bool
 */
function validate_current_theme()
{
    /**
     * Filter whether to validate the current theme.
     *
     * @since 2.7.0
     *
     * @param bool true Validation flag to check the current theme.
     */
    if (defined('WP_INSTALLING') || !apply_filters('validate_current_theme', true)) {
        return true;
    }
    if (get_template() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/index.php')) {
        switch_theme(WP_DEFAULT_THEME);
        return false;
    }
    if (get_stylesheet() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/style.css')) {
        switch_theme(WP_DEFAULT_THEME);
        return false;
    }
    if (is_child_theme() && !file_exists(get_stylesheet_directory() . '/style.css')) {
        switch_theme(WP_DEFAULT_THEME);
        return false;
    }
    return true;
}

WordPress Version: 3.7

/**
 * Checks that current theme files 'index.php' and 'style.css' exists.
 *
 * Does not check the default theme, which is the fallback and should always exist.
 * Will switch theme to the fallback theme if current theme does not validate.
 * You can use the 'validate_current_theme' filter to return false to
 * disable this functionality.
 *
 * @since 1.5.0
 * @see WP_DEFAULT_THEME
 *
 * @return bool
 */
function validate_current_theme()
{
    // Don't validate during an install/upgrade.
    if (defined('WP_INSTALLING') || !apply_filters('validate_current_theme', true)) {
        return true;
    }
    if (get_template() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/index.php')) {
        switch_theme(WP_DEFAULT_THEME);
        return false;
    }
    if (get_stylesheet() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/style.css')) {
        switch_theme(WP_DEFAULT_THEME);
        return false;
    }
    if (is_child_theme() && !file_exists(get_stylesheet_directory() . '/style.css')) {
        switch_theme(WP_DEFAULT_THEME);
        return false;
    }
    return true;
}