switch_theme

Switches the theme.

Description

switch_theme( (string) $stylesheet ); 

Accepts one argument: $stylesheet of the theme. It also accepts an additional function signature of two arguments: $template then $stylesheet. This is for backward compatibility.

Parameters (1)

0. $stylesheet (string)
The stylesheet.

Usage

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

Defined (1)

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

/wp-includes/theme.php  
  1. function switch_theme( $stylesheet ) { 
  2. global $wp_theme_directories, $wp_customize, $sidebars_widgets; 
  3.  
  4. $_sidebars_widgets = null; 
  5. if ( 'wp_ajax_customize_save' === current_action() ) { 
  6. $_sidebars_widgets = $wp_customize->post_value( $wp_customize->get_setting( 'old_sidebars_widgets_data' ) ); 
  7. } elseif ( is_array( $sidebars_widgets ) ) { 
  8. $_sidebars_widgets = $sidebars_widgets; 
  9.  
  10. if ( is_array( $_sidebars_widgets ) ) { 
  11. set_theme_mod( 'sidebars_widgets', array( 'time' => time(), 'data' => $_sidebars_widgets ) ); 
  12.  
  13. $nav_menu_locations = get_theme_mod( 'nav_menu_locations' ); 
  14.  
  15. if ( func_num_args() > 1 ) { 
  16. $stylesheet = func_get_arg( 1 ); 
  17.  
  18. $old_theme = wp_get_theme(); 
  19. $new_theme = wp_get_theme( $stylesheet ); 
  20. $template = $new_theme->get_template(); 
  21.  
  22. update_option( 'template', $template ); 
  23. update_option( 'stylesheet', $stylesheet ); 
  24.  
  25. if ( count( $wp_theme_directories ) > 1 ) { 
  26. update_option( 'template_root', get_raw_theme_root( $template, true ) ); 
  27. update_option( 'stylesheet_root', get_raw_theme_root( $stylesheet, true ) ); 
  28. } else { 
  29. delete_option( 'template_root' ); 
  30. delete_option( 'stylesheet_root' ); 
  31.  
  32. $new_name = $new_theme->get('Name'); 
  33.  
  34. update_option( 'current_theme', $new_name ); 
  35.  
  36. // Migrate from the old mods_{name} option to theme_mods_{slug}. 
  37. if ( is_admin() && false === get_option( 'theme_mods_' . $stylesheet ) ) { 
  38. $default_theme_mods = (array) get_option( 'mods_' . $new_name ); 
  39. if ( ! empty( $nav_menu_locations ) && empty( $default_theme_mods['nav_menu_locations'] ) ) { 
  40. $default_theme_mods['nav_menu_locations'] = $nav_menu_locations; 
  41. add_option( "theme_mods_$stylesheet", $default_theme_mods ); 
  42. } else { 
  43. /** 
  44. * Since retrieve_widgets() is called when initializing a theme in the Customizer,  
  45. * we need to remove the theme mods to avoid overwriting changes made via 
  46. * the Customizer when accessing wp-admin/widgets.php. 
  47. */ 
  48. if ( 'wp_ajax_customize_save' === current_action() ) { 
  49. remove_theme_mod( 'sidebars_widgets' ); 
  50.  
  51. if ( ! empty( $nav_menu_locations ) ) { 
  52. $nav_mods = get_theme_mod( 'nav_menu_locations' ); 
  53. if ( empty( $nav_mods ) ) { 
  54. set_theme_mod( 'nav_menu_locations', $nav_menu_locations ); 
  55.  
  56. update_option( 'theme_switched', $old_theme->get_stylesheet() ); 
  57.  
  58. /** 
  59. * Fires after the theme is switched. 
  60. * @since 1.5.0 
  61. * @since 4.5.0 Introduced the `$old_theme` parameter. 
  62. * @param string $new_name Name of the new theme. 
  63. * @param WP_Theme $new_theme WP_Theme instance of the new theme. 
  64. * @param WP_Theme $old_theme WP_Theme instance of the old theme. 
  65. */ 
  66. do_action( 'switch_theme', $new_name, $new_theme, $old_theme );