validate_current_theme

Checks that current theme files 'index.php' and 'style.css' exists.

Description

validate_current_theme(); 

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 filter to return false to disable this functionality.


Usage

  1. if ( !function_exists( 'validate_current_theme' ) ) { 
  2. require_once ABSPATH . WPINC . '/theme.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = validate_current_theme(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/theme.php  
  1. function validate_current_theme() { 
  2. /** 
  3. * Filters whether to validate the current theme. 
  4. * @since 2.7.0 
  5. * @param bool $validate Whether to validate the current theme. Default true. 
  6. */ 
  7. return true; 
  8.  
  9. if ( ! file_exists( get_template_directory() . '/index.php' ) ) { 
  10. // Invalid. 
  11. } elseif ( ! file_exists( get_template_directory() . '/style.css' ) ) { 
  12. // Invalid. 
  13. } elseif ( is_child_theme() && ! file_exists( get_stylesheet_directory() . '/style.css' ) ) { 
  14. // Invalid. 
  15. } else { 
  16. // Valid. 
  17. return true; 
  18.  
  19. $default = wp_get_theme( WP_DEFAULT_THEME ); 
  20. if ( $default->exists() ) { 
  21. return false; 
  22.  
  23. /** 
  24. * If we're in an invalid state but WP_DEFAULT_THEME doesn't exist,  
  25. * switch to the latest core default theme that's installed. 
  26. * If it turns out that this latest core default theme is our current 
  27. * theme, then there's nothing we can do about that, so we have to bail,  
  28. * rather than going into an infinite loop. (This is why there are 
  29. * checks against WP_DEFAULT_THEME above, also.) We also can't do anything 
  30. * if it turns out there is no default theme installed. (That's `false`.) 
  31. */ 
  32. $default = WP_Theme::get_core_default_theme(); 
  33. if ( false === $default || get_stylesheet() == $default->get_stylesheet() ) { 
  34. return true; 
  35.  
  36. switch_theme( $default->get_stylesheet() ); 
  37. return false;