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 * Whether a child theme is in use.
114 * @return bool true if a child theme is in use, false otherwise.
116 function is_child_theme() {
117 return ( TEMPLATEPATH !== STYLESHEETPATH );
121 * Retrieve name of the current stylesheet.
123 * The theme name that the administrator has currently set the front end theme
126 * For all extensive purposes, the template name and the stylesheet name are
127 * going to be the same for most cases.
130 * @uses apply_filters() Calls 'stylesheet' filter on stylesheet name.
132 * @return string Stylesheet name.
134 function get_stylesheet() {
135 return apply_filters('stylesheet', get_option('stylesheet'));
139 * Retrieve stylesheet directory path for current theme.
142 * @uses apply_filters() Calls 'stylesheet_directory' filter on stylesheet directory and theme name.
144 * @return string Path to current theme directory.
146 function get_stylesheet_directory() {
147 $stylesheet = get_stylesheet();
148 $theme_root = get_theme_root( $stylesheet );
149 $stylesheet_dir = "$theme_root/$stylesheet";
151 return apply_filters( 'stylesheet_directory', $stylesheet_dir, $stylesheet, $theme_root );
155 * Retrieve stylesheet directory URI.
161 function get_stylesheet_directory_uri() {
162 $stylesheet = get_stylesheet();
163 $theme_root_uri = get_theme_root_uri( $stylesheet );
164 $stylesheet_dir_uri = "$theme_root_uri/$stylesheet";
166 return apply_filters( 'stylesheet_directory_uri', $stylesheet_dir_uri, $stylesheet, $theme_root_uri );
170 * Retrieve URI of current theme stylesheet.
172 * The stylesheet file name is 'style.css' which is appended to {@link
173 * get_stylesheet_directory_uri() stylesheet directory URI} path.
176 * @uses apply_filters() Calls 'stylesheet_uri' filter on stylesheet URI path and stylesheet directory URI.
180 function get_stylesheet_uri() {
181 $stylesheet_dir_uri = get_stylesheet_directory_uri();
182 $stylesheet_uri = $stylesheet_dir_uri . '/style.css';
183 return apply_filters('stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri);
187 * Retrieve localized stylesheet URI.
189 * The stylesheet directory for the localized stylesheet files are located, by
190 * default, in the base theme directory. The name of the locale file will be the
191 * locale followed by '.css'. If that does not exist, then the text direction
192 * stylesheet will be checked for existence, for example 'ltr.css'.
194 * The theme may change the location of the stylesheet directory by either using
195 * the 'stylesheet_directory_uri' filter or the 'locale_stylesheet_uri' filter.
196 * If you want to change the location of the stylesheet files for the entire
197 * WordPress workflow, then change the former. If you just have the locale in a
198 * separate folder, then change the latter.
201 * @uses apply_filters() Calls 'locale_stylesheet_uri' filter on stylesheet URI path and stylesheet directory URI.
205 function get_locale_stylesheet_uri() {
207 $stylesheet_dir_uri = get_stylesheet_directory_uri();
208 $dir = get_stylesheet_directory();
209 $locale = get_locale();
210 if ( file_exists("$dir/$locale.css") )
211 $stylesheet_uri = "$stylesheet_dir_uri/$locale.css";
212 elseif ( !empty($wp_locale->text_direction) && file_exists("$dir/{$wp_locale->text_direction}.css") )
213 $stylesheet_uri = "$stylesheet_dir_uri/{$wp_locale->text_direction}.css";
215 $stylesheet_uri = '';
216 return apply_filters('locale_stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri);
220 * Retrieve name of the current theme.
223 * @uses apply_filters() Calls 'template' filter on template option.
225 * @return string Template name.
227 function get_template() {
228 return apply_filters('template', get_option('template'));
232 * Retrieve current theme directory.
235 * @uses apply_filters() Calls 'template_directory' filter on template directory path and template name.
237 * @return string Template directory path.
239 function get_template_directory() {
240 $template = get_template();
241 $theme_root = get_theme_root( $template );
242 $template_dir = "$theme_root/$template";
244 return apply_filters( 'template_directory', $template_dir, $template, $theme_root );
248 * Retrieve theme directory URI.
251 * @uses apply_filters() Calls 'template_directory_uri' filter on template directory URI path and template name.
253 * @return string Template directory URI.
255 function get_template_directory_uri() {
256 $template = get_template();
257 $theme_root_uri = get_theme_root_uri( $template );
258 $template_dir_uri = "$theme_root_uri/$template";
260 return apply_filters( 'template_directory_uri', $template_dir_uri, $template, $theme_root_uri );
264 * Retrieve theme roots.
268 * @return array|string An array of theme roots keyed by template/stylesheet or a single theme root if all themes have the same root.
270 function get_theme_roots() {
271 global $wp_theme_directories;
273 if ( count($wp_theme_directories) <= 1 )
276 $theme_roots = get_site_transient( 'theme_roots' );
277 if ( false === $theme_roots ) {
278 search_theme_directories( true ); // Regenerate the transient.
279 $theme_roots = get_site_transient( 'theme_roots' );
285 * Register a directory that contains themes.
289 * @param string $directory Either the full filesystem path to a theme folder or a folder within WP_CONTENT_DIR
292 function register_theme_directory( $directory ) {
293 global $wp_theme_directories;
295 if ( ! file_exists( $directory ) ) {
296 // Try prepending as the theme directory could be relative to the content directory
297 $directory = WP_CONTENT_DIR . '/' . $directory;
298 // If this directory does not exist, return and do not register
299 if ( ! file_exists( $directory ) )
303 $wp_theme_directories[] = $directory;
309 * Search all registered theme directories for complete and valid themes.
313 * @param bool $force Optional. Whether to force a new directory scan. Defaults to false.
314 * @return array Valid themes found
316 function search_theme_directories( $force = false ) {
317 global $wp_theme_directories;
318 if ( empty( $wp_theme_directories ) )
321 static $found_themes;
322 if ( ! $force && isset( $found_themes ) )
323 return $found_themes;
325 $found_themes = array();
327 $wp_theme_directories = (array) $wp_theme_directories;
329 // Set up maybe-relative, maybe-absolute array of theme directories.
330 // We always want to return absolute, but we need to cache relative
331 // use in for get_theme_root().
332 foreach ( $wp_theme_directories as $theme_root ) {
333 if ( 0 === strpos( $theme_root, WP_CONTENT_DIR ) )
334 $relative_theme_roots[ str_replace( WP_CONTENT_DIR, '', $theme_root ) ] = $theme_root;
336 $relative_theme_roots[ $theme_root ] = $theme_root;
339 if ( $cache_expiration = apply_filters( 'wp_cache_themes_persistently', false, 'search_theme_directories' ) ) {
340 $cached_roots = get_site_transient( 'theme_roots' );
341 if ( is_array( $cached_roots ) ) {
342 foreach ( $cached_roots as $theme_dir => $theme_root ) {
343 // A cached theme root is no longer around, so skip it.
344 if ( ! isset( $relative_theme_roots[ $theme_root ] ) )
346 $found_themes[ $theme_dir ] = array(
347 'theme_file' => $theme_dir . '/style.css',
348 'theme_root' => $relative_theme_roots[ $theme_root ], // Convert relative to absolute.
351 return $found_themes;
353 if ( ! is_int( $cache_expiration ) )
354 $cache_expiration = 1800; // half hour
356 $cache_expiration = 1800; // half hour
359 /* Loop the registered theme directories and extract all themes */
360 foreach ( $wp_theme_directories as $theme_root ) {
362 // Start with directories in the root of the current theme directory.
363 $dirs = @ scandir( $theme_root );
366 foreach ( $dirs as $dir ) {
367 if ( ! is_dir( $theme_root . '/' . $dir ) || $dir[0] == '.' || $dir == 'CVS' )
369 if ( file_exists( $theme_root . '/' . $dir . '/style.css' ) ) {
370 // wp-content/themes/a-single-theme
371 // wp-content/themes is $theme_root, a-single-theme is $dir
372 $found_themes[ $dir ] = array(
373 'theme_file' => $dir . '/style.css',
374 'theme_root' => $theme_root,
377 $found_theme = false;
378 // wp-content/themes/a-folder-of-themes/*
379 // wp-content/themes is $theme_root, a-folder-of-themes is $dir, then themes are $sub_dirs
380 $sub_dirs = @ scandir( $theme_root . '/' . $dir );
383 foreach ( $sub_dirs as $sub_dir ) {
384 if ( ! is_dir( $theme_root . '/' . $dir ) || $dir[0] == '.' || $dir == 'CVS' )
386 if ( ! file_exists( $theme_root . '/' . $dir . '/' . $sub_dir . '/style.css' ) )
388 $found_themes[ $dir . '/' . $sub_dir ] = array(
389 'theme_file' => $dir . '/' . $sub_dir . '/style.css',
390 'theme_root' => $theme_root,
394 // Never mind the above, it's just a theme missing a style.css.
395 // Return it; WP_Theme will catch the error.
396 if ( ! $found_theme )
397 $found_themes[ $dir ] = array(
398 'theme_file' => $dir . '/style.css',
399 'theme_root' => $theme_root,
405 asort( $found_themes );
407 $theme_roots = array();
408 $relative_theme_roots = array_flip( $relative_theme_roots );
410 foreach ( $found_themes as $theme_dir => $theme_data ) {
411 $theme_roots[ $theme_dir ] = $relative_theme_roots[ $theme_data['theme_root'] ]; // Convert absolute to relative.
414 if ( $theme_roots != get_site_transient( 'theme_roots' ) )
415 set_site_transient( 'theme_roots', $theme_roots, $cache_expiration );
417 return $found_themes;
421 * Retrieve path to themes directory.
423 * Does not have trailing slash.
426 * @uses apply_filters() Calls 'theme_root' filter on path.
428 * @param string $stylesheet_or_template The stylesheet or template name of the theme
429 * @return string Theme path.
431 function get_theme_root( $stylesheet_or_template = false ) {
432 global $wp_theme_directories;
434 if ( $stylesheet_or_template && $theme_root = get_raw_theme_root( $stylesheet_or_template ) ) {
435 // Always prepend WP_CONTENT_DIR unless the root currently registered as a theme directory.
436 // This gives relative theme roots the benefit of the doubt when things go haywire.
437 if ( ! in_array( $theme_root, (array) $wp_theme_directories ) )
438 $theme_root = WP_CONTENT_DIR . $theme_root;
440 $theme_root = WP_CONTENT_DIR . '/themes';
443 return apply_filters( 'theme_root', $theme_root );
447 * Retrieve URI for themes directory.
449 * Does not have trailing slash.
453 * @param string $stylesheet_or_template Optional. The stylesheet or template name of the theme.
454 * Default is to leverage the main theme root.
455 * @param string $theme_root Optional. The theme root for which calculations will be based, preventing
456 * the need for a get_raw_theme_root() call.
457 * @return string Themes URI.
459 function get_theme_root_uri( $stylesheet_or_template = false, $theme_root = false ) {
460 global $wp_theme_directories;
462 if ( $stylesheet_or_template && ! $theme_root )
463 $theme_root = get_raw_theme_root( $stylesheet_or_template );
465 if ( $stylesheet_or_template && $theme_root ) {
466 if ( in_array( $theme_root, (array) $wp_theme_directories ) ) {
467 // Absolute path. Make an educated guess. YMMV -- but note the filter below.
468 if ( 0 === strpos( $theme_root, WP_CONTENT_DIR ) )
469 $theme_root_uri = content_url( str_replace( WP_CONTENT_DIR, '', $theme_root ) );
470 elseif ( 0 === strpos( $theme_root, ABSPATH ) )
471 $theme_root_uri = site_url( str_replace( ABSPATH, '', $theme_root ) );
472 elseif ( 0 === strpos( $theme_root, WP_PLUGIN_DIR ) || 0 === strpos( $theme_root, WPMU_PLUGIN_DIR ) )
473 $theme_root_uri = plugins_url( basename( $theme_root ), $theme_root );
475 $theme_root_uri = $theme_root;
477 $theme_root_uri = content_url( $theme_root );
480 $theme_root_uri = content_url( 'themes' );
483 return apply_filters( 'theme_root_uri', $theme_root_uri, get_option('siteurl'), $stylesheet_or_template );
487 * Get the raw theme root relative to the content directory with no filters applied.
491 * @param string $stylesheet_or_template The stylesheet or template name of the theme
492 * @param bool $skip_cache Optional. Whether to skip the cache. Defaults to false, meaning the cache is used.
493 * @return string Theme root
495 function get_raw_theme_root( $stylesheet_or_template, $skip_cache = false ) {
496 global $wp_theme_directories;
498 if ( count($wp_theme_directories) <= 1 )
503 // If requesting the root for the current theme, consult options to avoid calling get_theme_roots()
504 if ( ! $skip_cache ) {
505 if ( get_option('stylesheet') == $stylesheet_or_template )
506 $theme_root = get_option('stylesheet_root');
507 elseif ( get_option('template') == $stylesheet_or_template )
508 $theme_root = get_option('template_root');
511 if ( empty($theme_root) ) {
512 $theme_roots = get_theme_roots();
513 if ( !empty($theme_roots[$stylesheet_or_template]) )
514 $theme_root = $theme_roots[$stylesheet_or_template];
521 * Display localized stylesheet link element.
525 function locale_stylesheet() {
526 $stylesheet = get_locale_stylesheet_uri();
527 if ( empty($stylesheet) )
529 echo '<link rel="stylesheet" href="' . $stylesheet . '" type="text/css" media="screen" />';
533 * Start preview theme output buffer.
535 * Will only preform task if the user has permissions and template and preview
536 * query variables exist.
540 function preview_theme() {
541 if ( ! (isset($_GET['template']) && isset($_GET['preview'])) )
544 if ( !current_user_can( 'switch_themes' ) )
547 // Admin Thickbox requests
548 if ( isset( $_GET['preview_iframe'] ) )
549 show_admin_bar( false );
551 $_GET['template'] = preg_replace('|[^a-z0-9_./-]|i', '', $_GET['template']);
553 if ( validate_file($_GET['template']) )
556 add_filter( 'template', '_preview_theme_template_filter' );
558 if ( isset($_GET['stylesheet']) ) {
559 $_GET['stylesheet'] = preg_replace('|[^a-z0-9_./-]|i', '', $_GET['stylesheet']);
560 if ( validate_file($_GET['stylesheet']) )
562 add_filter( 'stylesheet', '_preview_theme_stylesheet_filter' );
565 // Prevent theme mods to current theme being used on theme being previewed
566 add_filter( 'pre_option_theme_mods_' . get_option( 'stylesheet' ), '__return_empty_array' );
568 ob_start( 'preview_theme_ob_filter' );
570 add_action('setup_theme', 'preview_theme');
573 * Private function to modify the current template when previewing a theme
580 function _preview_theme_template_filter() {
581 return isset($_GET['template']) ? $_GET['template'] : '';
585 * Private function to modify the current stylesheet when previewing a theme
592 function _preview_theme_stylesheet_filter() {
593 return isset($_GET['stylesheet']) ? $_GET['stylesheet'] : '';
597 * Callback function for ob_start() to capture all links in the theme.
602 * @param string $content
605 function preview_theme_ob_filter( $content ) {
606 return preg_replace_callback( "|(<a.*?href=([\"']))(.*?)([\"'].*?>)|", 'preview_theme_ob_filter_callback', $content );
610 * Manipulates preview theme links in order to control and maintain location.
612 * Callback function for preg_replace_callback() to accept and filter matches.
617 * @param array $matches
620 function preview_theme_ob_filter_callback( $matches ) {
621 if ( strpos($matches[4], 'onclick') !== false )
622 $matches[4] = preg_replace('#onclick=([\'"]).*?(?<!\\\)\\1#i', '', $matches[4]); //Strip out any onclicks from rest of <a>. (?<!\\\) means to ignore the '" if its escaped by \ to prevent breaking mid-attribute.
624 ( false !== strpos($matches[3], '/wp-admin/') )
626 ( false !== strpos( $matches[3], '://' ) && 0 !== strpos( $matches[3], home_url() ) )
628 ( false !== strpos($matches[3], '/feed/') )
630 ( false !== strpos($matches[3], '/trackback/') )
632 return $matches[1] . "#$matches[2] onclick=$matches[2]return false;" . $matches[4];
634 $link = add_query_arg( array( 'preview' => 1, 'template' => $_GET['template'], 'stylesheet' => @$_GET['stylesheet'], 'preview_iframe' => 1 ), $matches[3] );
635 if ( 0 === strpos($link, 'preview=1') )
637 return $matches[1] . esc_attr( $link ) . $matches[4];
641 * Switches current theme to new template and stylesheet names.
644 * @uses do_action() Calls 'switch_theme' action, passing the new theme.
646 * @param string $template Template name
647 * @param string $stylesheet Stylesheet name.
649 function switch_theme( $template, $stylesheet ) {
650 global $wp_theme_directories, $sidebars_widgets;
652 if ( is_array( $sidebars_widgets ) )
653 set_theme_mod( 'sidebars_widgets', array( 'time' => time(), 'data' => $sidebars_widgets ) );
655 $old_theme = wp_get_theme();
656 $new_theme = wp_get_theme( $stylesheet );
657 $new_name = $new_theme->get('Name');
659 update_option( 'template', $template );
660 update_option( 'stylesheet', $stylesheet );
662 if ( count( $wp_theme_directories ) > 1 ) {
663 update_option( 'template_root', get_raw_theme_root( $template, true ) );
664 update_option( 'stylesheet_root', get_raw_theme_root( $stylesheet, true ) );
667 update_option( 'current_theme', $new_name );
669 if ( is_admin() && false === get_option( 'theme_mods_' . $stylesheet ) ) {
670 $default_theme_mods = (array) get_option( 'mods_' . $new_name );
671 add_option( "theme_mods_$stylesheet", $default_theme_mods );
674 update_option( 'theme_switched', $old_theme->get_stylesheet() );
675 do_action( 'switch_theme', $new_name, $new_theme );
679 * Checks that current theme files 'index.php' and 'style.css' exists.
681 * Does not check the default theme, which is the fallback and should always exist.
682 * Will switch theme to the fallback theme if current theme does not validate.
683 * You can use the 'validate_current_theme' filter to return false to
684 * disable this functionality.
687 * @see WP_DEFAULT_THEME
691 function validate_current_theme() {
692 // Don't validate during an install/upgrade.
693 if ( defined('WP_INSTALLING') || !apply_filters( 'validate_current_theme', true ) )
696 if ( get_template() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/index.php') ) {
697 switch_theme( WP_DEFAULT_THEME, WP_DEFAULT_THEME );
701 if ( get_stylesheet() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/style.css') ) {
702 switch_theme( WP_DEFAULT_THEME, WP_DEFAULT_THEME );
706 if ( is_child_theme() && ! file_exists( get_stylesheet_directory() . '/style.css' ) ) {
707 switch_theme( WP_DEFAULT_THEME, WP_DEFAULT_THEME );
715 * Retrieve all theme modifications.
719 * @return array Theme modifications.
721 function get_theme_mods() {
722 $theme_slug = get_option( 'stylesheet' );
723 if ( false === ( $mods = get_option( "theme_mods_$theme_slug" ) ) ) {
724 $theme_name = get_option( 'current_theme' );
725 if ( false === $theme_name )
726 $theme_name = wp_get_theme()->get('Name');
727 $mods = get_option( "mods_$theme_name" ); // Deprecated location.
728 if ( is_admin() && false !== $mods ) {
729 update_option( "theme_mods_$theme_slug", $mods );
730 delete_option( "mods_$theme_name" );
737 * Retrieve theme modification value for the current theme.
739 * If the modification name does not exist, then the $default will be passed
740 * through {@link http://php.net/sprintf sprintf()} PHP function with the first
741 * string the template directory URI and the second string the stylesheet
745 * @uses apply_filters() Calls 'theme_mod_$name' filter on the value.
747 * @param string $name Theme modification name.
748 * @param bool|string $default
751 function get_theme_mod( $name, $default = false ) {
752 $mods = get_theme_mods();
754 if ( isset( $mods[ $name ] ) )
755 return apply_filters( "theme_mod_$name", $mods[ $name ] );
757 if ( is_string( $default ) )
758 $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
760 return apply_filters( "theme_mod_$name", $default );
764 * Update theme modification value for the current theme.
768 * @param string $name Theme modification name.
769 * @param string $value theme modification value.
771 function set_theme_mod( $name, $value ) {
772 $mods = get_theme_mods();
774 $mods[ $name ] = $value;
776 $theme = get_option( 'stylesheet' );
777 update_option( "theme_mods_$theme", $mods );
781 * Remove theme modification name from current theme list.
783 * If removing the name also removes all elements, then the entire option will
788 * @param string $name Theme modification name.
791 function remove_theme_mod( $name ) {
792 $mods = get_theme_mods();
794 if ( ! isset( $mods[ $name ] ) )
797 unset( $mods[ $name ] );
799 if ( empty( $mods ) )
800 return remove_theme_mods();
802 $theme = get_option( 'stylesheet' );
803 update_option( "theme_mods_$theme", $mods );
807 * Remove theme modifications option for current theme.
811 function remove_theme_mods() {
812 delete_option( 'theme_mods_' . get_option( 'stylesheet' ) );
815 $theme_name = get_option( 'current_theme' );
816 if ( false === $theme_name )
817 $theme_name = wp_get_theme()->get('Name');
818 delete_option( 'mods_' . $theme_name );
822 * Retrieve text color for custom header.
828 function get_header_textcolor() {
829 return get_theme_mod('header_textcolor', get_theme_support( 'custom-header', 'default-text-color' ) );
833 * Display text color for custom header.
837 function header_textcolor() {
838 echo get_header_textcolor();
842 * Whether to display the header text.
848 function display_header_text() {
849 if ( ! current_theme_supports( 'custom-header', 'header-text' ) )
852 $text_color = get_theme_mod( 'header_textcolor', get_theme_support( 'custom-header', 'default-text-color' ) );
853 return 'blank' != $text_color;
857 * Retrieve header image for custom header.
863 function get_header_image() {
864 $url = get_theme_mod( 'header_image', get_theme_support( 'custom-header', 'default-image' ) );
866 if ( 'remove-header' == $url )
869 if ( is_random_header_image() )
870 $url = get_random_header_image();
873 $url = str_replace( 'http://', 'https://', $url );
875 $url = str_replace( 'https://', 'http://', $url );
877 return esc_url_raw( $url );
881 * Get random header image data from registered images in theme.
887 * @return string Path to header image
890 function _get_random_header_data() {
891 static $_wp_random_header;
893 if ( empty( $_wp_random_header ) ) {
894 global $_wp_default_headers;
895 $header_image_mod = get_theme_mod( 'header_image', '' );
898 if ( 'random-uploaded-image' == $header_image_mod )
899 $headers = get_uploaded_header_images();
900 elseif ( ! empty( $_wp_default_headers ) ) {
901 if ( 'random-default-image' == $header_image_mod ) {
902 $headers = $_wp_default_headers;
904 if ( current_theme_supports( 'custom-header', 'random-default' ) )
905 $headers = $_wp_default_headers;
909 if ( empty( $headers ) )
912 $_wp_random_header = (object) $headers[ array_rand( $headers ) ];
914 $_wp_random_header->url = sprintf( $_wp_random_header->url, get_template_directory_uri(), get_stylesheet_directory_uri() );
915 $_wp_random_header->thumbnail_url = sprintf( $_wp_random_header->thumbnail_url, get_template_directory_uri(), get_stylesheet_directory_uri() );
917 return $_wp_random_header;
921 * Get random header image url from registered images in theme.
925 * @return string Path to header image
928 function get_random_header_image() {
929 $random_image = _get_random_header_data();
930 if ( empty( $random_image->url ) )
932 return $random_image->url;
936 * Check if random header image is in use.
938 * Always true if user expressly chooses the option in Appearance > Header.
939 * Also true if theme has multiple header images registered, no specific header image
940 * is chosen, and theme turns on random headers with add_theme_support().
944 * @param string $type The random pool to use. any|default|uploaded
947 function is_random_header_image( $type = 'any' ) {
948 $header_image_mod = get_theme_mod( 'header_image', get_theme_support( 'custom-header', 'default-image' ) );
950 if ( 'any' == $type ) {
951 if ( 'random-default-image' == $header_image_mod || 'random-uploaded-image' == $header_image_mod || ( '' != get_random_header_image() && empty( $header_image_mod ) ) )
954 if ( "random-$type-image" == $header_image_mod )
956 elseif ( 'default' == $type && empty( $header_image_mod ) && '' != get_random_header_image() )
964 * Display header image path.
968 function header_image() {
969 echo get_header_image();
973 * Get the header images uploaded for the current theme.
979 function get_uploaded_header_images() {
980 $header_images = array();
983 $headers = get_posts( array( 'post_type' => 'attachment', 'meta_key' => '_wp_attachment_is_custom_header', 'meta_value' => get_option('stylesheet'), 'orderby' => 'none', 'nopaging' => true ) );
985 if ( empty( $headers ) )
988 foreach ( (array) $headers as $header ) {
989 $url = esc_url_raw( $header->guid );
990 $header_data = wp_get_attachment_metadata( $header->ID );
991 $header_index = basename($url);
992 $header_images[$header_index] = array();
993 $header_images[$header_index]['attachment_id'] = $header->ID;
994 $header_images[$header_index]['url'] = $url;
995 $header_images[$header_index]['thumbnail_url'] = $url;
996 $header_images[$header_index]['width'] = $header_data['width'];
997 $header_images[$header_index]['height'] = $header_data['height'];
1000 return $header_images;
1004 * Get the header image data.
1010 function get_custom_header() {
1011 $data = is_random_header_image()? _get_random_header_data() : get_theme_mod( 'header_image_data' );
1014 'thumbnail_url' => '',
1015 'width' => get_theme_support( 'custom-header', 'width' ),
1016 'height' => get_theme_support( 'custom-header', 'height' ),
1018 return (object) wp_parse_args( $data, $default );
1022 * Register a selection of default headers to be displayed by the custom header admin UI.
1026 * @param array $headers Array of headers keyed by a string id. The ids point to arrays containing 'url', 'thumbnail_url', and 'description' keys.
1028 function register_default_headers( $headers ) {
1029 global $_wp_default_headers;
1031 $_wp_default_headers = array_merge( (array) $_wp_default_headers, (array) $headers );
1035 * Unregister default headers.
1037 * This function must be called after register_default_headers() has already added the
1038 * header you want to remove.
1040 * @see register_default_headers()
1043 * @param string|array $header The header string id (key of array) to remove, or an array thereof.
1044 * @return True on success, false on failure.
1046 function unregister_default_headers( $header ) {
1047 global $_wp_default_headers;
1048 if ( is_array( $header ) ) {
1049 array_map( 'unregister_default_headers', $header );
1050 } elseif ( isset( $_wp_default_headers[ $header ] ) ) {
1051 unset( $_wp_default_headers[ $header ] );
1059 * Retrieve background image for custom background.
1065 function get_background_image() {
1066 return get_theme_mod('background_image', get_theme_support( 'custom-background', 'default-image' ) );
1070 * Display background image path.
1074 function background_image() {
1075 echo get_background_image();
1079 * Retrieve value for custom background color.
1085 function get_background_color() {
1086 return get_theme_mod('background_color', get_theme_support( 'custom-background', 'default-color' ) );
1090 * Display background color value.
1094 function background_color() {
1095 echo get_background_color();
1099 * Default custom background callback.
1104 function _custom_background_cb() {
1105 // $background is the saved custom image, or the default image.
1106 $background = get_background_image();
1108 // $color is the saved custom color.
1109 // A default has to be specified in style.css. It will not be printed here.
1110 $color = get_theme_mod( 'background_color' );
1112 if ( ! $background && ! $color )
1115 $style = $color ? "background-color: #$color;" : '';
1117 if ( $background ) {
1118 $image = " background-image: url('$background');";
1120 $repeat = get_theme_mod( 'background_repeat', 'repeat' );
1121 if ( ! in_array( $repeat, array( 'no-repeat', 'repeat-x', 'repeat-y', 'repeat' ) ) )
1123 $repeat = " background-repeat: $repeat;";
1125 $position = get_theme_mod( 'background_position_x', 'left' );
1126 if ( ! in_array( $position, array( 'center', 'right', 'left' ) ) )
1128 $position = " background-position: top $position;";
1130 $attachment = get_theme_mod( 'background_attachment', 'scroll' );
1131 if ( ! in_array( $attachment, array( 'fixed', 'scroll' ) ) )
1132 $attachment = 'scroll';
1133 $attachment = " background-attachment: $attachment;";
1135 $style .= $image . $repeat . $position . $attachment;
1138 <style type="text/css" id="custom-background-css">
1139 body.custom-background { <?php echo trim( $style ); ?> }
1145 * Add callback for custom TinyMCE editor stylesheets.
1147 * The parameter $stylesheet is the name of the stylesheet, relative to
1148 * the theme root. It also accepts an array of stylesheets.
1149 * It is optional and defaults to 'editor-style.css'.
1151 * This function automatically adds another stylesheet with -rtl prefix, e.g. editor-style-rtl.css.
1152 * If that file doesn't exist, it is removed before adding the stylesheet(s) to TinyMCE.
1153 * If an array of stylesheets is passed to add_editor_style(),
1154 * RTL is only added for the first stylesheet.
1156 * Since version 3.4 the TinyMCE body has .rtl CSS class.
1157 * It is a better option to use that class and add any RTL styles to the main stylesheet.
1161 * @param mixed $stylesheet Optional. Stylesheet name or array thereof, relative to theme root.
1162 * Defaults to 'editor-style.css'
1164 function add_editor_style( $stylesheet = 'editor-style.css' ) {
1166 add_theme_support( 'editor-style' );
1171 global $editor_styles;
1172 $editor_styles = (array) $editor_styles;
1173 $stylesheet = (array) $stylesheet;
1175 $rtl_stylesheet = str_replace('.css', '-rtl.css', $stylesheet[0]);
1176 $stylesheet[] = $rtl_stylesheet;
1179 $editor_styles = array_merge( $editor_styles, $stylesheet );
1183 * Removes all visual editor stylesheets.
1187 * @return bool True on success, false if there were no stylesheets to remove.
1189 function remove_editor_styles() {
1190 if ( ! current_theme_supports( 'editor-style' ) )
1192 _remove_theme_support( 'editor-style' );
1194 $GLOBALS['editor_styles'] = array();
1199 * Allows a theme to register its support of a certain feature
1201 * Must be called in the theme's functions.php file to work.
1202 * If attached to a hook, it must be after_setup_theme.
1203 * The init hook may be too late for some features.
1206 * @param string $feature the feature being added
1208 function add_theme_support( $feature ) {
1209 global $_wp_theme_features;
1211 if ( func_num_args() == 1 )
1214 $args = array_slice( func_get_args(), 1 );
1216 switch ( $feature ) {
1217 case 'post-formats' :
1218 if ( is_array( $args[0] ) )
1219 $args[0] = array_intersect( $args[0], array_keys( get_post_format_slugs() ) );
1222 case 'custom-header-uploads' :
1223 return add_theme_support( 'custom-header', array( 'uploads' => true ) );
1226 case 'custom-header' :
1227 if ( ! is_array( $args ) )
1228 $args = array( 0 => array() );
1231 'default-image' => '',
1232 'random-default' => false,
1235 'flex-height' => false,
1236 'flex-width' => false,
1237 'default-text-color' => '',
1238 'header-text' => true,
1240 'wp-head-callback' => '',
1241 'admin-head-callback' => '',
1242 'admin-preview-callback' => '',
1245 $jit = isset( $args[0]['__jit'] );
1246 unset( $args[0]['__jit'] );
1248 // Merge in data from previous add_theme_support() calls.
1249 // The first value registered wins. (A child theme is set up first.)
1250 if ( isset( $_wp_theme_features['custom-header'] ) )
1251 $args[0] = wp_parse_args( $_wp_theme_features['custom-header'][0], $args[0] );
1253 // Load in the defaults at the end, as we need to insure first one wins.
1254 // This will cause all constants to be defined, as each arg will then be set to the default.
1256 $args[0] = wp_parse_args( $args[0], $defaults );
1258 // If a constant was defined, use that value. Otherwise, define the constant to ensure
1259 // the constant is always accurate (and is not defined later, overriding our value).
1260 // As stated above, the first value wins.
1261 // Once we get to wp_loaded (just-in-time), define any constants we haven't already.
1262 // Constants are lame. Don't reference them. This is just for backwards compatibility.
1264 if ( defined( 'NO_HEADER_TEXT' ) )
1265 $args[0]['header-text'] = ! NO_HEADER_TEXT;
1266 elseif ( isset( $args[0]['header-text'] ) )
1267 define( 'NO_HEADER_TEXT', empty( $args[0]['header-text'] ) );
1269 if ( defined( 'HEADER_IMAGE_WIDTH' ) )
1270 $args[0]['width'] = (int) HEADER_IMAGE_WIDTH;
1271 elseif ( isset( $args[0]['width'] ) )
1272 define( 'HEADER_IMAGE_WIDTH', (int) $args[0]['width'] );
1274 if ( defined( 'HEADER_IMAGE_HEIGHT' ) )
1275 $args[0]['height'] = (int) HEADER_IMAGE_HEIGHT;
1276 elseif ( isset( $args[0]['height'] ) )
1277 define( 'HEADER_IMAGE_HEIGHT', (int) $args[0]['height'] );
1279 if ( defined( 'HEADER_TEXTCOLOR' ) )
1280 $args[0]['default-text-color'] = HEADER_TEXTCOLOR;
1281 elseif ( isset( $args[0]['default-text-color'] ) )
1282 define( 'HEADER_TEXTCOLOR', $args[0]['default-text-color'] );
1284 if ( defined( 'HEADER_IMAGE' ) )
1285 $args[0]['default-image'] = HEADER_IMAGE;
1286 elseif ( isset( $args[0]['default-image'] ) )
1287 define( 'HEADER_IMAGE', $args[0]['default-image'] );
1289 if ( $jit && ! empty( $args[0]['default-image'] ) )
1290 $args[0]['random-default'] = false;
1292 // If headers are supported, and we still don't have a defined width or height,
1293 // we have implicit flex sizes.
1295 if ( empty( $args[0]['width'] ) && empty( $args[0]['flex-width'] ) )
1296 $args[0]['flex-width'] = true;
1297 if ( empty( $args[0]['height'] ) && empty( $args[0]['flex-height'] ) )
1298 $args[0]['flex-height'] = true;
1303 case 'custom-background' :
1304 if ( ! is_array( $args ) )
1305 $args = array( 0 => array() );
1308 'default-image' => '',
1309 'default-color' => '',
1310 'wp-head-callback' => '_custom_background_cb',
1311 'admin-head-callback' => '',
1312 'admin-preview-callback' => '',
1315 $jit = isset( $args[0]['__jit'] );
1316 unset( $args[0]['__jit'] );
1318 // Merge in data from previous add_theme_support() calls. The first value registered wins.
1319 if ( isset( $_wp_theme_features['custom-background'] ) )
1320 $args[0] = wp_parse_args( $_wp_theme_features['custom-background'][0], $args[0] );
1323 $args[0] = wp_parse_args( $args[0], $defaults );
1325 if ( defined( 'BACKGROUND_COLOR' ) )
1326 $args[0]['default-color'] = BACKGROUND_COLOR;
1327 elseif ( isset( $args[0]['default-color'] ) || $jit )
1328 define( 'BACKGROUND_COLOR', $args[0]['default-color'] );
1330 if ( defined( 'BACKGROUND_IMAGE' ) )
1331 $args[0]['default-image'] = BACKGROUND_IMAGE;
1332 elseif ( isset( $args[0]['default-image'] ) || $jit )
1333 define( 'BACKGROUND_IMAGE', $args[0]['default-image'] );
1338 $_wp_theme_features[ $feature ] = $args;
1342 * Registers the internal custom header and background routines.
1347 function _custom_header_background_just_in_time() {
1348 global $custom_image_header, $custom_background;
1350 if ( current_theme_supports( 'custom-header' ) ) {
1351 // In case any constants were defined after an add_custom_image_header() call, re-run.
1352 add_theme_support( 'custom-header', array( '__jit' => true ) );
1354 $args = get_theme_support( 'custom-header' );
1355 if ( $args[0]['wp-head-callback'] )
1356 add_action( 'wp_head', $args[0]['wp-head-callback'] );
1359 require_once( ABSPATH . 'wp-admin/custom-header.php' );
1360 $custom_image_header = new Custom_Image_Header( $args[0]['admin-head-callback'], $args[0]['admin-preview-callback'] );
1364 if ( current_theme_supports( 'custom-background' ) ) {
1365 // In case any constants were defined after an add_custom_background() call, re-run.
1366 add_theme_support( 'custom-background', array( '__jit' => true ) );
1368 $args = get_theme_support( 'custom-background' );
1369 add_action( 'wp_head', $args[0]['wp-head-callback'] );
1372 require_once( ABSPATH . 'wp-admin/custom-background.php' );
1373 $custom_background = new Custom_Background( $args[0]['admin-head-callback'], $args[0]['admin-preview-callback'] );
1377 add_action( 'wp_loaded', '_custom_header_background_just_in_time' );
1380 * Gets the theme support arguments passed when registering that support
1383 * @param string $feature the feature to check
1384 * @return array The array of extra arguments
1386 function get_theme_support( $feature ) {
1387 global $_wp_theme_features;
1388 if ( ! isset( $_wp_theme_features[ $feature ] ) )
1391 if ( func_num_args() <= 1 )
1392 return $_wp_theme_features[ $feature ];
1394 $args = array_slice( func_get_args(), 1 );
1395 switch ( $feature ) {
1396 case 'custom-header' :
1397 case 'custom-background' :
1398 if ( isset( $_wp_theme_features[ $feature ][0][ $args[0] ] ) )
1399 return $_wp_theme_features[ $feature ][0][ $args[0] ];
1403 return $_wp_theme_features[ $feature ];
1409 * Allows a theme to de-register its support of a certain feature
1411 * Should be called in the theme's functions.php file. Generally would
1412 * be used for child themes to override support from the parent theme.
1415 * @see add_theme_support()
1416 * @param string $feature the feature being added
1417 * @return bool Whether feature was removed.
1419 function remove_theme_support( $feature ) {
1420 // Blacklist: for internal registrations not used directly by themes.
1421 if ( in_array( $feature, array( 'editor-style', 'widgets', 'menus' ) ) )
1424 return _remove_theme_support( $feature );
1428 * Do not use. Removes theme support internally, ignorant of the blacklist.
1433 function _remove_theme_support( $feature ) {
1434 global $_wp_theme_features;
1436 switch ( $feature ) {
1437 case 'custom-header-uploads' :
1438 if ( ! isset( $_wp_theme_features['custom-header'] ) )
1440 add_theme_support( 'custom-header', array( 'uploads' => false ) );
1441 return; // Do not continue - custom-header-uploads no longer exists.
1444 if ( ! isset( $_wp_theme_features[ $feature ] ) )
1447 switch ( $feature ) {
1448 case 'custom-header' :
1449 $support = get_theme_support( 'custom-header' );
1450 if ( $support[0]['wp-head-callback'] )
1451 remove_action( 'wp_head', $support[0]['wp-head-callback'] );
1452 remove_action( 'admin_menu', array( $GLOBALS['custom_image_header'], 'init' ) );
1453 unset( $GLOBALS['custom_image_header'] );
1456 case 'custom-background' :
1457 $support = get_theme_support( 'custom-background' );
1458 remove_action( 'wp_head', $support[0]['wp-head-callback'] );
1459 remove_action( 'admin_menu', array( $GLOBALS['custom_background'], 'init' ) );
1460 unset( $GLOBALS['custom_background'] );
1464 unset( $_wp_theme_features[ $feature ] );
1469 * Checks a theme's support for a given feature
1472 * @param string $feature the feature being checked
1475 function current_theme_supports( $feature ) {
1476 global $_wp_theme_features;
1478 if ( 'custom-header-uploads' == $feature )
1479 return current_theme_supports( 'custom-header', 'uploads' );
1481 if ( !isset( $_wp_theme_features[$feature] ) )
1484 // If no args passed then no extra checks need be performed
1485 if ( func_num_args() <= 1 )
1488 $args = array_slice( func_get_args(), 1 );
1490 switch ( $feature ) {
1491 case 'post-thumbnails':
1492 // post-thumbnails can be registered for only certain content/post types by passing
1493 // an array of types to add_theme_support(). If no array was passed, then
1494 // any type is accepted
1495 if ( true === $_wp_theme_features[$feature] ) // Registered for all types
1497 $content_type = $args[0];
1498 return in_array( $content_type, $_wp_theme_features[$feature][0] );
1501 case 'post-formats':
1502 // specific post formats can be registered by passing an array of types to
1503 // add_theme_support()
1504 $post_format = $args[0];
1505 return in_array( $post_format, $_wp_theme_features[$feature][0] );
1508 case 'custom-header':
1509 case 'custom-background' :
1510 // specific custom header and background capabilities can be registered by passing
1511 // an array to add_theme_support()
1512 $header_support = $args[0];
1513 return ( isset( $_wp_theme_features[$feature][0][$header_support] ) && $_wp_theme_features[$feature][0][$header_support] );
1517 return apply_filters('current_theme_supports-' . $feature, true, $args, $_wp_theme_features[$feature]);
1521 * Checks a theme's support for a given feature before loading the functions which implement it.
1524 * @param string $feature the feature being checked
1525 * @param string $include the file containing the functions that implement the feature
1527 function require_if_theme_supports( $feature, $include) {
1528 if ( current_theme_supports( $feature ) )
1529 require ( $include );
1533 * Checks an attachment being deleted to see if it's a header or background image.
1535 * If true it removes the theme modification which would be pointing at the deleted
1540 * @param int $id the attachment id
1542 function _delete_attachment_theme_mod( $id ) {
1543 $attachment_image = wp_get_attachment_url( $id );
1544 $header_image = get_header_image();
1545 $background_image = get_background_image();
1547 if ( $header_image && $header_image == $attachment_image )
1548 remove_theme_mod( 'header_image' );
1550 if ( $background_image && $background_image == $attachment_image )
1551 remove_theme_mod( 'background_image' );
1554 add_action( 'delete_attachment', '_delete_attachment_theme_mod' );
1557 * Checks if a theme has been changed and runs 'after_switch_theme' hook on the next WP load
1561 function check_theme_switched() {
1562 if ( $stylesheet = get_option( 'theme_switched' ) ) {
1563 $old_theme = wp_get_theme( $stylesheet );
1565 if ( $old_theme->exists() )
1566 do_action( 'after_switch_theme', $old_theme->get('Name'), $old_theme );
1568 do_action( 'after_switch_theme', $stylesheet );
1570 update_option( 'theme_switched', false );
1575 * Includes and instantiates the WP_Customize_Manager class.
1577 * Fires when ?wp_customize=on or on wp-admin/customize.php.
1581 function _wp_customize_include() {
1582 if ( ! ( ( isset( $_REQUEST['wp_customize'] ) && 'on' == $_REQUEST['wp_customize'] )
1583 || ( is_admin() && 'customize.php' == basename( $_SERVER['PHP_SELF'] ) )
1587 require( ABSPATH . WPINC . '/class-wp-customize-manager.php' );
1588 // Init Customize class
1589 $GLOBALS['wp_customize'] = new WP_Customize_Manager;
1591 add_action( 'plugins_loaded', '_wp_customize_include' );
1594 * Adds settings for the customize-loader script.
1598 function _wp_customize_loader_settings() {
1601 $admin_origin = parse_url( admin_url() );
1602 $home_origin = parse_url( home_url() );
1603 $cross_domain = ( strtolower( $admin_origin[ 'host' ] ) != strtolower( $home_origin[ 'host' ] ) );
1606 'mobile' => wp_is_mobile(),
1607 'ios' => wp_is_mobile() && preg_match( '/iPad|iPod|iPhone/', $_SERVER['HTTP_USER_AGENT'] ),
1611 'url' => esc_url( admin_url( 'customize.php' ) ),
1612 'isCrossDomain' => $cross_domain,
1613 'browser' => $browser,
1616 $script = 'var _wpCustomizeLoaderSettings = ' . json_encode( $settings ) . ';';
1618 $data = $wp_scripts->get_data( 'customize-loader', 'data' );
1620 $script = "$data\n$script";
1622 $wp_scripts->add_data( 'customize-loader', 'data', $script );
1624 add_action( 'admin_enqueue_scripts', '_wp_customize_loader_settings' );
1627 * Returns a URL to load the theme customizer.
1631 * @param string $stylesheet Optional. Theme to customize. Defaults to current theme.
1633 function wp_customize_url( $stylesheet = null ) {
1634 $url = admin_url( 'customize.php' );
1636 $url .= '?theme=' . $stylesheet;
1637 return esc_url( $url );
1641 * Prints a script to check whether or not the customizer is supported,
1642 * and apply either the no-customize-support or customize-support class
1645 * This function MUST be called inside the body tag.
1647 * Ideally, call this function immediately after the body tag is opened.
1648 * This prevents a flash of unstyled content.
1650 * It is also recommended that you add the "no-customize-support" class
1651 * to the body tag by default.
1655 function wp_customize_support_script() {
1656 $admin_origin = parse_url( admin_url() );
1657 $home_origin = parse_url( home_url() );
1658 $cross_domain = ( strtolower( $admin_origin[ 'host' ] ) != strtolower( $home_origin[ 'host' ] ) );
1661 <script type="text/javascript">
1663 var request, b = document.body, c = 'className', cs = 'customize-support', rcs = new RegExp('(^|\\s+)(no-)?'+cs+'(\\s+|$)');
1665 <?php if ( $cross_domain ): ?>
1666 request = (function(){ var xhr = new XMLHttpRequest(); return ('withCredentials' in xhr); })();
1671 b[c] = b[c].replace( rcs, '' );
1672 b[c] += ( window.postMessage && request ? ' ' : ' no-' ) + cs;