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.
114 function wp_clean_themes_cache() {
115 delete_site_transient('update_themes');
116 search_theme_directories( true );
117 foreach ( wp_get_themes( array( 'errors' => null ) ) as $theme )
118 $theme->cache_delete();
122 * Whether a child theme is in use.
126 * @return bool true if a child theme is in use, false otherwise.
128 function is_child_theme() {
129 return ( TEMPLATEPATH !== STYLESHEETPATH );
133 * Retrieve name of the current stylesheet.
135 * The theme name that the administrator has currently set the front end theme
138 * For all extensive purposes, the template name and the stylesheet name are
139 * going to be the same for most cases.
142 * @uses apply_filters() Calls 'stylesheet' filter on stylesheet name.
144 * @return string Stylesheet name.
146 function get_stylesheet() {
147 return apply_filters('stylesheet', get_option('stylesheet'));
151 * Retrieve stylesheet directory path for current theme.
154 * @uses apply_filters() Calls 'stylesheet_directory' filter on stylesheet directory and theme name.
156 * @return string Path to current theme directory.
158 function get_stylesheet_directory() {
159 $stylesheet = get_stylesheet();
160 $theme_root = get_theme_root( $stylesheet );
161 $stylesheet_dir = "$theme_root/$stylesheet";
163 return apply_filters( 'stylesheet_directory', $stylesheet_dir, $stylesheet, $theme_root );
167 * Retrieve stylesheet directory URI.
173 function get_stylesheet_directory_uri() {
174 $stylesheet = get_stylesheet();
175 $theme_root_uri = get_theme_root_uri( $stylesheet );
176 $stylesheet_dir_uri = "$theme_root_uri/$stylesheet";
178 return apply_filters( 'stylesheet_directory_uri', $stylesheet_dir_uri, $stylesheet, $theme_root_uri );
182 * Retrieve URI of current theme stylesheet.
184 * The stylesheet file name is 'style.css' which is appended to {@link
185 * get_stylesheet_directory_uri() stylesheet directory URI} path.
188 * @uses apply_filters() Calls 'stylesheet_uri' filter on stylesheet URI path and stylesheet directory URI.
192 function get_stylesheet_uri() {
193 $stylesheet_dir_uri = get_stylesheet_directory_uri();
194 $stylesheet_uri = $stylesheet_dir_uri . '/style.css';
195 return apply_filters('stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri);
199 * Retrieve localized stylesheet URI.
201 * The stylesheet directory for the localized stylesheet files are located, by
202 * default, in the base theme directory. The name of the locale file will be the
203 * locale followed by '.css'. If that does not exist, then the text direction
204 * stylesheet will be checked for existence, for example 'ltr.css'.
206 * The theme may change the location of the stylesheet directory by either using
207 * the 'stylesheet_directory_uri' filter or the 'locale_stylesheet_uri' filter.
208 * If you want to change the location of the stylesheet files for the entire
209 * WordPress workflow, then change the former. If you just have the locale in a
210 * separate folder, then change the latter.
213 * @uses apply_filters() Calls 'locale_stylesheet_uri' filter on stylesheet URI path and stylesheet directory URI.
217 function get_locale_stylesheet_uri() {
219 $stylesheet_dir_uri = get_stylesheet_directory_uri();
220 $dir = get_stylesheet_directory();
221 $locale = get_locale();
222 if ( file_exists("$dir/$locale.css") )
223 $stylesheet_uri = "$stylesheet_dir_uri/$locale.css";
224 elseif ( !empty($wp_locale->text_direction) && file_exists("$dir/{$wp_locale->text_direction}.css") )
225 $stylesheet_uri = "$stylesheet_dir_uri/{$wp_locale->text_direction}.css";
227 $stylesheet_uri = '';
228 return apply_filters('locale_stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri);
232 * Retrieve name of the current theme.
235 * @uses apply_filters() Calls 'template' filter on template option.
237 * @return string Template name.
239 function get_template() {
240 return apply_filters('template', get_option('template'));
244 * Retrieve current theme directory.
247 * @uses apply_filters() Calls 'template_directory' filter on template directory path and template name.
249 * @return string Template directory path.
251 function get_template_directory() {
252 $template = get_template();
253 $theme_root = get_theme_root( $template );
254 $template_dir = "$theme_root/$template";
256 return apply_filters( 'template_directory', $template_dir, $template, $theme_root );
260 * Retrieve theme directory URI.
263 * @uses apply_filters() Calls 'template_directory_uri' filter on template directory URI path and template name.
265 * @return string Template directory URI.
267 function get_template_directory_uri() {
268 $template = get_template();
269 $theme_root_uri = get_theme_root_uri( $template );
270 $template_dir_uri = "$theme_root_uri/$template";
272 return apply_filters( 'template_directory_uri', $template_dir_uri, $template, $theme_root_uri );
276 * Retrieve theme roots.
280 * @return array|string An array of theme roots keyed by template/stylesheet or a single theme root if all themes have the same root.
282 function get_theme_roots() {
283 global $wp_theme_directories;
285 if ( count($wp_theme_directories) <= 1 )
288 $theme_roots = get_site_transient( 'theme_roots' );
289 if ( false === $theme_roots ) {
290 search_theme_directories( true ); // Regenerate the transient.
291 $theme_roots = get_site_transient( 'theme_roots' );
297 * Register a directory that contains themes.
301 * @param string $directory Either the full filesystem path to a theme folder or a folder within WP_CONTENT_DIR
304 function register_theme_directory( $directory ) {
305 global $wp_theme_directories;
307 if ( ! file_exists( $directory ) ) {
308 // Try prepending as the theme directory could be relative to the content directory
309 $directory = WP_CONTENT_DIR . '/' . $directory;
310 // If this directory does not exist, return and do not register
311 if ( ! file_exists( $directory ) )
315 $wp_theme_directories[] = $directory;
321 * Search all registered theme directories for complete and valid themes.
325 * @param bool $force Optional. Whether to force a new directory scan. Defaults to false.
326 * @return array Valid themes found
328 function search_theme_directories( $force = false ) {
329 global $wp_theme_directories;
330 if ( empty( $wp_theme_directories ) )
333 static $found_themes;
334 if ( ! $force && isset( $found_themes ) )
335 return $found_themes;
337 $found_themes = array();
339 $wp_theme_directories = (array) $wp_theme_directories;
341 // Set up maybe-relative, maybe-absolute array of theme directories.
342 // We always want to return absolute, but we need to cache relative
343 // use in for get_theme_root().
344 foreach ( $wp_theme_directories as $theme_root ) {
345 if ( 0 === strpos( $theme_root, WP_CONTENT_DIR ) )
346 $relative_theme_roots[ str_replace( WP_CONTENT_DIR, '', $theme_root ) ] = $theme_root;
348 $relative_theme_roots[ $theme_root ] = $theme_root;
351 if ( $cache_expiration = apply_filters( 'wp_cache_themes_persistently', false, 'search_theme_directories' ) ) {
352 $cached_roots = get_site_transient( 'theme_roots' );
353 if ( is_array( $cached_roots ) ) {
354 foreach ( $cached_roots as $theme_dir => $theme_root ) {
355 // A cached theme root is no longer around, so skip it.
356 if ( ! isset( $relative_theme_roots[ $theme_root ] ) )
358 $found_themes[ $theme_dir ] = array(
359 'theme_file' => $theme_dir . '/style.css',
360 'theme_root' => $relative_theme_roots[ $theme_root ], // Convert relative to absolute.
363 return $found_themes;
365 if ( ! is_int( $cache_expiration ) )
366 $cache_expiration = 1800; // half hour
368 $cache_expiration = 1800; // half hour
371 /* Loop the registered theme directories and extract all themes */
372 foreach ( $wp_theme_directories as $theme_root ) {
374 // Start with directories in the root of the current theme directory.
375 $dirs = @ scandir( $theme_root );
378 foreach ( $dirs as $dir ) {
379 if ( ! is_dir( $theme_root . '/' . $dir ) || $dir[0] == '.' || $dir == 'CVS' )
381 if ( file_exists( $theme_root . '/' . $dir . '/style.css' ) ) {
382 // wp-content/themes/a-single-theme
383 // wp-content/themes is $theme_root, a-single-theme is $dir
384 $found_themes[ $dir ] = array(
385 'theme_file' => $dir . '/style.css',
386 'theme_root' => $theme_root,
389 $found_theme = false;
390 // wp-content/themes/a-folder-of-themes/*
391 // wp-content/themes is $theme_root, a-folder-of-themes is $dir, then themes are $sub_dirs
392 $sub_dirs = @ scandir( $theme_root . '/' . $dir );
395 foreach ( $sub_dirs as $sub_dir ) {
396 if ( ! is_dir( $theme_root . '/' . $dir . '/' . $sub_dir ) || $dir[0] == '.' || $dir == 'CVS' )
398 if ( ! file_exists( $theme_root . '/' . $dir . '/' . $sub_dir . '/style.css' ) )
400 $found_themes[ $dir . '/' . $sub_dir ] = array(
401 'theme_file' => $dir . '/' . $sub_dir . '/style.css',
402 'theme_root' => $theme_root,
406 // Never mind the above, it's just a theme missing a style.css.
407 // Return it; WP_Theme will catch the error.
408 if ( ! $found_theme )
409 $found_themes[ $dir ] = array(
410 'theme_file' => $dir . '/style.css',
411 'theme_root' => $theme_root,
417 asort( $found_themes );
419 $theme_roots = array();
420 $relative_theme_roots = array_flip( $relative_theme_roots );
422 foreach ( $found_themes as $theme_dir => $theme_data ) {
423 $theme_roots[ $theme_dir ] = $relative_theme_roots[ $theme_data['theme_root'] ]; // Convert absolute to relative.
426 if ( $theme_roots != get_site_transient( 'theme_roots' ) )
427 set_site_transient( 'theme_roots', $theme_roots, $cache_expiration );
429 return $found_themes;
433 * Retrieve path to themes directory.
435 * Does not have trailing slash.
438 * @uses apply_filters() Calls 'theme_root' filter on path.
440 * @param string $stylesheet_or_template The stylesheet or template name of the theme
441 * @return string Theme path.
443 function get_theme_root( $stylesheet_or_template = false ) {
444 global $wp_theme_directories;
446 if ( $stylesheet_or_template && $theme_root = get_raw_theme_root( $stylesheet_or_template ) ) {
447 // Always prepend WP_CONTENT_DIR unless the root currently registered as a theme directory.
448 // This gives relative theme roots the benefit of the doubt when things go haywire.
449 if ( ! in_array( $theme_root, (array) $wp_theme_directories ) )
450 $theme_root = WP_CONTENT_DIR . $theme_root;
452 $theme_root = WP_CONTENT_DIR . '/themes';
455 return apply_filters( 'theme_root', $theme_root );
459 * Retrieve URI for themes directory.
461 * Does not have trailing slash.
465 * @param string $stylesheet_or_template Optional. The stylesheet or template name of the theme.
466 * Default is to leverage the main theme root.
467 * @param string $theme_root Optional. The theme root for which calculations will be based, preventing
468 * the need for a get_raw_theme_root() call.
469 * @return string Themes URI.
471 function get_theme_root_uri( $stylesheet_or_template = false, $theme_root = false ) {
472 global $wp_theme_directories;
474 if ( $stylesheet_or_template && ! $theme_root )
475 $theme_root = get_raw_theme_root( $stylesheet_or_template );
477 if ( $stylesheet_or_template && $theme_root ) {
478 if ( in_array( $theme_root, (array) $wp_theme_directories ) ) {
479 // Absolute path. Make an educated guess. YMMV -- but note the filter below.
480 if ( 0 === strpos( $theme_root, WP_CONTENT_DIR ) )
481 $theme_root_uri = content_url( str_replace( WP_CONTENT_DIR, '', $theme_root ) );
482 elseif ( 0 === strpos( $theme_root, ABSPATH ) )
483 $theme_root_uri = site_url( str_replace( ABSPATH, '', $theme_root ) );
484 elseif ( 0 === strpos( $theme_root, WP_PLUGIN_DIR ) || 0 === strpos( $theme_root, WPMU_PLUGIN_DIR ) )
485 $theme_root_uri = plugins_url( basename( $theme_root ), $theme_root );
487 $theme_root_uri = $theme_root;
489 $theme_root_uri = content_url( $theme_root );
492 $theme_root_uri = content_url( 'themes' );
495 return apply_filters( 'theme_root_uri', $theme_root_uri, get_option('siteurl'), $stylesheet_or_template );
499 * Get the raw theme root relative to the content directory with no filters applied.
503 * @param string $stylesheet_or_template The stylesheet or template name of the theme
504 * @param bool $skip_cache Optional. Whether to skip the cache. Defaults to false, meaning the cache is used.
505 * @return string Theme root
507 function get_raw_theme_root( $stylesheet_or_template, $skip_cache = false ) {
508 global $wp_theme_directories;
510 if ( count($wp_theme_directories) <= 1 )
515 // If requesting the root for the current theme, consult options to avoid calling get_theme_roots()
516 if ( ! $skip_cache ) {
517 if ( get_option('stylesheet') == $stylesheet_or_template )
518 $theme_root = get_option('stylesheet_root');
519 elseif ( get_option('template') == $stylesheet_or_template )
520 $theme_root = get_option('template_root');
523 if ( empty($theme_root) ) {
524 $theme_roots = get_theme_roots();
525 if ( !empty($theme_roots[$stylesheet_or_template]) )
526 $theme_root = $theme_roots[$stylesheet_or_template];
533 * Display localized stylesheet link element.
537 function locale_stylesheet() {
538 $stylesheet = get_locale_stylesheet_uri();
539 if ( empty($stylesheet) )
541 echo '<link rel="stylesheet" href="' . $stylesheet . '" type="text/css" media="screen" />';
545 * Start preview theme output buffer.
547 * Will only preform task if the user has permissions and template and preview
548 * query variables exist.
552 function preview_theme() {
553 if ( ! (isset($_GET['template']) && isset($_GET['preview'])) )
556 if ( !current_user_can( 'switch_themes' ) )
559 // Admin Thickbox requests
560 if ( isset( $_GET['preview_iframe'] ) )
561 show_admin_bar( false );
563 $_GET['template'] = preg_replace('|[^a-z0-9_./-]|i', '', $_GET['template']);
565 if ( validate_file($_GET['template']) )
568 add_filter( 'template', '_preview_theme_template_filter' );
570 if ( isset($_GET['stylesheet']) ) {
571 $_GET['stylesheet'] = preg_replace('|[^a-z0-9_./-]|i', '', $_GET['stylesheet']);
572 if ( validate_file($_GET['stylesheet']) )
574 add_filter( 'stylesheet', '_preview_theme_stylesheet_filter' );
577 // Prevent theme mods to current theme being used on theme being previewed
578 add_filter( 'pre_option_theme_mods_' . get_option( 'stylesheet' ), '__return_empty_array' );
580 ob_start( 'preview_theme_ob_filter' );
582 add_action('setup_theme', 'preview_theme');
585 * Private function to modify the current template when previewing a theme
592 function _preview_theme_template_filter() {
593 return isset($_GET['template']) ? $_GET['template'] : '';
597 * Private function to modify the current stylesheet when previewing a theme
604 function _preview_theme_stylesheet_filter() {
605 return isset($_GET['stylesheet']) ? $_GET['stylesheet'] : '';
609 * Callback function for ob_start() to capture all links in the theme.
614 * @param string $content
617 function preview_theme_ob_filter( $content ) {
618 return preg_replace_callback( "|(<a.*?href=([\"']))(.*?)([\"'].*?>)|", 'preview_theme_ob_filter_callback', $content );
622 * Manipulates preview theme links in order to control and maintain location.
624 * Callback function for preg_replace_callback() to accept and filter matches.
629 * @param array $matches
632 function preview_theme_ob_filter_callback( $matches ) {
633 if ( strpos($matches[4], 'onclick') !== false )
634 $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.
636 ( false !== strpos($matches[3], '/wp-admin/') )
638 ( false !== strpos( $matches[3], '://' ) && 0 !== strpos( $matches[3], home_url() ) )
640 ( false !== strpos($matches[3], '/feed/') )
642 ( false !== strpos($matches[3], '/trackback/') )
644 return $matches[1] . "#$matches[2] onclick=$matches[2]return false;" . $matches[4];
646 $link = add_query_arg( array( 'preview' => 1, 'template' => $_GET['template'], 'stylesheet' => @$_GET['stylesheet'], 'preview_iframe' => 1 ), $matches[3] );
647 if ( 0 === strpos($link, 'preview=1') )
649 return $matches[1] . esc_attr( $link ) . $matches[4];
653 * Switches the theme.
655 * Accepts one argument: $stylesheet of the theme. It also accepts an additional function signature
656 * of two arguments: $template then $stylesheet. This is for backwards compatibility.
659 * @uses do_action() Calls 'switch_theme' action, passing the new theme.
661 * @param string $stylesheet Stylesheet name
663 function switch_theme( $stylesheet ) {
664 global $wp_theme_directories, $sidebars_widgets;
666 if ( is_array( $sidebars_widgets ) )
667 set_theme_mod( 'sidebars_widgets', array( 'time' => time(), 'data' => $sidebars_widgets ) );
669 $old_theme = wp_get_theme();
670 $new_theme = wp_get_theme( $stylesheet );
672 if ( func_num_args() > 1 ) {
673 $template = $stylesheet;
674 $stylesheet = func_get_arg( 1 );
676 $template = $new_theme->get_template();
679 update_option( 'template', $template );
680 update_option( 'stylesheet', $stylesheet );
682 if ( count( $wp_theme_directories ) > 1 ) {
683 update_option( 'template_root', get_raw_theme_root( $template, true ) );
684 update_option( 'stylesheet_root', get_raw_theme_root( $stylesheet, true ) );
686 delete_option( 'template_root' );
687 delete_option( 'stylesheet_root' );
690 $new_name = $new_theme->get('Name');
692 update_option( 'current_theme', $new_name );
694 if ( is_admin() && false === get_option( 'theme_mods_' . $stylesheet ) ) {
695 $default_theme_mods = (array) get_option( 'mods_' . $new_name );
696 add_option( "theme_mods_$stylesheet", $default_theme_mods );
699 update_option( 'theme_switched', $old_theme->get_stylesheet() );
700 do_action( 'switch_theme', $new_name, $new_theme );
704 * Checks that current theme files 'index.php' and 'style.css' exists.
706 * Does not check the default theme, which is the fallback and should always exist.
707 * Will switch theme to the fallback theme if current theme does not validate.
708 * You can use the 'validate_current_theme' filter to return false to
709 * disable this functionality.
712 * @see WP_DEFAULT_THEME
716 function validate_current_theme() {
717 // Don't validate during an install/upgrade.
718 if ( defined('WP_INSTALLING') || !apply_filters( 'validate_current_theme', true ) )
721 if ( get_template() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/index.php') ) {
722 switch_theme( WP_DEFAULT_THEME );
726 if ( get_stylesheet() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/style.css') ) {
727 switch_theme( WP_DEFAULT_THEME );
731 if ( is_child_theme() && ! file_exists( get_stylesheet_directory() . '/style.css' ) ) {
732 switch_theme( WP_DEFAULT_THEME );
740 * Retrieve all theme modifications.
744 * @return array Theme modifications.
746 function get_theme_mods() {
747 $theme_slug = get_option( 'stylesheet' );
748 if ( false === ( $mods = get_option( "theme_mods_$theme_slug" ) ) ) {
749 $theme_name = get_option( 'current_theme' );
750 if ( false === $theme_name )
751 $theme_name = wp_get_theme()->get('Name');
752 $mods = get_option( "mods_$theme_name" ); // Deprecated location.
753 if ( is_admin() && false !== $mods ) {
754 update_option( "theme_mods_$theme_slug", $mods );
755 delete_option( "mods_$theme_name" );
762 * Retrieve theme modification value for the current theme.
764 * If the modification name does not exist, then the $default will be passed
765 * through {@link http://php.net/sprintf sprintf()} PHP function with the first
766 * string the template directory URI and the second string the stylesheet
770 * @uses apply_filters() Calls 'theme_mod_$name' filter on the value.
772 * @param string $name Theme modification name.
773 * @param bool|string $default
776 function get_theme_mod( $name, $default = false ) {
777 $mods = get_theme_mods();
779 if ( isset( $mods[ $name ] ) )
780 return apply_filters( "theme_mod_$name", $mods[ $name ] );
782 if ( is_string( $default ) )
783 $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
785 return apply_filters( "theme_mod_$name", $default );
789 * Update theme modification value for the current theme.
793 * @param string $name Theme modification name.
794 * @param string $value theme modification value.
796 function set_theme_mod( $name, $value ) {
797 $mods = get_theme_mods();
799 $mods[ $name ] = $value;
801 $theme = get_option( 'stylesheet' );
802 update_option( "theme_mods_$theme", $mods );
806 * Remove theme modification name from current theme list.
808 * If removing the name also removes all elements, then the entire option will
813 * @param string $name Theme modification name.
816 function remove_theme_mod( $name ) {
817 $mods = get_theme_mods();
819 if ( ! isset( $mods[ $name ] ) )
822 unset( $mods[ $name ] );
824 if ( empty( $mods ) )
825 return remove_theme_mods();
827 $theme = get_option( 'stylesheet' );
828 update_option( "theme_mods_$theme", $mods );
832 * Remove theme modifications option for current theme.
836 function remove_theme_mods() {
837 delete_option( 'theme_mods_' . get_option( 'stylesheet' ) );
840 $theme_name = get_option( 'current_theme' );
841 if ( false === $theme_name )
842 $theme_name = wp_get_theme()->get('Name');
843 delete_option( 'mods_' . $theme_name );
847 * Retrieve text color for custom header.
853 function get_header_textcolor() {
854 return get_theme_mod('header_textcolor', get_theme_support( 'custom-header', 'default-text-color' ) );
858 * Display text color for custom header.
862 function header_textcolor() {
863 echo get_header_textcolor();
867 * Whether to display the header text.
873 function display_header_text() {
874 if ( ! current_theme_supports( 'custom-header', 'header-text' ) )
877 $text_color = get_theme_mod( 'header_textcolor', get_theme_support( 'custom-header', 'default-text-color' ) );
878 return 'blank' != $text_color;
882 * Retrieve header image for custom header.
888 function get_header_image() {
889 $url = get_theme_mod( 'header_image', get_theme_support( 'custom-header', 'default-image' ) );
891 if ( 'remove-header' == $url )
894 if ( is_random_header_image() )
895 $url = get_random_header_image();
897 return esc_url_raw( set_url_scheme( $url ) );
901 * Get random header image data from registered images in theme.
907 * @return string Path to header image
910 function _get_random_header_data() {
911 static $_wp_random_header;
913 if ( empty( $_wp_random_header ) ) {
914 global $_wp_default_headers;
915 $header_image_mod = get_theme_mod( 'header_image', '' );
918 if ( 'random-uploaded-image' == $header_image_mod )
919 $headers = get_uploaded_header_images();
920 elseif ( ! empty( $_wp_default_headers ) ) {
921 if ( 'random-default-image' == $header_image_mod ) {
922 $headers = $_wp_default_headers;
924 if ( current_theme_supports( 'custom-header', 'random-default' ) )
925 $headers = $_wp_default_headers;
929 if ( empty( $headers ) )
932 $_wp_random_header = (object) $headers[ array_rand( $headers ) ];
934 $_wp_random_header->url = sprintf( $_wp_random_header->url, get_template_directory_uri(), get_stylesheet_directory_uri() );
935 $_wp_random_header->thumbnail_url = sprintf( $_wp_random_header->thumbnail_url, get_template_directory_uri(), get_stylesheet_directory_uri() );
937 return $_wp_random_header;
941 * Get random header image url from registered images in theme.
945 * @return string Path to header image
948 function get_random_header_image() {
949 $random_image = _get_random_header_data();
950 if ( empty( $random_image->url ) )
952 return $random_image->url;
956 * Check if random header image is in use.
958 * Always true if user expressly chooses the option in Appearance > Header.
959 * Also true if theme has multiple header images registered, no specific header image
960 * is chosen, and theme turns on random headers with add_theme_support().
964 * @param string $type The random pool to use. any|default|uploaded
967 function is_random_header_image( $type = 'any' ) {
968 $header_image_mod = get_theme_mod( 'header_image', get_theme_support( 'custom-header', 'default-image' ) );
970 if ( 'any' == $type ) {
971 if ( 'random-default-image' == $header_image_mod || 'random-uploaded-image' == $header_image_mod || ( '' != get_random_header_image() && empty( $header_image_mod ) ) )
974 if ( "random-$type-image" == $header_image_mod )
976 elseif ( 'default' == $type && empty( $header_image_mod ) && '' != get_random_header_image() )
984 * Display header image path.
988 function header_image() {
989 echo get_header_image();
993 * Get the header images uploaded for the current theme.
999 function get_uploaded_header_images() {
1000 $header_images = array();
1003 $headers = get_posts( array( 'post_type' => 'attachment', 'meta_key' => '_wp_attachment_is_custom_header', 'meta_value' => get_option('stylesheet'), 'orderby' => 'none', 'nopaging' => true ) );
1005 if ( empty( $headers ) )
1008 foreach ( (array) $headers as $header ) {
1009 $url = esc_url_raw( $header->guid );
1010 $header_data = wp_get_attachment_metadata( $header->ID );
1011 $header_index = basename($url);
1012 $header_images[$header_index] = array();
1013 $header_images[$header_index]['attachment_id'] = $header->ID;
1014 $header_images[$header_index]['url'] = $url;
1015 $header_images[$header_index]['thumbnail_url'] = $url;
1016 $header_images[$header_index]['width'] = $header_data['width'];
1017 $header_images[$header_index]['height'] = $header_data['height'];
1020 return $header_images;
1024 * Get the header image data.
1030 function get_custom_header() {
1031 global $_wp_default_headers;
1033 if ( is_random_header_image() ) {
1034 $data = _get_random_header_data();
1036 $data = get_theme_mod( 'header_image_data' );
1037 if ( ! $data && current_theme_supports( 'custom-header', 'default-image' ) ) {
1038 $directory_args = array( get_template_directory_uri(), get_stylesheet_directory_uri() );
1040 $data['url'] = $data['thumbnail_url'] = vsprintf( get_theme_support( 'custom-header', 'default-image' ), $directory_args );
1041 if ( ! empty( $_wp_default_headers ) ) {
1042 foreach ( (array) $_wp_default_headers as $default_header ) {
1043 $url = vsprintf( $default_header['url'], $directory_args );
1044 if ( $data['url'] == $url ) {
1045 $data = $default_header;
1046 $data['url'] = $url;
1047 $data['thumbnail_url'] = vsprintf( $data['thumbnail_url'], $directory_args );
1057 'thumbnail_url' => '',
1058 'width' => get_theme_support( 'custom-header', 'width' ),
1059 'height' => get_theme_support( 'custom-header', 'height' ),
1061 return (object) wp_parse_args( $data, $default );
1065 * Register a selection of default headers to be displayed by the custom header admin UI.
1069 * @param array $headers Array of headers keyed by a string id. The ids point to arrays containing 'url', 'thumbnail_url', and 'description' keys.
1071 function register_default_headers( $headers ) {
1072 global $_wp_default_headers;
1074 $_wp_default_headers = array_merge( (array) $_wp_default_headers, (array) $headers );
1078 * Unregister default headers.
1080 * This function must be called after register_default_headers() has already added the
1081 * header you want to remove.
1083 * @see register_default_headers()
1086 * @param string|array $header The header string id (key of array) to remove, or an array thereof.
1087 * @return True on success, false on failure.
1089 function unregister_default_headers( $header ) {
1090 global $_wp_default_headers;
1091 if ( is_array( $header ) ) {
1092 array_map( 'unregister_default_headers', $header );
1093 } elseif ( isset( $_wp_default_headers[ $header ] ) ) {
1094 unset( $_wp_default_headers[ $header ] );
1102 * Retrieve background image for custom background.
1108 function get_background_image() {
1109 return get_theme_mod('background_image', get_theme_support( 'custom-background', 'default-image' ) );
1113 * Display background image path.
1117 function background_image() {
1118 echo get_background_image();
1122 * Retrieve value for custom background color.
1128 function get_background_color() {
1129 return get_theme_mod('background_color', get_theme_support( 'custom-background', 'default-color' ) );
1133 * Display background color value.
1137 function background_color() {
1138 echo get_background_color();
1142 * Default custom background callback.
1147 function _custom_background_cb() {
1148 // $background is the saved custom image, or the default image.
1149 $background = set_url_scheme( get_background_image() );
1151 // $color is the saved custom color.
1152 // A default has to be specified in style.css. It will not be printed here.
1153 $color = get_theme_mod( 'background_color' );
1155 if ( ! $background && ! $color )
1158 $style = $color ? "background-color: #$color;" : '';
1160 if ( $background ) {
1161 $image = " background-image: url('$background');";
1163 $repeat = get_theme_mod( 'background_repeat', 'repeat' );
1164 if ( ! in_array( $repeat, array( 'no-repeat', 'repeat-x', 'repeat-y', 'repeat' ) ) )
1166 $repeat = " background-repeat: $repeat;";
1168 $position = get_theme_mod( 'background_position_x', 'left' );
1169 if ( ! in_array( $position, array( 'center', 'right', 'left' ) ) )
1171 $position = " background-position: top $position;";
1173 $attachment = get_theme_mod( 'background_attachment', 'scroll' );
1174 if ( ! in_array( $attachment, array( 'fixed', 'scroll' ) ) )
1175 $attachment = 'scroll';
1176 $attachment = " background-attachment: $attachment;";
1178 $style .= $image . $repeat . $position . $attachment;
1181 <style type="text/css" id="custom-background-css">
1182 body.custom-background { <?php echo trim( $style ); ?> }
1188 * Add callback for custom TinyMCE editor stylesheets.
1190 * The parameter $stylesheet is the name of the stylesheet, relative to
1191 * the theme root. It also accepts an array of stylesheets.
1192 * It is optional and defaults to 'editor-style.css'.
1194 * This function automatically adds another stylesheet with -rtl prefix, e.g. editor-style-rtl.css.
1195 * If that file doesn't exist, it is removed before adding the stylesheet(s) to TinyMCE.
1196 * If an array of stylesheets is passed to add_editor_style(),
1197 * RTL is only added for the first stylesheet.
1199 * Since version 3.4 the TinyMCE body has .rtl CSS class.
1200 * It is a better option to use that class and add any RTL styles to the main stylesheet.
1204 * @param mixed $stylesheet Optional. Stylesheet name or array thereof, relative to theme root.
1205 * Defaults to 'editor-style.css'
1207 function add_editor_style( $stylesheet = 'editor-style.css' ) {
1209 add_theme_support( 'editor-style' );
1214 global $editor_styles;
1215 $editor_styles = (array) $editor_styles;
1216 $stylesheet = (array) $stylesheet;
1218 $rtl_stylesheet = str_replace('.css', '-rtl.css', $stylesheet[0]);
1219 $stylesheet[] = $rtl_stylesheet;
1222 $editor_styles = array_merge( $editor_styles, $stylesheet );
1226 * Removes all visual editor stylesheets.
1230 * @return bool True on success, false if there were no stylesheets to remove.
1232 function remove_editor_styles() {
1233 if ( ! current_theme_supports( 'editor-style' ) )
1235 _remove_theme_support( 'editor-style' );
1237 $GLOBALS['editor_styles'] = array();
1242 * Allows a theme to register its support of a certain feature
1244 * Must be called in the theme's functions.php file to work.
1245 * If attached to a hook, it must be after_setup_theme.
1246 * The init hook may be too late for some features.
1249 * @param string $feature the feature being added
1251 function add_theme_support( $feature ) {
1252 global $_wp_theme_features;
1254 if ( func_num_args() == 1 )
1257 $args = array_slice( func_get_args(), 1 );
1259 switch ( $feature ) {
1260 case 'post-formats' :
1261 if ( is_array( $args[0] ) )
1262 $args[0] = array_intersect( $args[0], array_keys( get_post_format_slugs() ) );
1265 case 'custom-header-uploads' :
1266 return add_theme_support( 'custom-header', array( 'uploads' => true ) );
1269 case 'custom-header' :
1270 if ( ! is_array( $args ) )
1271 $args = array( 0 => array() );
1274 'default-image' => '',
1275 'random-default' => false,
1278 'flex-height' => false,
1279 'flex-width' => false,
1280 'default-text-color' => '',
1281 'header-text' => true,
1283 'wp-head-callback' => '',
1284 'admin-head-callback' => '',
1285 'admin-preview-callback' => '',
1288 $jit = isset( $args[0]['__jit'] );
1289 unset( $args[0]['__jit'] );
1291 // Merge in data from previous add_theme_support() calls.
1292 // The first value registered wins. (A child theme is set up first.)
1293 if ( isset( $_wp_theme_features['custom-header'] ) )
1294 $args[0] = wp_parse_args( $_wp_theme_features['custom-header'][0], $args[0] );
1296 // Load in the defaults at the end, as we need to insure first one wins.
1297 // This will cause all constants to be defined, as each arg will then be set to the default.
1299 $args[0] = wp_parse_args( $args[0], $defaults );
1301 // If a constant was defined, use that value. Otherwise, define the constant to ensure
1302 // the constant is always accurate (and is not defined later, overriding our value).
1303 // As stated above, the first value wins.
1304 // Once we get to wp_loaded (just-in-time), define any constants we haven't already.
1305 // Constants are lame. Don't reference them. This is just for backwards compatibility.
1307 if ( defined( 'NO_HEADER_TEXT' ) )
1308 $args[0]['header-text'] = ! NO_HEADER_TEXT;
1309 elseif ( isset( $args[0]['header-text'] ) )
1310 define( 'NO_HEADER_TEXT', empty( $args[0]['header-text'] ) );
1312 if ( defined( 'HEADER_IMAGE_WIDTH' ) )
1313 $args[0]['width'] = (int) HEADER_IMAGE_WIDTH;
1314 elseif ( isset( $args[0]['width'] ) )
1315 define( 'HEADER_IMAGE_WIDTH', (int) $args[0]['width'] );
1317 if ( defined( 'HEADER_IMAGE_HEIGHT' ) )
1318 $args[0]['height'] = (int) HEADER_IMAGE_HEIGHT;
1319 elseif ( isset( $args[0]['height'] ) )
1320 define( 'HEADER_IMAGE_HEIGHT', (int) $args[0]['height'] );
1322 if ( defined( 'HEADER_TEXTCOLOR' ) )
1323 $args[0]['default-text-color'] = HEADER_TEXTCOLOR;
1324 elseif ( isset( $args[0]['default-text-color'] ) )
1325 define( 'HEADER_TEXTCOLOR', $args[0]['default-text-color'] );
1327 if ( defined( 'HEADER_IMAGE' ) )
1328 $args[0]['default-image'] = HEADER_IMAGE;
1329 elseif ( isset( $args[0]['default-image'] ) )
1330 define( 'HEADER_IMAGE', $args[0]['default-image'] );
1332 if ( $jit && ! empty( $args[0]['default-image'] ) )
1333 $args[0]['random-default'] = false;
1335 // If headers are supported, and we still don't have a defined width or height,
1336 // we have implicit flex sizes.
1338 if ( empty( $args[0]['width'] ) && empty( $args[0]['flex-width'] ) )
1339 $args[0]['flex-width'] = true;
1340 if ( empty( $args[0]['height'] ) && empty( $args[0]['flex-height'] ) )
1341 $args[0]['flex-height'] = true;
1346 case 'custom-background' :
1347 if ( ! is_array( $args ) )
1348 $args = array( 0 => array() );
1351 'default-image' => '',
1352 'default-color' => '',
1353 'wp-head-callback' => '_custom_background_cb',
1354 'admin-head-callback' => '',
1355 'admin-preview-callback' => '',
1358 $jit = isset( $args[0]['__jit'] );
1359 unset( $args[0]['__jit'] );
1361 // Merge in data from previous add_theme_support() calls. The first value registered wins.
1362 if ( isset( $_wp_theme_features['custom-background'] ) )
1363 $args[0] = wp_parse_args( $_wp_theme_features['custom-background'][0], $args[0] );
1366 $args[0] = wp_parse_args( $args[0], $defaults );
1368 if ( defined( 'BACKGROUND_COLOR' ) )
1369 $args[0]['default-color'] = BACKGROUND_COLOR;
1370 elseif ( isset( $args[0]['default-color'] ) || $jit )
1371 define( 'BACKGROUND_COLOR', $args[0]['default-color'] );
1373 if ( defined( 'BACKGROUND_IMAGE' ) )
1374 $args[0]['default-image'] = BACKGROUND_IMAGE;
1375 elseif ( isset( $args[0]['default-image'] ) || $jit )
1376 define( 'BACKGROUND_IMAGE', $args[0]['default-image'] );
1381 $_wp_theme_features[ $feature ] = $args;
1385 * Registers the internal custom header and background routines.
1390 function _custom_header_background_just_in_time() {
1391 global $custom_image_header, $custom_background;
1393 if ( current_theme_supports( 'custom-header' ) ) {
1394 // In case any constants were defined after an add_custom_image_header() call, re-run.
1395 add_theme_support( 'custom-header', array( '__jit' => true ) );
1397 $args = get_theme_support( 'custom-header' );
1398 if ( $args[0]['wp-head-callback'] )
1399 add_action( 'wp_head', $args[0]['wp-head-callback'] );
1402 require_once( ABSPATH . 'wp-admin/custom-header.php' );
1403 $custom_image_header = new Custom_Image_Header( $args[0]['admin-head-callback'], $args[0]['admin-preview-callback'] );
1407 if ( current_theme_supports( 'custom-background' ) ) {
1408 // In case any constants were defined after an add_custom_background() call, re-run.
1409 add_theme_support( 'custom-background', array( '__jit' => true ) );
1411 $args = get_theme_support( 'custom-background' );
1412 add_action( 'wp_head', $args[0]['wp-head-callback'] );
1415 require_once( ABSPATH . 'wp-admin/custom-background.php' );
1416 $custom_background = new Custom_Background( $args[0]['admin-head-callback'], $args[0]['admin-preview-callback'] );
1420 add_action( 'wp_loaded', '_custom_header_background_just_in_time' );
1423 * Gets the theme support arguments passed when registering that support
1426 * @param string $feature the feature to check
1427 * @return array The array of extra arguments
1429 function get_theme_support( $feature ) {
1430 global $_wp_theme_features;
1431 if ( ! isset( $_wp_theme_features[ $feature ] ) )
1434 if ( func_num_args() <= 1 )
1435 return $_wp_theme_features[ $feature ];
1437 $args = array_slice( func_get_args(), 1 );
1438 switch ( $feature ) {
1439 case 'custom-header' :
1440 case 'custom-background' :
1441 if ( isset( $_wp_theme_features[ $feature ][0][ $args[0] ] ) )
1442 return $_wp_theme_features[ $feature ][0][ $args[0] ];
1446 return $_wp_theme_features[ $feature ];
1452 * Allows a theme to de-register its support of a certain feature
1454 * Should be called in the theme's functions.php file. Generally would
1455 * be used for child themes to override support from the parent theme.
1458 * @see add_theme_support()
1459 * @param string $feature the feature being added
1460 * @return bool Whether feature was removed.
1462 function remove_theme_support( $feature ) {
1463 // Blacklist: for internal registrations not used directly by themes.
1464 if ( in_array( $feature, array( 'editor-style', 'widgets', 'menus' ) ) )
1467 return _remove_theme_support( $feature );
1471 * Do not use. Removes theme support internally, ignorant of the blacklist.
1476 function _remove_theme_support( $feature ) {
1477 global $_wp_theme_features;
1479 switch ( $feature ) {
1480 case 'custom-header-uploads' :
1481 if ( ! isset( $_wp_theme_features['custom-header'] ) )
1483 add_theme_support( 'custom-header', array( 'uploads' => false ) );
1484 return; // Do not continue - custom-header-uploads no longer exists.
1487 if ( ! isset( $_wp_theme_features[ $feature ] ) )
1490 switch ( $feature ) {
1491 case 'custom-header' :
1492 if ( false === did_action( 'wp_loaded', '_custom_header_background_just_in_time' ) )
1494 $support = get_theme_support( 'custom-header' );
1495 if ( $support[0]['wp-head-callback'] )
1496 remove_action( 'wp_head', $support[0]['wp-head-callback'] );
1497 remove_action( 'admin_menu', array( $GLOBALS['custom_image_header'], 'init' ) );
1498 unset( $GLOBALS['custom_image_header'] );
1501 case 'custom-background' :
1502 if ( false === did_action( 'wp_loaded', '_custom_header_background_just_in_time' ) )
1504 $support = get_theme_support( 'custom-background' );
1505 remove_action( 'wp_head', $support[0]['wp-head-callback'] );
1506 remove_action( 'admin_menu', array( $GLOBALS['custom_background'], 'init' ) );
1507 unset( $GLOBALS['custom_background'] );
1511 unset( $_wp_theme_features[ $feature ] );
1516 * Checks a theme's support for a given feature
1519 * @param string $feature the feature being checked
1522 function current_theme_supports( $feature ) {
1523 global $_wp_theme_features;
1525 if ( 'custom-header-uploads' == $feature )
1526 return current_theme_supports( 'custom-header', 'uploads' );
1528 if ( !isset( $_wp_theme_features[$feature] ) )
1531 // If no args passed then no extra checks need be performed
1532 if ( func_num_args() <= 1 )
1535 $args = array_slice( func_get_args(), 1 );
1537 switch ( $feature ) {
1538 case 'post-thumbnails':
1539 // post-thumbnails can be registered for only certain content/post types by passing
1540 // an array of types to add_theme_support(). If no array was passed, then
1541 // any type is accepted
1542 if ( true === $_wp_theme_features[$feature] ) // Registered for all types
1544 $content_type = $args[0];
1545 return in_array( $content_type, $_wp_theme_features[$feature][0] );
1548 case 'post-formats':
1549 // specific post formats can be registered by passing an array of types to
1550 // add_theme_support()
1551 $post_format = $args[0];
1552 return in_array( $post_format, $_wp_theme_features[$feature][0] );
1555 case 'custom-header':
1556 case 'custom-background' :
1557 // specific custom header and background capabilities can be registered by passing
1558 // an array to add_theme_support()
1559 $header_support = $args[0];
1560 return ( isset( $_wp_theme_features[$feature][0][$header_support] ) && $_wp_theme_features[$feature][0][$header_support] );
1564 return apply_filters('current_theme_supports-' . $feature, true, $args, $_wp_theme_features[$feature]);
1568 * Checks a theme's support for a given feature before loading the functions which implement it.
1571 * @param string $feature the feature being checked
1572 * @param string $include the file containing the functions that implement the feature
1574 function require_if_theme_supports( $feature, $include) {
1575 if ( current_theme_supports( $feature ) )
1576 require ( $include );
1580 * Checks an attachment being deleted to see if it's a header or background image.
1582 * If true it removes the theme modification which would be pointing at the deleted
1587 * @param int $id the attachment id
1589 function _delete_attachment_theme_mod( $id ) {
1590 $attachment_image = wp_get_attachment_url( $id );
1591 $header_image = get_header_image();
1592 $background_image = get_background_image();
1594 if ( $header_image && $header_image == $attachment_image )
1595 remove_theme_mod( 'header_image' );
1597 if ( $background_image && $background_image == $attachment_image )
1598 remove_theme_mod( 'background_image' );
1601 add_action( 'delete_attachment', '_delete_attachment_theme_mod' );
1604 * Checks if a theme has been changed and runs 'after_switch_theme' hook on the next WP load
1608 function check_theme_switched() {
1609 if ( $stylesheet = get_option( 'theme_switched' ) ) {
1610 $old_theme = wp_get_theme( $stylesheet );
1612 if ( $old_theme->exists() )
1613 do_action( 'after_switch_theme', $old_theme->get('Name'), $old_theme );
1615 do_action( 'after_switch_theme', $stylesheet );
1617 update_option( 'theme_switched', false );
1622 * Includes and instantiates the WP_Customize_Manager class.
1624 * Fires when ?wp_customize=on or on wp-admin/customize.php.
1628 function _wp_customize_include() {
1629 if ( ! ( ( isset( $_REQUEST['wp_customize'] ) && 'on' == $_REQUEST['wp_customize'] )
1630 || ( is_admin() && 'customize.php' == basename( $_SERVER['PHP_SELF'] ) )
1634 require( ABSPATH . WPINC . '/class-wp-customize-manager.php' );
1635 // Init Customize class
1636 $GLOBALS['wp_customize'] = new WP_Customize_Manager;
1638 add_action( 'plugins_loaded', '_wp_customize_include' );
1641 * Adds settings for the customize-loader script.
1645 function _wp_customize_loader_settings() {
1648 $admin_origin = parse_url( admin_url() );
1649 $home_origin = parse_url( home_url() );
1650 $cross_domain = ( strtolower( $admin_origin[ 'host' ] ) != strtolower( $home_origin[ 'host' ] ) );
1653 'mobile' => wp_is_mobile(),
1654 'ios' => wp_is_mobile() && preg_match( '/iPad|iPod|iPhone/', $_SERVER['HTTP_USER_AGENT'] ),
1658 'url' => esc_url( admin_url( 'customize.php' ) ),
1659 'isCrossDomain' => $cross_domain,
1660 'browser' => $browser,
1663 $script = 'var _wpCustomizeLoaderSettings = ' . json_encode( $settings ) . ';';
1665 $data = $wp_scripts->get_data( 'customize-loader', 'data' );
1667 $script = "$data\n$script";
1669 $wp_scripts->add_data( 'customize-loader', 'data', $script );
1671 add_action( 'admin_enqueue_scripts', '_wp_customize_loader_settings' );
1674 * Returns a URL to load the theme customizer.
1678 * @param string $stylesheet Optional. Theme to customize. Defaults to current theme.
1679 * The theme's stylesheet will be urlencoded if necessary.
1681 function wp_customize_url( $stylesheet = null ) {
1682 $url = admin_url( 'customize.php' );
1684 $url .= '?theme=' . urlencode( $stylesheet );
1685 return esc_url( $url );
1689 * Prints a script to check whether or not the customizer is supported,
1690 * and apply either the no-customize-support or customize-support class
1693 * This function MUST be called inside the body tag.
1695 * Ideally, call this function immediately after the body tag is opened.
1696 * This prevents a flash of unstyled content.
1698 * It is also recommended that you add the "no-customize-support" class
1699 * to the body tag by default.
1703 function wp_customize_support_script() {
1704 $admin_origin = parse_url( admin_url() );
1705 $home_origin = parse_url( home_url() );
1706 $cross_domain = ( strtolower( $admin_origin[ 'host' ] ) != strtolower( $home_origin[ 'host' ] ) );
1709 <script type="text/javascript">
1711 var request, b = document.body, c = 'className', cs = 'customize-support', rcs = new RegExp('(^|\\s+)(no-)?'+cs+'(\\s+|$)');
1713 <?php if ( $cross_domain ): ?>
1714 request = (function(){ var xhr = new XMLHttpRequest(); return ('withCredentials' in xhr); })();
1719 b[c] = b[c].replace( rcs, ' ' );
1720 b[c] += ( window.postMessage && request ? ' ' : ' no-' ) + cs;