switch_theme() - WordPress 函数参考中文文档

返回介绍

switch_theme()

发布于 2017-09-11 字数4659 浏览 831 评论 0

switch_theme( string $stylesheet )

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 backward compatibility.


参数

$stylesheet

(string)
(Required)
Stylesheet name


源代码

File: wp-includes/theme.php

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 ) );
	}

	$nav_menu_locations = get_theme_mod( 'nav_menu_locations' );

	if ( func_num_args() > 1 ) {
		$stylesheet = func_get_arg( 1 );
	}

	$old_theme = wp_get_theme();
	$new_theme = wp_get_theme( $stylesheet );
	$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 );
		if ( ! empty( $nav_menu_locations ) && empty( $default_theme_mods['nav_menu_locations'] ) ) {
			$default_theme_mods['nav_menu_locations'] = $nav_menu_locations;
		}
		add_option( "theme_mods_$stylesheet", $default_theme_mods );
	} else {
		/*
		 * Since retrieve_widgets() is called when initializing a theme in the Customizer,
		 * we need 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' );
		}

		if ( ! empty( $nav_menu_locations ) ) {
			$nav_mods = get_theme_mod( 'nav_menu_locations' );
			if ( empty( $nav_mods ) ) {
				set_theme_mod( 'nav_menu_locations', $nav_menu_locations );
			}
		}
	}

	update_option( 'theme_switched', $old_theme->get_stylesheet() );

	/**
	 * Fires after the theme is switched.
	 *
	 * @since 1.5.0
	 * @since 4.5.0 Introduced the `$old_theme` parameter.
	 *
	 * @param string   $new_name  Name of the new theme.
	 * @param WP_Theme $new_theme WP_Theme instance of the new theme.
	 * @param WP_Theme $old_theme WP_Theme instance of the old theme.
	 */
	do_action( 'switch_theme', $new_name, $new_theme, $old_theme );
}

更新日志

Version 描述
2.5.0 Introduced.

相关函数

Uses

  • wp-includes/theme.php:
    set_theme_mod()
  • wp-includes/theme.php:
    remove_theme_mod()
  • wp-includes/theme.php:
    get_theme_mod()
  • wp-includes/theme.php:
    get_raw_theme_root()
  • wp-includes/theme.php:
    switch_theme
  • wp-includes/theme.php:
    wp_get_theme()
  • wp-includes/load.php:
    is_admin()
  • wp-includes/plugin.php:
    current_action()
  • wp-includes/plugin.php:
    do_action()
  • wp-includes/option.php:
    update_option()
  • wp-includes/option.php:
    delete_option()
  • wp-includes/option.php:
    add_option()
  • wp-includes/option.php:
    get_option()
  • Show 8 more uses
    Hide more uses


Used By

  • wp-includes/class-wp-customize-manager.php:
    WP_Customize_Manager::save_changeset_post()
  • wp-admin/includes/class-theme-upgrader.php:
    Theme_Upgrader::current_after()
  • wp-includes/theme.php:
    validate_current_theme()

User Contributed Notes

上一篇:submit_button()

下一篇:sync_category_tag_slugs()

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!