switch_theme()将当前主题转换为新模板和新的样式表单名称

目录

描述

译文

将当前主题转换为新模板和新的样式表单名称

原文

Switches current theme to new template and stylesheet names.

Accepts one argument: $stylesheet of the theme. ($stylesheet is the name of your folder slug. It's the same value that you'd use for a child theme, something like `twentythirteen`.) It also accepts an additional function signature of two arguments: $template then $stylesheet. This is for backwards compatibility.

switch_theme() 描述

用法

<?php switch_theme$stylesheet ?>Optional usage for backwards compatibility:<?php switch_theme$template$stylesheet ?>

switch_theme() 用法

参数

Current Parameters

$stylesheet

(string) (必填) Stylesheet name.

默认值: None

Alternate Function Signature Parameters for Backwards Compatibility

$template

(string) (必填) Template name.

默认值: None

$stylesheet

(string) (必填) Stylesheet name.

默认值: None

switch_theme() 参数

返回值

(void) 

This function does not return a value.

switch_theme() 返回值

注意

  • 使用到: do_action() 调用 'switch_theme' 动作 on updated theme display name.

switch_theme() 注意

历史

添加于 版本: 2.5.0

Altered 4/23 to define default/alternate function signature and 参数

switch_theme() 历史

源文件

switch_theme() 函数的代码位于 wp-includes/theme.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Switches the theme.
 *
 * Accepts one argument: $stylesheet of the theme. It also accepts an additional function signature
 * of two arguments: $template then $stylesheet. This is for backwards compatibility.
 *
 * @since 2.5.0
 *
 * @global array                $wp_theme_directories
 * @global WP_Customize_Manager $wp_customize
 * @global array                $sidebars_widgets
 *
 * @param string $stylesheet Stylesheet name
 */
function switch_theme( $stylesheet ) {
	global $wp_theme_directories, $wp_customize, $sidebars_widgets;

	$_sidebars_widgets = null;
	if ( 'wp_ajax_customize_save' === current_action() ) {
		$_sidebars_widgets = $wp_customize->post_value( $wp_customize->get_setting( 'old_sidebars_widgets_data' ) );
	} elseif ( is_array( $sidebars_widgets ) ) {
		$_sidebars_widgets = $sidebars_widgets;
	}

	if ( is_array( $_sidebars_widgets ) ) {
		set_theme_mod( 'sidebars_widgets', array( 'time' => time(), 'data' => $_sidebars_widgets ) );
	}

	$old_theme  = wp_get_theme();
	$new_theme = wp_get_theme( $stylesheet );

	if ( func_num_args() > 1 ) {
		$template = $stylesheet;
		$stylesheet = func_get_arg( 1 );
	} else {
		$template = $new_theme->get_template();
	}

	update_option( 'template', $template );
	update_option( 'stylesheet', $stylesheet );

	if ( count( $wp_theme_directories ) > 1 ) {
		update_option( 'template_root', get_raw_theme_root( $template, true ) );
		update_option( 'stylesheet_root', get_raw_theme_root( $stylesheet, true ) );
	} else {
		delete_option( 'template_root' );
		delete_option( 'stylesheet_root' );
	}

	$new_name  = $new_theme->get('Name');

	update_option( 'current_theme', $new_name );

	// Migrate from the old mods_{name} option to theme_mods_{slug}.
	if ( is_admin() && false === get_option( 'theme_mods_' . $stylesheet ) ) {
		$default_theme_mods = (array) get_option( 'mods_' . $new_name );
		add_option( "theme_mods_$stylesheet", $default_theme_mods );
	} else {
		/*
		 * Since retrieve_widgets() is called when initializing a theme in the Customizer,
		 * we need to to remove the theme mods to avoid overwriting changes made via
		 * the Customizer when accessing wp-admin/widgets.php.
		 */
		if ( 'wp_ajax_customize_save' === current_action() ) {
			remove_theme_mod( 'sidebars_widgets' );
		}
	}

	update_option( 'theme_switched', $old_theme->get_stylesheet() );
	/**
	 * Fires after the theme is switched.
	 *
	 * @since 1.5.0
	 *
	 * @param string   $new_name  Name of the new theme.
	 * @param WP_Theme $new_theme WP_Theme instance of the new theme.
	 */
	do_action( 'switch_theme', $new_name, $new_theme );
}

switch_theme() 源文件