3 * Theme, template, and stylesheet functions.
10 * Returns an array of WP_Theme objects based on the arguments.
12 * Despite advances over get_themes(), this function is quite expensive, and grows
13 * linearly with additional themes. Stick to wp_get_theme() if possible.
17 * @param array $args The search arguments. Optional.
18 * - errors mixed True to return themes with errors, false to return themes without errors, null
19 * to return all themes. Defaults to false.
20 * - allowed mixed (Multisite) True to return only allowed themes for a site. False to return only
21 * disallowed themes for a site. 'site' to return only site-allowed themes. 'network'
22 * to return only network-allowed themes. Null to return all themes. Defaults to null.
23 * - blog_id int (Multisite) The blog ID used to calculate which themes are allowed. Defaults to 0,
24 * synonymous for the current blog.
25 * @return Array of WP_Theme objects.
27 function wp_get_themes( $args = array() ) {
28 global $wp_theme_directories;
30 $defaults = array( 'errors' => false, 'allowed' => null, 'blog_id' => 0 );
31 $args = wp_parse_args( $args, $defaults );
33 $theme_directories = search_theme_directories();
35 if ( count( $wp_theme_directories ) > 1 ) {
36 // Make sure the current theme wins out, in case search_theme_directories() picks the wrong
37 // one in the case of a conflict. (Normally, last registered theme root wins.)
38 $current_theme = get_stylesheet();
39 if ( isset( $theme_directories[ $current_theme ] ) ) {
40 $root_of_current_theme = get_raw_theme_root( $current_theme );
41 if ( ! in_array( $root_of_current_theme, $wp_theme_directories ) )
42 $root_of_current_theme = WP_CONTENT_DIR . $root_of_current_theme;
43 $theme_directories[ $current_theme ]['theme_root'] = $root_of_current_theme;
47 if ( empty( $theme_directories ) )
50 if ( is_multisite() && null !== $args['allowed'] ) {
51 $allowed = $args['allowed'];
52 if ( 'network' === $allowed )
53 $theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed_on_network() );
54 elseif ( 'site' === $allowed )
55 $theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed_on_site( $args['blog_id'] ) );
57 $theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed( $args['blog_id'] ) );
59 $theme_directories = array_diff_key( $theme_directories, WP_Theme::get_allowed( $args['blog_id'] ) );
63 static $_themes = array();
65 foreach ( $theme_directories as $theme => $theme_root ) {
66 if ( isset( $_themes[ $theme_root['theme_root'] . '/' . $theme ] ) )
67 $themes[ $theme ] = $_themes[ $theme_root['theme_root'] . '/' . $theme ];
69 $themes[ $theme ] = $_themes[ $theme_root['theme_root'] . '/' . $theme ] = new WP_Theme( $theme, $theme_root['theme_root'] );
72 if ( null !== $args['errors'] ) {
73 foreach ( $themes as $theme => $wp_theme ) {
74 if ( $wp_theme->errors() != $args['errors'] )
75 unset( $themes[ $theme ] );
83 * Gets a WP_Theme object for a theme.
87 * @param string $stylesheet Directory name for the theme. Optional. Defaults to current theme.
88 * @param string $theme_root Absolute path of the theme root to look in. Optional. If not specified, get_raw_theme_root()
89 * is used to calculate the theme root for the $stylesheet provided (or current theme).
90 * @return WP_Theme Theme object. Be sure to check the object's exists() method if you need to confirm the theme's existence.
92 function wp_get_theme( $stylesheet = null, $theme_root = null ) {
93 global $wp_theme_directories;
95 if ( empty( $stylesheet ) )
96 $stylesheet = get_stylesheet();
98 if ( empty( $theme_root ) ) {
99 $theme_root = get_raw_theme_root( $stylesheet );
100 if ( false === $theme_root )
101 $theme_root = WP_CONTENT_DIR . '/themes';
102 elseif ( ! in_array( $theme_root, (array) $wp_theme_directories ) )
103 $theme_root = WP_CONTENT_DIR . $theme_root;
106 return new WP_Theme( $stylesheet, $theme_root );
110 * Clears the cache held by get_theme_roots() and WP_Theme.
113 * @param bool $clear_update_cache Whether to clear the Theme updates cache
115 function wp_clean_themes_cache( $clear_update_cache = true ) {
116 if ( $clear_update_cache )
117 delete_site_transient( 'update_themes' );
118 search_theme_directories( true );
119 foreach ( wp_get_themes( array( 'errors' => null ) ) as $theme )
120 $theme->cache_delete();
124 * Whether a child theme is in use.
128 * @return bool true if a child theme is in use, false otherwise.
130 function is_child_theme() {
131 return ( TEMPLATEPATH !== STYLESHEETPATH );
135 * Retrieve name of the current stylesheet.
137 * The theme name that the administrator has currently set the front end theme
140 * For all extensive purposes, the template name and the stylesheet name are
141 * going to be the same for most cases.
144 * @uses apply_filters() Calls 'stylesheet' filter on stylesheet name.
146 * @return string Stylesheet name.
148 function get_stylesheet() {
149 return apply_filters('stylesheet', get_option('stylesheet'));
153 * Retrieve stylesheet directory path for current theme.
156 * @uses apply_filters() Calls 'stylesheet_directory' filter on stylesheet directory and theme name.
158 * @return string Path to current theme directory.
160 function get_stylesheet_directory() {
161 $stylesheet = get_stylesheet();
162 $theme_root = get_theme_root( $stylesheet );
163 $stylesheet_dir = "$theme_root/$stylesheet";
165 return apply_filters( 'stylesheet_directory', $stylesheet_dir, $stylesheet, $theme_root );
169 * Retrieve stylesheet directory URI.
175 function get_stylesheet_directory_uri() {
176 $stylesheet = get_stylesheet();
177 $theme_root_uri = get_theme_root_uri( $stylesheet );
178 $stylesheet_dir_uri = "$theme_root_uri/$stylesheet";
180 return apply_filters( 'stylesheet_directory_uri', $stylesheet_dir_uri, $stylesheet, $theme_root_uri );
184 * Retrieve URI of current theme stylesheet.
186 * The stylesheet file name is 'style.css' which is appended to {@link
187 * get_stylesheet_directory_uri() stylesheet directory URI} path.
190 * @uses apply_filters() Calls 'stylesheet_uri' filter on stylesheet URI path and stylesheet directory URI.
194 function get_stylesheet_uri() {
195 $stylesheet_dir_uri = get_stylesheet_directory_uri();
196 $stylesheet_uri = $stylesheet_dir_uri . '/style.css';
197 return apply_filters('stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri);
201 * Retrieve localized stylesheet URI.
203 * The stylesheet directory for the localized stylesheet files are located, by
204 * default, in the base theme directory. The name of the locale file will be the
205 * locale followed by '.css'. If that does not exist, then the text direction
206 * stylesheet will be checked for existence, for example 'ltr.css'.
208 * The theme may change the location of the stylesheet directory by either using
209 * the 'stylesheet_directory_uri' filter or the 'locale_stylesheet_uri' filter.
210 * If you want to change the location of the stylesheet files for the entire
211 * WordPress workflow, then change the former. If you just have the locale in a
212 * separate folder, then change the latter.
215 * @uses apply_filters() Calls 'locale_stylesheet_uri' filter on stylesheet URI path and stylesheet directory URI.
219 function get_locale_stylesheet_uri() {
221 $stylesheet_dir_uri = get_stylesheet_directory_uri();
222 $dir = get_stylesheet_directory();
223 $locale = get_locale();
224 if ( file_exists("$dir/$locale.css") )
225 $stylesheet_uri = "$stylesheet_dir_uri/$locale.css";
226 elseif ( !empty($wp_locale->text_direction) && file_exists("$dir/{$wp_locale->text_direction}.css") )
227 $stylesheet_uri = "$stylesheet_dir_uri/{$wp_locale->text_direction}.css";
229 $stylesheet_uri = '';
230 return apply_filters('locale_stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri);
234 * Retrieve name of the current theme.
237 * @uses apply_filters() Calls 'template' filter on template option.
239 * @return string Template name.
241 function get_template() {
242 return apply_filters('template', get_option('template'));
246 * Retrieve current theme directory.
249 * @uses apply_filters() Calls 'template_directory' filter on template directory path and template name.
251 * @return string Template directory path.
253 function get_template_directory() {
254 $template = get_template();
255 $theme_root = get_theme_root( $template );
256 $template_dir = "$theme_root/$template";
258 return apply_filters( 'template_directory', $template_dir, $template, $theme_root );
262 * Retrieve theme directory URI.
265 * @uses apply_filters() Calls 'template_directory_uri' filter on template directory URI path and template name.
267 * @return string Template directory URI.
269 function get_template_directory_uri() {
270 $template = get_template();
271 $theme_root_uri = get_theme_root_uri( $template );
272 $template_dir_uri = "$theme_root_uri/$template";
274 return apply_filters( 'template_directory_uri', $template_dir_uri, $template, $theme_root_uri );
278 * Retrieve theme roots.
282 * @return array|string An array of theme roots keyed by template/stylesheet or a single theme root if all themes have the same root.
284 function get_theme_roots() {
285 global $wp_theme_directories;
287 if ( count($wp_theme_directories) <= 1 )
290 $theme_roots = get_site_transient( 'theme_roots' );
291 if ( false === $theme_roots ) {
292 search_theme_directories( true ); // Regenerate the transient.
293 $theme_roots = get_site_transient( 'theme_roots' );
299 * Register a directory that contains themes.
303 * @param string $directory Either the full filesystem path to a theme folder or a folder within WP_CONTENT_DIR
306 function register_theme_directory( $directory ) {
307 global $wp_theme_directories;
309 if ( ! file_exists( $directory ) ) {
310 // Try prepending as the theme directory could be relative to the content directory
311 $directory = WP_CONTENT_DIR . '/' . $directory;
312 // If this directory does not exist, return and do not register
313 if ( ! file_exists( $directory ) )
317 $wp_theme_directories[] = $directory;
323 * Search all registered theme directories for complete and valid themes.
327 * @param bool $force Optional. Whether to force a new directory scan. Defaults to false.
328 * @return array Valid themes found
330 function search_theme_directories( $force = false ) {
331 global $wp_theme_directories;
332 if ( empty( $wp_theme_directories ) )
335 static $found_themes;
336 if ( ! $force && isset( $found_themes ) )
337 return $found_themes;
339 $found_themes = array();
341 $wp_theme_directories = (array) $wp_theme_directories;
343 // Set up maybe-relative, maybe-absolute array of theme directories.
344 // We always want to return absolute, but we need to cache relative
345 // to use in get_theme_root().
346 foreach ( $wp_theme_directories as $theme_root ) {
347 if ( 0 === strpos( $theme_root, WP_CONTENT_DIR ) )
348 $relative_theme_roots[ str_replace( WP_CONTENT_DIR, '', $theme_root ) ] = $theme_root;
350 $relative_theme_roots[ $theme_root ] = $theme_root;
353 if ( $cache_expiration = apply_filters( 'wp_cache_themes_persistently', false, 'search_theme_directories' ) ) {
354 $cached_roots = get_site_transient( 'theme_roots' );
355 if ( is_array( $cached_roots ) ) {
356 foreach ( $cached_roots as $theme_dir => $theme_root ) {
357 // A cached theme root is no longer around, so skip it.
358 if ( ! isset( $relative_theme_roots[ $theme_root ] ) )
360 $found_themes[ $theme_dir ] = array(
361 'theme_file' => $theme_dir . '/style.css',
362 'theme_root' => $relative_theme_roots[ $theme_root ], // Convert relative to absolute.
365 return $found_themes;
367 if ( ! is_int( $cache_expiration ) )
368 $cache_expiration = 1800; // half hour
370 $cache_expiration = 1800; // half hour
373 /* Loop the registered theme directories and extract all themes */
374 foreach ( $wp_theme_directories as $theme_root ) {
376 // Start with directories in the root of the current theme directory.
377 $dirs = @ scandir( $theme_root );
379 trigger_error( "$theme_root is not readable", E_USER_NOTICE );
382 foreach ( $dirs as $dir ) {
383 if ( ! is_dir( $theme_root . '/' . $dir ) || $dir[0] == '.' || $dir == 'CVS' )
385 if ( file_exists( $theme_root . '/' . $dir . '/style.css' ) ) {
386 // wp-content/themes/a-single-theme
387 // wp-content/themes is $theme_root, a-single-theme is $dir
388 $found_themes[ $dir ] = array(
389 'theme_file' => $dir . '/style.css',
390 'theme_root' => $theme_root,
393 $found_theme = false;
394 // wp-content/themes/a-folder-of-themes/*
395 // wp-content/themes is $theme_root, a-folder-of-themes is $dir, then themes are $sub_dirs
396 $sub_dirs = @ scandir( $theme_root . '/' . $dir );
398 trigger_error( "$theme_root/$dir is not readable", E_USER_NOTICE );
401 foreach ( $sub_dirs as $sub_dir ) {
402 if ( ! is_dir( $theme_root . '/' . $dir . '/' . $sub_dir ) || $dir[0] == '.' || $dir == 'CVS' )
404 if ( ! file_exists( $theme_root . '/' . $dir . '/' . $sub_dir . '/style.css' ) )
406 $found_themes[ $dir . '/' . $sub_dir ] = array(
407 'theme_file' => $dir . '/' . $sub_dir . '/style.css',
408 'theme_root' => $theme_root,
412 // Never mind the above, it's just a theme missing a style.css.
413 // Return it; WP_Theme will catch the error.
414 if ( ! $found_theme )
415 $found_themes[ $dir ] = array(
416 'theme_file' => $dir . '/style.css',
417 'theme_root' => $theme_root,
423 asort( $found_themes );
425 $theme_roots = array();
426 $relative_theme_roots = array_flip( $relative_theme_roots );
428 foreach ( $found_themes as $theme_dir => $theme_data ) {
429 $theme_roots[ $theme_dir ] = $relative_theme_roots[ $theme_data['theme_root'] ]; // Convert absolute to relative.
432 if ( $theme_roots != get_site_transient( 'theme_roots' ) )
433 set_site_transient( 'theme_roots', $theme_roots, $cache_expiration );
435 return $found_themes;
439 * Retrieve path to themes directory.
441 * Does not have trailing slash.
444 * @uses apply_filters() Calls 'theme_root' filter on path.
446 * @param string $stylesheet_or_template The stylesheet or template name of the theme
447 * @return string Theme path.
449 function get_theme_root( $stylesheet_or_template = false ) {
450 global $wp_theme_directories;
452 if ( $stylesheet_or_template && $theme_root = get_raw_theme_root( $stylesheet_or_template ) ) {
453 // Always prepend WP_CONTENT_DIR unless the root currently registered as a theme directory.
454 // This gives relative theme roots the benefit of the doubt when things go haywire.
455 if ( ! in_array( $theme_root, (array) $wp_theme_directories ) )
456 $theme_root = WP_CONTENT_DIR . $theme_root;
458 $theme_root = WP_CONTENT_DIR . '/themes';
461 return apply_filters( 'theme_root', $theme_root );
465 * Retrieve URI for themes directory.
467 * Does not have trailing slash.
471 * @param string $stylesheet_or_template Optional. The stylesheet or template name of the theme.
472 * Default is to leverage the main theme root.
473 * @param string $theme_root Optional. The theme root for which calculations will be based, preventing
474 * the need for a get_raw_theme_root() call.
475 * @return string Themes URI.
477 function get_theme_root_uri( $stylesheet_or_template = false, $theme_root = false ) {
478 global $wp_theme_directories;
480 if ( $stylesheet_or_template && ! $theme_root )
481 $theme_root = get_raw_theme_root( $stylesheet_or_template );
483 if ( $stylesheet_or_template && $theme_root ) {
484 if ( in_array( $theme_root, (array) $wp_theme_directories ) ) {
485 // Absolute path. Make an educated guess. YMMV -- but note the filter below.
486 if ( 0 === strpos( $theme_root, WP_CONTENT_DIR ) )
487 $theme_root_uri = content_url( str_replace( WP_CONTENT_DIR, '', $theme_root ) );
488 elseif ( 0 === strpos( $theme_root, ABSPATH ) )
489 $theme_root_uri = site_url( str_replace( ABSPATH, '', $theme_root ) );
490 elseif ( 0 === strpos( $theme_root, WP_PLUGIN_DIR ) || 0 === strpos( $theme_root, WPMU_PLUGIN_DIR ) )
491 $theme_root_uri = plugins_url( basename( $theme_root ), $theme_root );
493 $theme_root_uri = $theme_root;
495 $theme_root_uri = content_url( $theme_root );
498 $theme_root_uri = content_url( 'themes' );
501 return apply_filters( 'theme_root_uri', $theme_root_uri, get_option('siteurl'), $stylesheet_or_template );
505 * Get the raw theme root relative to the content directory with no filters applied.
509 * @param string $stylesheet_or_template The stylesheet or template name of the theme
510 * @param bool $skip_cache Optional. Whether to skip the cache. Defaults to false, meaning the cache is used.
511 * @return string Theme root
513 function get_raw_theme_root( $stylesheet_or_template, $skip_cache = false ) {
514 global $wp_theme_directories;
516 if ( count($wp_theme_directories) <= 1 )
521 // If requesting the root for the current theme, consult options to avoid calling get_theme_roots()
522 if ( ! $skip_cache ) {
523 if ( get_option('stylesheet') == $stylesheet_or_template )
524 $theme_root = get_option('stylesheet_root');
525 elseif ( get_option('template') == $stylesheet_or_template )
526 $theme_root = get_option('template_root');
529 if ( empty($theme_root) ) {
530 $theme_roots = get_theme_roots();
531 if ( !empty($theme_roots[$stylesheet_or_template]) )
532 $theme_root = $theme_roots[$stylesheet_or_template];
539 * Display localized stylesheet link element.
543 function locale_stylesheet() {
544 $stylesheet = get_locale_stylesheet_uri();
545 if ( empty($stylesheet) )
547 echo '<link rel="stylesheet" href="' . $stylesheet . '" type="text/css" media="screen" />';
551 * Start preview theme output buffer.
553 * Will only perform task if the user has permissions and template and preview
554 * query variables exist.
558 function preview_theme() {
559 if ( ! (isset($_GET['template']) && isset($_GET['preview'])) )
562 if ( !current_user_can( 'switch_themes' ) )
565 // Admin Thickbox requests
566 if ( isset( $_GET['preview_iframe'] ) )
567 show_admin_bar( false );
569 $_GET['template'] = preg_replace('|[^a-z0-9_./-]|i', '', $_GET['template']);
571 if ( validate_file($_GET['template']) )
574 add_filter( 'template', '_preview_theme_template_filter' );
576 if ( isset($_GET['stylesheet']) ) {
577 $_GET['stylesheet'] = preg_replace('|[^a-z0-9_./-]|i', '', $_GET['stylesheet']);
578 if ( validate_file($_GET['stylesheet']) )
580 add_filter( 'stylesheet', '_preview_theme_stylesheet_filter' );
583 // Prevent theme mods to current theme being used on theme being previewed
584 add_filter( 'pre_option_theme_mods_' . get_option( 'stylesheet' ), '__return_empty_array' );
586 ob_start( 'preview_theme_ob_filter' );
588 add_action('setup_theme', 'preview_theme');
591 * Private function to modify the current template when previewing a theme
598 function _preview_theme_template_filter() {
599 return isset($_GET['template']) ? $_GET['template'] : '';
603 * Private function to modify the current stylesheet when previewing a theme
610 function _preview_theme_stylesheet_filter() {
611 return isset($_GET['stylesheet']) ? $_GET['stylesheet'] : '';
615 * Callback function for ob_start() to capture all links in the theme.
620 * @param string $content
623 function preview_theme_ob_filter( $content ) {
624 return preg_replace_callback( "|(<a.*?href=([\"']))(.*?)([\"'].*?>)|", 'preview_theme_ob_filter_callback', $content );
628 * Manipulates preview theme links in order to control and maintain location.
630 * Callback function for preg_replace_callback() to accept and filter matches.
635 * @param array $matches
638 function preview_theme_ob_filter_callback( $matches ) {
639 if ( strpos($matches[4], 'onclick') !== false )
640 $matches[4] = preg_replace('#onclick=([\'"]).*?(?<!\\\)\\1#i', '', $matches[4]); //Strip out any onclicks from rest of <a>. (?<!\\\) means to ignore the '" if it's escaped by \ to prevent breaking mid-attribute.
642 ( false !== strpos($matches[3], '/wp-admin/') )
644 ( false !== strpos( $matches[3], '://' ) && 0 !== strpos( $matches[3], home_url() ) )
646 ( false !== strpos($matches[3], '/feed/') )
648 ( false !== strpos($matches[3], '/trackback/') )
650 return $matches[1] . "#$matches[2] onclick=$matches[2]return false;" . $matches[4];
652 $stylesheet = isset( $_GET['stylesheet'] ) ? $_GET['stylesheet'] : '';
653 $template = isset( $_GET['template'] ) ? $_GET['template'] : '';
655 $link = add_query_arg( array( 'preview' => 1, 'template' => $template, 'stylesheet' => $stylesheet, 'preview_iframe' => 1 ), $matches[3] );
656 if ( 0 === strpos($link, 'preview=1') )
658 return $matches[1] . esc_attr( $link ) . $matches[4];
662 * Switches the theme.
664 * Accepts one argument: $stylesheet of the theme. It also accepts an additional function signature
665 * of two arguments: $template then $stylesheet. This is for backwards compatibility.
668 * @uses do_action() Calls 'switch_theme' action, passing the new theme.
670 * @param string $stylesheet Stylesheet name
672 function switch_theme( $stylesheet ) {
673 global $wp_theme_directories, $sidebars_widgets;
675 if ( is_array( $sidebars_widgets ) )
676 set_theme_mod( 'sidebars_widgets', array( 'time' => time(), 'data' => $sidebars_widgets ) );
678 $old_theme = wp_get_theme();
679 $new_theme = wp_get_theme( $stylesheet );
681 if ( func_num_args() > 1 ) {
682 $template = $stylesheet;
683 $stylesheet = func_get_arg( 1 );
685 $template = $new_theme->get_template();
688 update_option( 'template', $template );
689 update_option( 'stylesheet', $stylesheet );
691 if ( count( $wp_theme_directories ) > 1 ) {
692 update_option( 'template_root', get_raw_theme_root( $template, true ) );
693 update_option( 'stylesheet_root', get_raw_theme_root( $stylesheet, true ) );
695 delete_option( 'template_root' );
696 delete_option( 'stylesheet_root' );
699 $new_name = $new_theme->get('Name');
701 update_option( 'current_theme', $new_name );
703 if ( is_admin() && false === get_option( 'theme_mods_' . $stylesheet ) ) {
704 $default_theme_mods = (array) get_option( 'mods_' . $new_name );
705 add_option( "theme_mods_$stylesheet", $default_theme_mods );
708 update_option( 'theme_switched', $old_theme->get_stylesheet() );
709 do_action( 'switch_theme', $new_name, $new_theme );
713 * Checks that current theme files 'index.php' and 'style.css' exists.
715 * Does not check the default theme, which is the fallback and should always exist.
716 * Will switch theme to the fallback theme if current theme does not validate.
717 * You can use the 'validate_current_theme' filter to return false to
718 * disable this functionality.
721 * @see WP_DEFAULT_THEME
725 function validate_current_theme() {
726 // Don't validate during an install/upgrade.
727 if ( defined('WP_INSTALLING') || !apply_filters( 'validate_current_theme', true ) )
730 if ( get_template() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/index.php') ) {
731 switch_theme( WP_DEFAULT_THEME );
735 if ( get_stylesheet() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/style.css') ) {
736 switch_theme( WP_DEFAULT_THEME );
740 if ( is_child_theme() && ! file_exists( get_stylesheet_directory() . '/style.css' ) ) {
741 switch_theme( WP_DEFAULT_THEME );
749 * Retrieve all theme modifications.
753 * @return array Theme modifications.
755 function get_theme_mods() {
756 $theme_slug = get_option( 'stylesheet' );
757 if ( false === ( $mods = get_option( "theme_mods_$theme_slug" ) ) ) {
758 $theme_name = get_option( 'current_theme' );
759 if ( false === $theme_name )
760 $theme_name = wp_get_theme()->get('Name');
761 $mods = get_option( "mods_$theme_name" ); // Deprecated location.
762 if ( is_admin() && false !== $mods ) {
763 update_option( "theme_mods_$theme_slug", $mods );
764 delete_option( "mods_$theme_name" );
771 * Retrieve theme modification value for the current theme.
773 * If the modification name does not exist, then the $default will be passed
774 * through {@link http://php.net/sprintf sprintf()} PHP function with the first
775 * string the template directory URI and the second string the stylesheet
779 * @uses apply_filters() Calls 'theme_mod_$name' filter on the value.
781 * @param string $name Theme modification name.
782 * @param bool|string $default
785 function get_theme_mod( $name, $default = false ) {
786 $mods = get_theme_mods();
788 if ( isset( $mods[ $name ] ) )
789 return apply_filters( "theme_mod_$name", $mods[ $name ] );
791 if ( is_string( $default ) )
792 $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
794 return apply_filters( "theme_mod_$name", $default );
798 * Update theme modification value for the current theme.
802 * @param string $name Theme modification name.
803 * @param string $value theme modification value.
805 function set_theme_mod( $name, $value ) {
806 $mods = get_theme_mods();
808 $mods[ $name ] = $value;
810 $theme = get_option( 'stylesheet' );
811 update_option( "theme_mods_$theme", $mods );
815 * Remove theme modification name from current theme list.
817 * If removing the name also removes all elements, then the entire option will
822 * @param string $name Theme modification name.
825 function remove_theme_mod( $name ) {
826 $mods = get_theme_mods();
828 if ( ! isset( $mods[ $name ] ) )
831 unset( $mods[ $name ] );
833 if ( empty( $mods ) )
834 return remove_theme_mods();
836 $theme = get_option( 'stylesheet' );
837 update_option( "theme_mods_$theme", $mods );
841 * Remove theme modifications option for current theme.
845 function remove_theme_mods() {
846 delete_option( 'theme_mods_' . get_option( 'stylesheet' ) );
849 $theme_name = get_option( 'current_theme' );
850 if ( false === $theme_name )
851 $theme_name = wp_get_theme()->get('Name');
852 delete_option( 'mods_' . $theme_name );
856 * Retrieve text color for custom header.
862 function get_header_textcolor() {
863 return get_theme_mod('header_textcolor', get_theme_support( 'custom-header', 'default-text-color' ) );
867 * Display text color for custom header.
871 function header_textcolor() {
872 echo get_header_textcolor();
876 * Whether to display the header text.
882 function display_header_text() {
883 if ( ! current_theme_supports( 'custom-header', 'header-text' ) )
886 $text_color = get_theme_mod( 'header_textcolor', get_theme_support( 'custom-header', 'default-text-color' ) );
887 return 'blank' != $text_color;
891 * Retrieve header image for custom header.
897 function get_header_image() {
898 $url = get_theme_mod( 'header_image', get_theme_support( 'custom-header', 'default-image' ) );
900 if ( 'remove-header' == $url )
903 if ( is_random_header_image() )
904 $url = get_random_header_image();
906 return esc_url_raw( set_url_scheme( $url ) );
910 * Get random header image data from registered images in theme.
916 * @return string Path to header image
919 function _get_random_header_data() {
920 static $_wp_random_header;
922 if ( empty( $_wp_random_header ) ) {
923 global $_wp_default_headers;
924 $header_image_mod = get_theme_mod( 'header_image', '' );
927 if ( 'random-uploaded-image' == $header_image_mod )
928 $headers = get_uploaded_header_images();
929 elseif ( ! empty( $_wp_default_headers ) ) {
930 if ( 'random-default-image' == $header_image_mod ) {
931 $headers = $_wp_default_headers;
933 if ( current_theme_supports( 'custom-header', 'random-default' ) )
934 $headers = $_wp_default_headers;
938 if ( empty( $headers ) )
941 $_wp_random_header = (object) $headers[ array_rand( $headers ) ];
943 $_wp_random_header->url = sprintf( $_wp_random_header->url, get_template_directory_uri(), get_stylesheet_directory_uri() );
944 $_wp_random_header->thumbnail_url = sprintf( $_wp_random_header->thumbnail_url, get_template_directory_uri(), get_stylesheet_directory_uri() );
946 return $_wp_random_header;
950 * Get random header image url from registered images in theme.
954 * @return string Path to header image
957 function get_random_header_image() {
958 $random_image = _get_random_header_data();
959 if ( empty( $random_image->url ) )
961 return $random_image->url;
965 * Check if random header image is in use.
967 * Always true if user expressly chooses the option in Appearance > Header.
968 * Also true if theme has multiple header images registered, no specific header image
969 * is chosen, and theme turns on random headers with add_theme_support().
973 * @param string $type The random pool to use. any|default|uploaded
976 function is_random_header_image( $type = 'any' ) {
977 $header_image_mod = get_theme_mod( 'header_image', get_theme_support( 'custom-header', 'default-image' ) );
979 if ( 'any' == $type ) {
980 if ( 'random-default-image' == $header_image_mod || 'random-uploaded-image' == $header_image_mod || ( '' != get_random_header_image() && empty( $header_image_mod ) ) )
983 if ( "random-$type-image" == $header_image_mod )
985 elseif ( 'default' == $type && empty( $header_image_mod ) && '' != get_random_header_image() )
993 * Display header image URL.
997 function header_image() {
998 echo esc_url( get_header_image() );
1002 * Get the header images uploaded for the current theme.
1008 function get_uploaded_header_images() {
1009 $header_images = array();
1012 $headers = get_posts( array( 'post_type' => 'attachment', 'meta_key' => '_wp_attachment_is_custom_header', 'meta_value' => get_option('stylesheet'), 'orderby' => 'none', 'nopaging' => true ) );
1014 if ( empty( $headers ) )
1017 foreach ( (array) $headers as $header ) {
1018 $url = esc_url_raw( $header->guid );
1019 $header_data = wp_get_attachment_metadata( $header->ID );
1020 $header_index = basename($url);
1021 $header_images[$header_index] = array();
1022 $header_images[$header_index]['attachment_id'] = $header->ID;
1023 $header_images[$header_index]['url'] = $url;
1024 $header_images[$header_index]['thumbnail_url'] = $url;
1025 if ( isset( $header_data['width'] ) )
1026 $header_images[$header_index]['width'] = $header_data['width'];
1027 if ( isset( $header_data['height'] ) )
1028 $header_images[$header_index]['height'] = $header_data['height'];
1031 return $header_images;
1035 * Get the header image data.
1041 function get_custom_header() {
1042 global $_wp_default_headers;
1044 if ( is_random_header_image() ) {
1045 $data = _get_random_header_data();
1047 $data = get_theme_mod( 'header_image_data' );
1048 if ( ! $data && current_theme_supports( 'custom-header', 'default-image' ) ) {
1049 $directory_args = array( get_template_directory_uri(), get_stylesheet_directory_uri() );
1051 $data['url'] = $data['thumbnail_url'] = vsprintf( get_theme_support( 'custom-header', 'default-image' ), $directory_args );
1052 if ( ! empty( $_wp_default_headers ) ) {
1053 foreach ( (array) $_wp_default_headers as $default_header ) {
1054 $url = vsprintf( $default_header['url'], $directory_args );
1055 if ( $data['url'] == $url ) {
1056 $data = $default_header;
1057 $data['url'] = $url;
1058 $data['thumbnail_url'] = vsprintf( $data['thumbnail_url'], $directory_args );
1068 'thumbnail_url' => '',
1069 'width' => get_theme_support( 'custom-header', 'width' ),
1070 'height' => get_theme_support( 'custom-header', 'height' ),
1072 return (object) wp_parse_args( $data, $default );
1076 * Register a selection of default headers to be displayed by the custom header admin UI.
1080 * @param array $headers Array of headers keyed by a string id. The ids point to arrays containing 'url', 'thumbnail_url', and 'description' keys.
1082 function register_default_headers( $headers ) {
1083 global $_wp_default_headers;
1085 $_wp_default_headers = array_merge( (array) $_wp_default_headers, (array) $headers );
1089 * Unregister default headers.
1091 * This function must be called after register_default_headers() has already added the
1092 * header you want to remove.
1094 * @see register_default_headers()
1097 * @param string|array $header The header string id (key of array) to remove, or an array thereof.
1098 * @return True on success, false on failure.
1100 function unregister_default_headers( $header ) {
1101 global $_wp_default_headers;
1102 if ( is_array( $header ) ) {
1103 array_map( 'unregister_default_headers', $header );
1104 } elseif ( isset( $_wp_default_headers[ $header ] ) ) {
1105 unset( $_wp_default_headers[ $header ] );
1113 * Retrieve background image for custom background.
1119 function get_background_image() {
1120 return get_theme_mod('background_image', get_theme_support( 'custom-background', 'default-image' ) );
1124 * Display background image path.
1128 function background_image() {
1129 echo get_background_image();
1133 * Retrieve value for custom background color.
1139 function get_background_color() {
1140 return get_theme_mod('background_color', get_theme_support( 'custom-background', 'default-color' ) );
1144 * Display background color value.
1148 function background_color() {
1149 echo get_background_color();
1153 * Default custom background callback.
1158 function _custom_background_cb() {
1159 // $background is the saved custom image, or the default image.
1160 $background = set_url_scheme( get_background_image() );
1162 // $color is the saved custom color.
1163 // A default has to be specified in style.css. It will not be printed here.
1164 $color = get_theme_mod( 'background_color' );
1166 if ( ! $background && ! $color )
1169 $style = $color ? "background-color: #$color;" : '';
1171 if ( $background ) {
1172 $image = " background-image: url('$background');";
1174 $repeat = get_theme_mod( 'background_repeat', 'repeat' );
1175 if ( ! in_array( $repeat, array( 'no-repeat', 'repeat-x', 'repeat-y', 'repeat' ) ) )
1177 $repeat = " background-repeat: $repeat;";
1179 $position = get_theme_mod( 'background_position_x', 'left' );
1180 if ( ! in_array( $position, array( 'center', 'right', 'left' ) ) )
1182 $position = " background-position: top $position;";
1184 $attachment = get_theme_mod( 'background_attachment', 'scroll' );
1185 if ( ! in_array( $attachment, array( 'fixed', 'scroll' ) ) )
1186 $attachment = 'scroll';
1187 $attachment = " background-attachment: $attachment;";
1189 $style .= $image . $repeat . $position . $attachment;
1192 <style type="text/css" id="custom-background-css">
1193 body.custom-background { <?php echo trim( $style ); ?> }
1199 * Add callback for custom TinyMCE editor stylesheets.
1201 * The parameter $stylesheet is the name of the stylesheet, relative to
1202 * the theme root. It also accepts an array of stylesheets.
1203 * It is optional and defaults to 'editor-style.css'.
1205 * This function automatically adds another stylesheet with -rtl prefix, e.g. editor-style-rtl.css.
1206 * If that file doesn't exist, it is removed before adding the stylesheet(s) to TinyMCE.
1207 * If an array of stylesheets is passed to add_editor_style(),
1208 * RTL is only added for the first stylesheet.
1210 * Since version 3.4 the TinyMCE body has .rtl CSS class.
1211 * It is a better option to use that class and add any RTL styles to the main stylesheet.
1215 * @param mixed $stylesheet Optional. Stylesheet name or array thereof, relative to theme root.
1216 * Defaults to 'editor-style.css'
1218 function add_editor_style( $stylesheet = 'editor-style.css' ) {
1220 add_theme_support( 'editor-style' );
1225 global $editor_styles;
1226 $editor_styles = (array) $editor_styles;
1227 $stylesheet = (array) $stylesheet;
1229 $rtl_stylesheet = str_replace('.css', '-rtl.css', $stylesheet[0]);
1230 $stylesheet[] = $rtl_stylesheet;
1233 $editor_styles = array_merge( $editor_styles, $stylesheet );
1237 * Removes all visual editor stylesheets.
1241 * @return bool True on success, false if there were no stylesheets to remove.
1243 function remove_editor_styles() {
1244 if ( ! current_theme_supports( 'editor-style' ) )
1246 _remove_theme_support( 'editor-style' );
1248 $GLOBALS['editor_styles'] = array();
1253 * Allows a theme to register its support of a certain feature
1255 * Must be called in the theme's functions.php file to work.
1256 * If attached to a hook, it must be after_setup_theme.
1257 * The init hook may be too late for some features.
1260 * @param string $feature the feature being added
1262 function add_theme_support( $feature ) {
1263 global $_wp_theme_features;
1265 if ( func_num_args() == 1 )
1268 $args = array_slice( func_get_args(), 1 );
1270 switch ( $feature ) {
1271 case 'post-formats' :
1272 if ( is_array( $args[0] ) )
1273 $args[0] = array_intersect( $args[0], array_keys( get_post_format_slugs() ) );
1277 // You can't just pass 'html5', you need to pass an array of types.
1278 if ( empty( $args[0] ) ) {
1279 $args = array( 0 => array( 'comment-list', 'comment-form', 'search-form' ) );
1280 } elseif ( ! is_array( $args[0] ) ) {
1281 _doing_it_wrong( "add_theme_support( 'html5' )", 'You need to pass an array of types.', '3.6.1' );
1285 // Calling 'html5' again merges, rather than overwrites.
1286 if ( isset( $_wp_theme_features['html5'] ) )
1287 $args[0] = array_merge( $_wp_theme_features['html5'][0], $args[0] );
1290 case 'custom-header-uploads' :
1291 return add_theme_support( 'custom-header', array( 'uploads' => true ) );
1294 case 'custom-header' :
1295 if ( ! is_array( $args ) )
1296 $args = array( 0 => array() );
1299 'default-image' => '',
1300 'random-default' => false,
1303 'flex-height' => false,
1304 'flex-width' => false,
1305 'default-text-color' => '',
1306 'header-text' => true,
1308 'wp-head-callback' => '',
1309 'admin-head-callback' => '',
1310 'admin-preview-callback' => '',
1313 $jit = isset( $args[0]['__jit'] );
1314 unset( $args[0]['__jit'] );
1316 // Merge in data from previous add_theme_support() calls.
1317 // The first value registered wins. (A child theme is set up first.)
1318 if ( isset( $_wp_theme_features['custom-header'] ) )
1319 $args[0] = wp_parse_args( $_wp_theme_features['custom-header'][0], $args[0] );
1321 // Load in the defaults at the end, as we need to insure first one wins.
1322 // This will cause all constants to be defined, as each arg will then be set to the default.
1324 $args[0] = wp_parse_args( $args[0], $defaults );
1326 // If a constant was defined, use that value. Otherwise, define the constant to ensure
1327 // the constant is always accurate (and is not defined later, overriding our value).
1328 // As stated above, the first value wins.
1329 // Once we get to wp_loaded (just-in-time), define any constants we haven't already.
1330 // Constants are lame. Don't reference them. This is just for backwards compatibility.
1332 if ( defined( 'NO_HEADER_TEXT' ) )
1333 $args[0]['header-text'] = ! NO_HEADER_TEXT;
1334 elseif ( isset( $args[0]['header-text'] ) )
1335 define( 'NO_HEADER_TEXT', empty( $args[0]['header-text'] ) );
1337 if ( defined( 'HEADER_IMAGE_WIDTH' ) )
1338 $args[0]['width'] = (int) HEADER_IMAGE_WIDTH;
1339 elseif ( isset( $args[0]['width'] ) )
1340 define( 'HEADER_IMAGE_WIDTH', (int) $args[0]['width'] );
1342 if ( defined( 'HEADER_IMAGE_HEIGHT' ) )
1343 $args[0]['height'] = (int) HEADER_IMAGE_HEIGHT;
1344 elseif ( isset( $args[0]['height'] ) )
1345 define( 'HEADER_IMAGE_HEIGHT', (int) $args[0]['height'] );
1347 if ( defined( 'HEADER_TEXTCOLOR' ) )
1348 $args[0]['default-text-color'] = HEADER_TEXTCOLOR;
1349 elseif ( isset( $args[0]['default-text-color'] ) )
1350 define( 'HEADER_TEXTCOLOR', $args[0]['default-text-color'] );
1352 if ( defined( 'HEADER_IMAGE' ) )
1353 $args[0]['default-image'] = HEADER_IMAGE;
1354 elseif ( isset( $args[0]['default-image'] ) )
1355 define( 'HEADER_IMAGE', $args[0]['default-image'] );
1357 if ( $jit && ! empty( $args[0]['default-image'] ) )
1358 $args[0]['random-default'] = false;
1360 // If headers are supported, and we still don't have a defined width or height,
1361 // we have implicit flex sizes.
1363 if ( empty( $args[0]['width'] ) && empty( $args[0]['flex-width'] ) )
1364 $args[0]['flex-width'] = true;
1365 if ( empty( $args[0]['height'] ) && empty( $args[0]['flex-height'] ) )
1366 $args[0]['flex-height'] = true;
1371 case 'custom-background' :
1372 if ( ! is_array( $args ) )
1373 $args = array( 0 => array() );
1376 'default-image' => '',
1377 'default-color' => '',
1378 'wp-head-callback' => '_custom_background_cb',
1379 'admin-head-callback' => '',
1380 'admin-preview-callback' => '',
1383 $jit = isset( $args[0]['__jit'] );
1384 unset( $args[0]['__jit'] );
1386 // Merge in data from previous add_theme_support() calls. The first value registered wins.
1387 if ( isset( $_wp_theme_features['custom-background'] ) )
1388 $args[0] = wp_parse_args( $_wp_theme_features['custom-background'][0], $args[0] );
1391 $args[0] = wp_parse_args( $args[0], $defaults );
1393 if ( defined( 'BACKGROUND_COLOR' ) )
1394 $args[0]['default-color'] = BACKGROUND_COLOR;
1395 elseif ( isset( $args[0]['default-color'] ) || $jit )
1396 define( 'BACKGROUND_COLOR', $args[0]['default-color'] );
1398 if ( defined( 'BACKGROUND_IMAGE' ) )
1399 $args[0]['default-image'] = BACKGROUND_IMAGE;
1400 elseif ( isset( $args[0]['default-image'] ) || $jit )
1401 define( 'BACKGROUND_IMAGE', $args[0]['default-image'] );
1406 $_wp_theme_features[ $feature ] = $args;
1410 * Registers the internal custom header and background routines.
1415 function _custom_header_background_just_in_time() {
1416 global $custom_image_header, $custom_background;
1418 if ( current_theme_supports( 'custom-header' ) ) {
1419 // In case any constants were defined after an add_custom_image_header() call, re-run.
1420 add_theme_support( 'custom-header', array( '__jit' => true ) );
1422 $args = get_theme_support( 'custom-header' );
1423 if ( $args[0]['wp-head-callback'] )
1424 add_action( 'wp_head', $args[0]['wp-head-callback'] );
1427 require_once( ABSPATH . 'wp-admin/custom-header.php' );
1428 $custom_image_header = new Custom_Image_Header( $args[0]['admin-head-callback'], $args[0]['admin-preview-callback'] );
1432 if ( current_theme_supports( 'custom-background' ) ) {
1433 // In case any constants were defined after an add_custom_background() call, re-run.
1434 add_theme_support( 'custom-background', array( '__jit' => true ) );
1436 $args = get_theme_support( 'custom-background' );
1437 add_action( 'wp_head', $args[0]['wp-head-callback'] );
1440 require_once( ABSPATH . 'wp-admin/custom-background.php' );
1441 $custom_background = new Custom_Background( $args[0]['admin-head-callback'], $args[0]['admin-preview-callback'] );
1445 add_action( 'wp_loaded', '_custom_header_background_just_in_time' );
1448 * Gets the theme support arguments passed when registering that support
1451 * @param string $feature the feature to check
1452 * @return array The array of extra arguments
1454 function get_theme_support( $feature ) {
1455 global $_wp_theme_features;
1456 if ( ! isset( $_wp_theme_features[ $feature ] ) )
1459 if ( func_num_args() <= 1 )
1460 return $_wp_theme_features[ $feature ];
1462 $args = array_slice( func_get_args(), 1 );
1463 switch ( $feature ) {
1464 case 'custom-header' :
1465 case 'custom-background' :
1466 if ( isset( $_wp_theme_features[ $feature ][0][ $args[0] ] ) )
1467 return $_wp_theme_features[ $feature ][0][ $args[0] ];
1471 return $_wp_theme_features[ $feature ];
1477 * Allows a theme to de-register its support of a certain feature
1479 * Should be called in the theme's functions.php file. Generally would
1480 * be used for child themes to override support from the parent theme.
1483 * @see add_theme_support()
1484 * @param string $feature the feature being added
1485 * @return bool Whether feature was removed.
1487 function remove_theme_support( $feature ) {
1488 // Blacklist: for internal registrations not used directly by themes.
1489 if ( in_array( $feature, array( 'editor-style', 'widgets', 'menus' ) ) )
1492 return _remove_theme_support( $feature );
1496 * Do not use. Removes theme support internally, ignorant of the blacklist.
1501 function _remove_theme_support( $feature ) {
1502 global $_wp_theme_features;
1504 switch ( $feature ) {
1505 case 'custom-header-uploads' :
1506 if ( ! isset( $_wp_theme_features['custom-header'] ) )
1508 add_theme_support( 'custom-header', array( 'uploads' => false ) );
1509 return; // Do not continue - custom-header-uploads no longer exists.
1512 if ( ! isset( $_wp_theme_features[ $feature ] ) )
1515 switch ( $feature ) {
1516 case 'custom-header' :
1517 if ( ! did_action( 'wp_loaded' ) )
1519 $support = get_theme_support( 'custom-header' );
1520 if ( $support[0]['wp-head-callback'] )
1521 remove_action( 'wp_head', $support[0]['wp-head-callback'] );
1522 remove_action( 'admin_menu', array( $GLOBALS['custom_image_header'], 'init' ) );
1523 unset( $GLOBALS['custom_image_header'] );
1526 case 'custom-background' :
1527 if ( ! did_action( 'wp_loaded' ) )
1529 $support = get_theme_support( 'custom-background' );
1530 remove_action( 'wp_head', $support[0]['wp-head-callback'] );
1531 remove_action( 'admin_menu', array( $GLOBALS['custom_background'], 'init' ) );
1532 unset( $GLOBALS['custom_background'] );
1536 unset( $_wp_theme_features[ $feature ] );
1541 * Checks a theme's support for a given feature
1544 * @param string $feature the feature being checked
1547 function current_theme_supports( $feature ) {
1548 global $_wp_theme_features;
1550 if ( 'custom-header-uploads' == $feature )
1551 return current_theme_supports( 'custom-header', 'uploads' );
1553 if ( !isset( $_wp_theme_features[$feature] ) )
1556 // If no args passed then no extra checks need be performed
1557 if ( func_num_args() <= 1 )
1560 $args = array_slice( func_get_args(), 1 );
1562 switch ( $feature ) {
1563 case 'post-thumbnails':
1564 // post-thumbnails can be registered for only certain content/post types by passing
1565 // an array of types to add_theme_support(). If no array was passed, then
1566 // any type is accepted
1567 if ( true === $_wp_theme_features[$feature] ) // Registered for all types
1569 $content_type = $args[0];
1570 return in_array( $content_type, $_wp_theme_features[$feature][0] );
1574 case 'post-formats':
1575 // specific post formats can be registered by passing an array of types to
1576 // add_theme_support()
1578 // Specific areas of HTML5 support *must* be passed via an array to add_theme_support()
1581 return in_array( $type, $_wp_theme_features[$feature][0] );
1584 case 'custom-header':
1585 case 'custom-background' :
1586 // specific custom header and background capabilities can be registered by passing
1587 // an array to add_theme_support()
1588 $header_support = $args[0];
1589 return ( isset( $_wp_theme_features[$feature][0][$header_support] ) && $_wp_theme_features[$feature][0][$header_support] );
1593 return apply_filters('current_theme_supports-' . $feature, true, $args, $_wp_theme_features[$feature]);
1597 * Checks a theme's support for a given feature before loading the functions which implement it.
1600 * @param string $feature the feature being checked
1601 * @param string $include the file containing the functions that implement the feature
1603 function require_if_theme_supports( $feature, $include) {
1604 if ( current_theme_supports( $feature ) )
1605 require ( $include );
1609 * Checks an attachment being deleted to see if it's a header or background image.
1611 * If true it removes the theme modification which would be pointing at the deleted
1616 * @param int $id the attachment id
1618 function _delete_attachment_theme_mod( $id ) {
1619 $attachment_image = wp_get_attachment_url( $id );
1620 $header_image = get_header_image();
1621 $background_image = get_background_image();
1623 if ( $header_image && $header_image == $attachment_image )
1624 remove_theme_mod( 'header_image' );
1626 if ( $background_image && $background_image == $attachment_image )
1627 remove_theme_mod( 'background_image' );
1630 add_action( 'delete_attachment', '_delete_attachment_theme_mod' );
1633 * Checks if a theme has been changed and runs 'after_switch_theme' hook on the next WP load
1637 function check_theme_switched() {
1638 if ( $stylesheet = get_option( 'theme_switched' ) ) {
1639 $old_theme = wp_get_theme( $stylesheet );
1641 if ( $old_theme->exists() )
1642 do_action( 'after_switch_theme', $old_theme->get('Name'), $old_theme );
1644 do_action( 'after_switch_theme', $stylesheet );
1646 update_option( 'theme_switched', false );
1651 * Includes and instantiates the WP_Customize_Manager class.
1653 * Fires when ?wp_customize=on or on wp-admin/customize.php.
1657 function _wp_customize_include() {
1658 if ( ! ( ( isset( $_REQUEST['wp_customize'] ) && 'on' == $_REQUEST['wp_customize'] )
1659 || ( is_admin() && 'customize.php' == basename( $_SERVER['PHP_SELF'] ) )
1663 require( ABSPATH . WPINC . '/class-wp-customize-manager.php' );
1664 // Init Customize class
1665 $GLOBALS['wp_customize'] = new WP_Customize_Manager;
1667 add_action( 'plugins_loaded', '_wp_customize_include' );
1670 * Adds settings for the customize-loader script.
1674 function _wp_customize_loader_settings() {
1677 $admin_origin = parse_url( admin_url() );
1678 $home_origin = parse_url( home_url() );
1679 $cross_domain = ( strtolower( $admin_origin[ 'host' ] ) != strtolower( $home_origin[ 'host' ] ) );
1682 'mobile' => wp_is_mobile(),
1683 'ios' => wp_is_mobile() && preg_match( '/iPad|iPod|iPhone/', $_SERVER['HTTP_USER_AGENT'] ),
1687 'url' => esc_url( admin_url( 'customize.php' ) ),
1688 'isCrossDomain' => $cross_domain,
1689 'browser' => $browser,
1692 $script = 'var _wpCustomizeLoaderSettings = ' . json_encode( $settings ) . ';';
1694 $data = $wp_scripts->get_data( 'customize-loader', 'data' );
1696 $script = "$data\n$script";
1698 $wp_scripts->add_data( 'customize-loader', 'data', $script );
1700 add_action( 'admin_enqueue_scripts', '_wp_customize_loader_settings' );
1703 * Returns a URL to load the theme customizer.
1707 * @param string $stylesheet Optional. Theme to customize. Defaults to current theme.
1708 * The theme's stylesheet will be urlencoded if necessary.
1710 function wp_customize_url( $stylesheet = null ) {
1711 $url = admin_url( 'customize.php' );
1713 $url .= '?theme=' . urlencode( $stylesheet );
1714 return esc_url( $url );
1718 * Prints a script to check whether or not the customizer is supported,
1719 * and apply either the no-customize-support or customize-support class
1722 * This function MUST be called inside the body tag.
1724 * Ideally, call this function immediately after the body tag is opened.
1725 * This prevents a flash of unstyled content.
1727 * It is also recommended that you add the "no-customize-support" class
1728 * to the body tag by default.
1732 function wp_customize_support_script() {
1733 $admin_origin = parse_url( admin_url() );
1734 $home_origin = parse_url( home_url() );
1735 $cross_domain = ( strtolower( $admin_origin[ 'host' ] ) != strtolower( $home_origin[ 'host' ] ) );
1738 <script type="text/javascript">
1740 var request, b = document.body, c = 'className', cs = 'customize-support', rcs = new RegExp('(^|\\s+)(no-)?'+cs+'(\\s+|$)');
1742 <?php if ( $cross_domain ): ?>
1743 request = (function(){ var xhr = new XMLHttpRequest(); return ('withCredentials' in xhr); })();
1748 b[c] = b[c].replace( rcs, ' ' );
1749 b[c] += ( window.postMessage && request ? ' ' : ' no-' ) + cs;