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 * @global array $wp_theme_directories
18 * @staticvar array $_themes
20 * @param array $args The search arguments. Optional.
21 * - errors mixed True to return themes with errors, false to return themes without errors, null
22 * to return all themes. Defaults to false.
23 * - allowed mixed (Multisite) True to return only allowed themes for a site. False to return only
24 * disallowed themes for a site. 'site' to return only site-allowed themes. 'network'
25 * to return only network-allowed themes. Null to return all themes. Defaults to null.
26 * - blog_id int (Multisite) The blog ID used to calculate which themes are allowed. Defaults to 0,
27 * synonymous for the current blog.
28 * @return array Array of WP_Theme objects.
30 function wp_get_themes( $args = array() ) {
31 global $wp_theme_directories;
33 $defaults = array( 'errors' => false, 'allowed' => null, 'blog_id' => 0 );
34 $args = wp_parse_args( $args, $defaults );
36 $theme_directories = search_theme_directories();
38 if ( count( $wp_theme_directories ) > 1 ) {
39 // Make sure the current theme wins out, in case search_theme_directories() picks the wrong
40 // one in the case of a conflict. (Normally, last registered theme root wins.)
41 $current_theme = get_stylesheet();
42 if ( isset( $theme_directories[ $current_theme ] ) ) {
43 $root_of_current_theme = get_raw_theme_root( $current_theme );
44 if ( ! in_array( $root_of_current_theme, $wp_theme_directories ) )
45 $root_of_current_theme = WP_CONTENT_DIR . $root_of_current_theme;
46 $theme_directories[ $current_theme ]['theme_root'] = $root_of_current_theme;
50 if ( empty( $theme_directories ) )
53 if ( is_multisite() && null !== $args['allowed'] ) {
54 $allowed = $args['allowed'];
55 if ( 'network' === $allowed )
56 $theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed_on_network() );
57 elseif ( 'site' === $allowed )
58 $theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed_on_site( $args['blog_id'] ) );
60 $theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed( $args['blog_id'] ) );
62 $theme_directories = array_diff_key( $theme_directories, WP_Theme::get_allowed( $args['blog_id'] ) );
66 static $_themes = array();
68 foreach ( $theme_directories as $theme => $theme_root ) {
69 if ( isset( $_themes[ $theme_root['theme_root'] . '/' . $theme ] ) )
70 $themes[ $theme ] = $_themes[ $theme_root['theme_root'] . '/' . $theme ];
72 $themes[ $theme ] = $_themes[ $theme_root['theme_root'] . '/' . $theme ] = new WP_Theme( $theme, $theme_root['theme_root'] );
75 if ( null !== $args['errors'] ) {
76 foreach ( $themes as $theme => $wp_theme ) {
77 if ( $wp_theme->errors() != $args['errors'] )
78 unset( $themes[ $theme ] );
86 * Gets a WP_Theme object for a theme.
90 * @global array $wp_theme_directories
92 * @param string $stylesheet Directory name for the theme. Optional. Defaults to current theme.
93 * @param string $theme_root Absolute path of the theme root to look in. Optional. If not specified, get_raw_theme_root()
94 * is used to calculate the theme root for the $stylesheet provided (or current theme).
95 * @return WP_Theme Theme object. Be sure to check the object's exists() method if you need to confirm the theme's existence.
97 function wp_get_theme( $stylesheet = null, $theme_root = null ) {
98 global $wp_theme_directories;
100 if ( empty( $stylesheet ) )
101 $stylesheet = get_stylesheet();
103 if ( empty( $theme_root ) ) {
104 $theme_root = get_raw_theme_root( $stylesheet );
105 if ( false === $theme_root )
106 $theme_root = WP_CONTENT_DIR . '/themes';
107 elseif ( ! in_array( $theme_root, (array) $wp_theme_directories ) )
108 $theme_root = WP_CONTENT_DIR . $theme_root;
111 return new WP_Theme( $stylesheet, $theme_root );
115 * Clears the cache held by get_theme_roots() and WP_Theme.
118 * @param bool $clear_update_cache Whether to clear the Theme updates cache
120 function wp_clean_themes_cache( $clear_update_cache = true ) {
121 if ( $clear_update_cache )
122 delete_site_transient( 'update_themes' );
123 search_theme_directories( true );
124 foreach ( wp_get_themes( array( 'errors' => null ) ) as $theme )
125 $theme->cache_delete();
129 * Whether a child theme is in use.
133 * @return bool true if a child theme is in use, false otherwise.
135 function is_child_theme() {
136 return ( TEMPLATEPATH !== STYLESHEETPATH );
140 * Retrieve name of the current stylesheet.
142 * The theme name that the administrator has currently set the front end theme
145 * For all intents and purposes, the template name and the stylesheet name are
146 * going to be the same for most cases.
150 * @return string Stylesheet name.
152 function get_stylesheet() {
154 * Filter the name of current stylesheet.
158 * @param string $stylesheet Name of the current stylesheet.
160 return apply_filters( 'stylesheet', get_option( 'stylesheet' ) );
164 * Retrieve stylesheet directory path for current theme.
168 * @return string Path to current theme directory.
170 function get_stylesheet_directory() {
171 $stylesheet = get_stylesheet();
172 $theme_root = get_theme_root( $stylesheet );
173 $stylesheet_dir = "$theme_root/$stylesheet";
176 * Filter the stylesheet directory path for current theme.
180 * @param string $stylesheet_dir Absolute path to the current them.
181 * @param string $stylesheet Directory name of the current theme.
182 * @param string $theme_root Absolute path to themes directory.
184 return apply_filters( 'stylesheet_directory', $stylesheet_dir, $stylesheet, $theme_root );
188 * Retrieve stylesheet directory URI.
194 function get_stylesheet_directory_uri() {
195 $stylesheet = str_replace( '%2F', '/', rawurlencode( get_stylesheet() ) );
196 $theme_root_uri = get_theme_root_uri( $stylesheet );
197 $stylesheet_dir_uri = "$theme_root_uri/$stylesheet";
200 * Filter the stylesheet directory URI.
204 * @param string $stylesheet_dir_uri Stylesheet directory URI.
205 * @param string $stylesheet Name of the activated theme's directory.
206 * @param string $theme_root_uri Themes root URI.
208 return apply_filters( 'stylesheet_directory_uri', $stylesheet_dir_uri, $stylesheet, $theme_root_uri );
212 * Retrieve URI of current theme stylesheet.
214 * The stylesheet file name is 'style.css' which is appended to {@link
215 * get_stylesheet_directory_uri() stylesheet directory URI} path.
221 function get_stylesheet_uri() {
222 $stylesheet_dir_uri = get_stylesheet_directory_uri();
223 $stylesheet_uri = $stylesheet_dir_uri . '/style.css';
225 * Filter the URI of the current theme stylesheet.
229 * @param string $stylesheet_uri Stylesheet URI for the current theme/child theme.
230 * @param string $stylesheet_dir_uri Stylesheet directory URI for the current theme/child theme.
232 return apply_filters( 'stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri );
236 * Retrieve localized stylesheet URI.
238 * The stylesheet directory for the localized stylesheet files are located, by
239 * default, in the base theme directory. The name of the locale file will be the
240 * locale followed by '.css'. If that does not exist, then the text direction
241 * stylesheet will be checked for existence, for example 'ltr.css'.
243 * The theme may change the location of the stylesheet directory by either using
244 * the 'stylesheet_directory_uri' filter or the 'locale_stylesheet_uri' filter.
245 * If you want to change the location of the stylesheet files for the entire
246 * WordPress workflow, then change the former. If you just have the locale in a
247 * separate folder, then change the latter.
251 * @global WP_Locale $wp_locale
255 function get_locale_stylesheet_uri() {
257 $stylesheet_dir_uri = get_stylesheet_directory_uri();
258 $dir = get_stylesheet_directory();
259 $locale = get_locale();
260 if ( file_exists("$dir/$locale.css") )
261 $stylesheet_uri = "$stylesheet_dir_uri/$locale.css";
262 elseif ( !empty($wp_locale->text_direction) && file_exists("$dir/{$wp_locale->text_direction}.css") )
263 $stylesheet_uri = "$stylesheet_dir_uri/{$wp_locale->text_direction}.css";
265 $stylesheet_uri = '';
267 * Filter the localized stylesheet URI.
271 * @param string $stylesheet_uri Localized stylesheet URI.
272 * @param string $stylesheet_dir_uri Stylesheet directory URI.
274 return apply_filters( 'locale_stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri );
278 * Retrieve name of the current theme.
282 * @return string Template name.
284 function get_template() {
286 * Filter the name of the current theme.
290 * @param string $template Current theme's directory name.
292 return apply_filters( 'template', get_option( 'template' ) );
296 * Retrieve current theme directory.
300 * @return string Template directory path.
302 function get_template_directory() {
303 $template = get_template();
304 $theme_root = get_theme_root( $template );
305 $template_dir = "$theme_root/$template";
308 * Filter the current theme directory path.
312 * @param string $template_dir The URI of the current theme directory.
313 * @param string $template Directory name of the current theme.
314 * @param string $theme_root Absolute path to the themes directory.
316 return apply_filters( 'template_directory', $template_dir, $template, $theme_root );
320 * Retrieve theme directory URI.
324 * @return string Template directory URI.
326 function get_template_directory_uri() {
327 $template = str_replace( '%2F', '/', rawurlencode( get_template() ) );
328 $theme_root_uri = get_theme_root_uri( $template );
329 $template_dir_uri = "$theme_root_uri/$template";
332 * Filter the current theme directory URI.
336 * @param string $template_dir_uri The URI of the current theme directory.
337 * @param string $template Directory name of the current theme.
338 * @param string $theme_root_uri The themes root URI.
340 return apply_filters( 'template_directory_uri', $template_dir_uri, $template, $theme_root_uri );
344 * Retrieve theme roots.
348 * @global array $wp_theme_directories
350 * @return array|string An array of theme roots keyed by template/stylesheet or a single theme root if all themes have the same root.
352 function get_theme_roots() {
353 global $wp_theme_directories;
355 if ( count($wp_theme_directories) <= 1 )
358 $theme_roots = get_site_transient( 'theme_roots' );
359 if ( false === $theme_roots ) {
360 search_theme_directories( true ); // Regenerate the transient.
361 $theme_roots = get_site_transient( 'theme_roots' );
367 * Register a directory that contains themes.
371 * @global array $wp_theme_directories
373 * @param string $directory Either the full filesystem path to a theme folder or a folder within WP_CONTENT_DIR
376 function register_theme_directory( $directory ) {
377 global $wp_theme_directories;
379 if ( ! file_exists( $directory ) ) {
380 // Try prepending as the theme directory could be relative to the content directory
381 $directory = WP_CONTENT_DIR . '/' . $directory;
382 // If this directory does not exist, return and do not register
383 if ( ! file_exists( $directory ) ) {
388 if ( ! is_array( $wp_theme_directories ) ) {
389 $wp_theme_directories = array();
392 $untrailed = untrailingslashit( $directory );
393 if ( ! empty( $untrailed ) && ! in_array( $untrailed, $wp_theme_directories ) ) {
394 $wp_theme_directories[] = $untrailed;
401 * Search all registered theme directories for complete and valid themes.
405 * @global array $wp_theme_directories
406 * @staticvar array $found_themes
408 * @param bool $force Optional. Whether to force a new directory scan. Defaults to false.
409 * @return array|false Valid themes found
411 function search_theme_directories( $force = false ) {
412 global $wp_theme_directories;
413 static $found_themes = null;
415 if ( empty( $wp_theme_directories ) )
418 if ( ! $force && isset( $found_themes ) )
419 return $found_themes;
421 $found_themes = array();
423 $wp_theme_directories = (array) $wp_theme_directories;
424 $relative_theme_roots = array();
426 // Set up maybe-relative, maybe-absolute array of theme directories.
427 // We always want to return absolute, but we need to cache relative
428 // to use in get_theme_root().
429 foreach ( $wp_theme_directories as $theme_root ) {
430 if ( 0 === strpos( $theme_root, WP_CONTENT_DIR ) )
431 $relative_theme_roots[ str_replace( WP_CONTENT_DIR, '', $theme_root ) ] = $theme_root;
433 $relative_theme_roots[ $theme_root ] = $theme_root;
437 * Filter whether to get the cache of the registered theme directories.
441 * @param bool $cache_expiration Whether to get the cache of the theme directories. Default false.
442 * @param string $cache_directory Directory to be searched for the cache.
444 if ( $cache_expiration = apply_filters( 'wp_cache_themes_persistently', false, 'search_theme_directories' ) ) {
445 $cached_roots = get_site_transient( 'theme_roots' );
446 if ( is_array( $cached_roots ) ) {
447 foreach ( $cached_roots as $theme_dir => $theme_root ) {
448 // A cached theme root is no longer around, so skip it.
449 if ( ! isset( $relative_theme_roots[ $theme_root ] ) )
451 $found_themes[ $theme_dir ] = array(
452 'theme_file' => $theme_dir . '/style.css',
453 'theme_root' => $relative_theme_roots[ $theme_root ], // Convert relative to absolute.
456 return $found_themes;
458 if ( ! is_int( $cache_expiration ) )
459 $cache_expiration = 1800; // half hour
461 $cache_expiration = 1800; // half hour
464 /* Loop the registered theme directories and extract all themes */
465 foreach ( $wp_theme_directories as $theme_root ) {
467 // Start with directories in the root of the current theme directory.
468 $dirs = @ scandir( $theme_root );
470 trigger_error( "$theme_root is not readable", E_USER_NOTICE );
473 foreach ( $dirs as $dir ) {
474 if ( ! is_dir( $theme_root . '/' . $dir ) || $dir[0] == '.' || $dir == 'CVS' )
476 if ( file_exists( $theme_root . '/' . $dir . '/style.css' ) ) {
477 // wp-content/themes/a-single-theme
478 // wp-content/themes is $theme_root, a-single-theme is $dir
479 $found_themes[ $dir ] = array(
480 'theme_file' => $dir . '/style.css',
481 'theme_root' => $theme_root,
484 $found_theme = false;
485 // wp-content/themes/a-folder-of-themes/*
486 // wp-content/themes is $theme_root, a-folder-of-themes is $dir, then themes are $sub_dirs
487 $sub_dirs = @ scandir( $theme_root . '/' . $dir );
489 trigger_error( "$theme_root/$dir is not readable", E_USER_NOTICE );
492 foreach ( $sub_dirs as $sub_dir ) {
493 if ( ! is_dir( $theme_root . '/' . $dir . '/' . $sub_dir ) || $dir[0] == '.' || $dir == 'CVS' )
495 if ( ! file_exists( $theme_root . '/' . $dir . '/' . $sub_dir . '/style.css' ) )
497 $found_themes[ $dir . '/' . $sub_dir ] = array(
498 'theme_file' => $dir . '/' . $sub_dir . '/style.css',
499 'theme_root' => $theme_root,
503 // Never mind the above, it's just a theme missing a style.css.
504 // Return it; WP_Theme will catch the error.
505 if ( ! $found_theme )
506 $found_themes[ $dir ] = array(
507 'theme_file' => $dir . '/style.css',
508 'theme_root' => $theme_root,
514 asort( $found_themes );
516 $theme_roots = array();
517 $relative_theme_roots = array_flip( $relative_theme_roots );
519 foreach ( $found_themes as $theme_dir => $theme_data ) {
520 $theme_roots[ $theme_dir ] = $relative_theme_roots[ $theme_data['theme_root'] ]; // Convert absolute to relative.
523 if ( $theme_roots != get_site_transient( 'theme_roots' ) )
524 set_site_transient( 'theme_roots', $theme_roots, $cache_expiration );
526 return $found_themes;
530 * Retrieve path to themes directory.
532 * Does not have trailing slash.
536 * @global array $wp_theme_directories
538 * @param string $stylesheet_or_template The stylesheet or template name of the theme
539 * @return string Theme path.
541 function get_theme_root( $stylesheet_or_template = false ) {
542 global $wp_theme_directories;
544 if ( $stylesheet_or_template && $theme_root = get_raw_theme_root( $stylesheet_or_template ) ) {
545 // Always prepend WP_CONTENT_DIR unless the root currently registered as a theme directory.
546 // This gives relative theme roots the benefit of the doubt when things go haywire.
547 if ( ! in_array( $theme_root, (array) $wp_theme_directories ) )
548 $theme_root = WP_CONTENT_DIR . $theme_root;
550 $theme_root = WP_CONTENT_DIR . '/themes';
554 * Filter the absolute path to the themes directory.
558 * @param string $theme_root Absolute path to themes directory.
560 return apply_filters( 'theme_root', $theme_root );
564 * Retrieve URI for themes directory.
566 * Does not have trailing slash.
570 * @global array $wp_theme_directories
572 * @param string $stylesheet_or_template Optional. The stylesheet or template name of the theme.
573 * Default is to leverage the main theme root.
574 * @param string $theme_root Optional. The theme root for which calculations will be based, preventing
575 * the need for a get_raw_theme_root() call.
576 * @return string Themes URI.
578 function get_theme_root_uri( $stylesheet_or_template = false, $theme_root = false ) {
579 global $wp_theme_directories;
581 if ( $stylesheet_or_template && ! $theme_root )
582 $theme_root = get_raw_theme_root( $stylesheet_or_template );
584 if ( $stylesheet_or_template && $theme_root ) {
585 if ( in_array( $theme_root, (array) $wp_theme_directories ) ) {
586 // Absolute path. Make an educated guess. YMMV -- but note the filter below.
587 if ( 0 === strpos( $theme_root, WP_CONTENT_DIR ) )
588 $theme_root_uri = content_url( str_replace( WP_CONTENT_DIR, '', $theme_root ) );
589 elseif ( 0 === strpos( $theme_root, ABSPATH ) )
590 $theme_root_uri = site_url( str_replace( ABSPATH, '', $theme_root ) );
591 elseif ( 0 === strpos( $theme_root, WP_PLUGIN_DIR ) || 0 === strpos( $theme_root, WPMU_PLUGIN_DIR ) )
592 $theme_root_uri = plugins_url( basename( $theme_root ), $theme_root );
594 $theme_root_uri = $theme_root;
596 $theme_root_uri = content_url( $theme_root );
599 $theme_root_uri = content_url( 'themes' );
603 * Filter the URI for themes directory.
607 * @param string $theme_root_uri The URI for themes directory.
608 * @param string $siteurl WordPress web address which is set in General Options.
609 * @param string $stylesheet_or_template Stylesheet or template name of the theme.
611 return apply_filters( 'theme_root_uri', $theme_root_uri, get_option( 'siteurl' ), $stylesheet_or_template );
615 * Get the raw theme root relative to the content directory with no filters applied.
619 * @global array $wp_theme_directories
621 * @param string $stylesheet_or_template The stylesheet or template name of the theme
622 * @param bool $skip_cache Optional. Whether to skip the cache.
623 * Defaults to false, meaning the cache is used.
624 * @return string Theme root
626 function get_raw_theme_root( $stylesheet_or_template, $skip_cache = false ) {
627 global $wp_theme_directories;
629 if ( count($wp_theme_directories) <= 1 )
634 // If requesting the root for the current theme, consult options to avoid calling get_theme_roots()
635 if ( ! $skip_cache ) {
636 if ( get_option('stylesheet') == $stylesheet_or_template )
637 $theme_root = get_option('stylesheet_root');
638 elseif ( get_option('template') == $stylesheet_or_template )
639 $theme_root = get_option('template_root');
642 if ( empty($theme_root) ) {
643 $theme_roots = get_theme_roots();
644 if ( !empty($theme_roots[$stylesheet_or_template]) )
645 $theme_root = $theme_roots[$stylesheet_or_template];
652 * Display localized stylesheet link element.
656 function locale_stylesheet() {
657 $stylesheet = get_locale_stylesheet_uri();
658 if ( empty($stylesheet) )
660 echo '<link rel="stylesheet" href="' . $stylesheet . '" type="text/css" media="screen" />';
664 * Switches the theme.
666 * Accepts one argument: $stylesheet of the theme. It also accepts an additional function signature
667 * of two arguments: $template then $stylesheet. This is for backwards compatibility.
671 * @global array $wp_theme_directories
672 * @global WP_Customize_Manager $wp_customize
673 * @global array $sidebars_widgets
675 * @param string $stylesheet Stylesheet name
677 function switch_theme( $stylesheet ) {
678 global $wp_theme_directories, $wp_customize, $sidebars_widgets;
680 $_sidebars_widgets = null;
681 if ( 'wp_ajax_customize_save' === current_action() ) {
682 $_sidebars_widgets = $wp_customize->post_value( $wp_customize->get_setting( 'old_sidebars_widgets_data' ) );
683 } elseif ( is_array( $sidebars_widgets ) ) {
684 $_sidebars_widgets = $sidebars_widgets;
687 if ( is_array( $_sidebars_widgets ) ) {
688 set_theme_mod( 'sidebars_widgets', array( 'time' => time(), 'data' => $_sidebars_widgets ) );
691 $old_theme = wp_get_theme();
692 $new_theme = wp_get_theme( $stylesheet );
694 if ( func_num_args() > 1 ) {
695 $template = $stylesheet;
696 $stylesheet = func_get_arg( 1 );
698 $template = $new_theme->get_template();
701 update_option( 'template', $template );
702 update_option( 'stylesheet', $stylesheet );
704 if ( count( $wp_theme_directories ) > 1 ) {
705 update_option( 'template_root', get_raw_theme_root( $template, true ) );
706 update_option( 'stylesheet_root', get_raw_theme_root( $stylesheet, true ) );
708 delete_option( 'template_root' );
709 delete_option( 'stylesheet_root' );
712 $new_name = $new_theme->get('Name');
714 update_option( 'current_theme', $new_name );
716 // Migrate from the old mods_{name} option to theme_mods_{slug}.
717 if ( is_admin() && false === get_option( 'theme_mods_' . $stylesheet ) ) {
718 $default_theme_mods = (array) get_option( 'mods_' . $new_name );
719 add_option( "theme_mods_$stylesheet", $default_theme_mods );
722 * Since retrieve_widgets() is called when initializing a theme in the Customizer,
723 * we need to to remove the theme mods to avoid overwriting changes made via
724 * the Customizer when accessing wp-admin/widgets.php.
726 if ( 'wp_ajax_customize_save' === current_action() ) {
727 remove_theme_mod( 'sidebars_widgets' );
731 update_option( 'theme_switched', $old_theme->get_stylesheet() );
733 * Fires after the theme is switched.
737 * @param string $new_name Name of the new theme.
738 * @param WP_Theme $new_theme WP_Theme instance of the new theme.
740 do_action( 'switch_theme', $new_name, $new_theme );
744 * Checks that current theme files 'index.php' and 'style.css' exists.
746 * Does not check the default theme, which is the fallback and should always exist.
747 * Will switch theme to the fallback theme if current theme does not validate.
748 * You can use the 'validate_current_theme' filter to return false to
749 * disable this functionality.
752 * @see WP_DEFAULT_THEME
756 function validate_current_theme() {
758 * Filter whether to validate the current theme.
762 * @param bool true Validation flag to check the current theme.
764 if ( defined('WP_INSTALLING') || ! apply_filters( 'validate_current_theme', true ) )
767 if ( get_template() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/index.php') ) {
768 switch_theme( WP_DEFAULT_THEME );
772 if ( get_stylesheet() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/style.css') ) {
773 switch_theme( WP_DEFAULT_THEME );
777 if ( is_child_theme() && ! file_exists( get_stylesheet_directory() . '/style.css' ) ) {
778 switch_theme( WP_DEFAULT_THEME );
786 * Retrieve all theme modifications.
790 * @return array|void Theme modifications.
792 function get_theme_mods() {
793 $theme_slug = get_option( 'stylesheet' );
794 $mods = get_option( "theme_mods_$theme_slug" );
795 if ( false === $mods ) {
796 $theme_name = get_option( 'current_theme' );
797 if ( false === $theme_name )
798 $theme_name = wp_get_theme()->get('Name');
799 $mods = get_option( "mods_$theme_name" ); // Deprecated location.
800 if ( is_admin() && false !== $mods ) {
801 update_option( "theme_mods_$theme_slug", $mods );
802 delete_option( "mods_$theme_name" );
809 * Retrieve theme modification value for the current theme.
811 * If the modification name does not exist, then the $default will be passed
812 * through {@link http://php.net/sprintf sprintf()} PHP function with the first
813 * string the template directory URI and the second string the stylesheet
818 * @param string $name Theme modification name.
819 * @param bool|string $default
822 function get_theme_mod( $name, $default = false ) {
823 $mods = get_theme_mods();
825 if ( isset( $mods[$name] ) ) {
827 * Filter the theme modification, or 'theme_mod', value.
829 * The dynamic portion of the hook name, `$name`, refers to
830 * the key name of the modification array. For example,
831 * 'header_textcolor', 'header_image', and so on depending
832 * on the theme options.
836 * @param string $current_mod The value of the current theme modification.
838 return apply_filters( "theme_mod_{$name}", $mods[$name] );
841 if ( is_string( $default ) )
842 $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
844 /** This filter is documented in wp-includes/theme.php */
845 return apply_filters( "theme_mod_{$name}", $default );
849 * Update theme modification value for the current theme.
853 * @param string $name Theme modification name.
854 * @param mixed $value Theme modification value.
856 function set_theme_mod( $name, $value ) {
857 $mods = get_theme_mods();
858 $old_value = isset( $mods[ $name ] ) ? $mods[ $name ] : false;
861 * Filter the theme mod value on save.
863 * The dynamic portion of the hook name, `$name`, refers to the key name of
864 * the modification array. For example, 'header_textcolor', 'header_image',
865 * and so on depending on the theme options.
869 * @param string $value The new value of the theme mod.
870 * @param string $old_value The current value of the theme mod.
872 $mods[ $name ] = apply_filters( "pre_set_theme_mod_$name", $value, $old_value );
874 $theme = get_option( 'stylesheet' );
875 update_option( "theme_mods_$theme", $mods );
879 * Remove theme modification name from current theme list.
881 * If removing the name also removes all elements, then the entire option will
886 * @param string $name Theme modification name.
888 function remove_theme_mod( $name ) {
889 $mods = get_theme_mods();
891 if ( ! isset( $mods[ $name ] ) )
894 unset( $mods[ $name ] );
896 if ( empty( $mods ) ) {
900 $theme = get_option( 'stylesheet' );
901 update_option( "theme_mods_$theme", $mods );
905 * Remove theme modifications option for current theme.
909 function remove_theme_mods() {
910 delete_option( 'theme_mods_' . get_option( 'stylesheet' ) );
913 $theme_name = get_option( 'current_theme' );
914 if ( false === $theme_name )
915 $theme_name = wp_get_theme()->get('Name');
916 delete_option( 'mods_' . $theme_name );
920 * Retrieve text color for custom header.
926 function get_header_textcolor() {
927 return get_theme_mod('header_textcolor', get_theme_support( 'custom-header', 'default-text-color' ) );
931 * Display text color for custom header.
935 function header_textcolor() {
936 echo get_header_textcolor();
940 * Whether to display the header text.
946 function display_header_text() {
947 if ( ! current_theme_supports( 'custom-header', 'header-text' ) )
950 $text_color = get_theme_mod( 'header_textcolor', get_theme_support( 'custom-header', 'default-text-color' ) );
951 return 'blank' !== $text_color;
955 * Check whether a header image is set or not.
959 * @see get_header_image()
961 * @return bool Whether a header image is set or not.
963 function has_header_image() {
964 return (bool) get_header_image();
968 * Retrieve header image for custom header.
972 * @return string|false
974 function get_header_image() {
975 $url = get_theme_mod( 'header_image', get_theme_support( 'custom-header', 'default-image' ) );
977 if ( 'remove-header' == $url )
980 if ( is_random_header_image() )
981 $url = get_random_header_image();
983 return esc_url_raw( set_url_scheme( $url ) );
987 * Get random header image data from registered images in theme.
993 * @global array $_wp_default_headers
994 * @staticvar object $_wp_random_header
998 function _get_random_header_data() {
999 static $_wp_random_header = null;
1001 if ( empty( $_wp_random_header ) ) {
1002 global $_wp_default_headers;
1003 $header_image_mod = get_theme_mod( 'header_image', '' );
1006 if ( 'random-uploaded-image' == $header_image_mod )
1007 $headers = get_uploaded_header_images();
1008 elseif ( ! empty( $_wp_default_headers ) ) {
1009 if ( 'random-default-image' == $header_image_mod ) {
1010 $headers = $_wp_default_headers;
1012 if ( current_theme_supports( 'custom-header', 'random-default' ) )
1013 $headers = $_wp_default_headers;
1017 if ( empty( $headers ) )
1018 return new stdClass;
1020 $_wp_random_header = (object) $headers[ array_rand( $headers ) ];
1022 $_wp_random_header->url = sprintf( $_wp_random_header->url, get_template_directory_uri(), get_stylesheet_directory_uri() );
1023 $_wp_random_header->thumbnail_url = sprintf( $_wp_random_header->thumbnail_url, get_template_directory_uri(), get_stylesheet_directory_uri() );
1025 return $_wp_random_header;
1029 * Get random header image url from registered images in theme.
1033 * @return string Path to header image
1035 function get_random_header_image() {
1036 $random_image = _get_random_header_data();
1037 if ( empty( $random_image->url ) )
1039 return $random_image->url;
1043 * Check if random header image is in use.
1045 * Always true if user expressly chooses the option in Appearance > Header.
1046 * Also true if theme has multiple header images registered, no specific header image
1047 * is chosen, and theme turns on random headers with add_theme_support().
1051 * @param string $type The random pool to use. any|default|uploaded
1054 function is_random_header_image( $type = 'any' ) {
1055 $header_image_mod = get_theme_mod( 'header_image', get_theme_support( 'custom-header', 'default-image' ) );
1057 if ( 'any' == $type ) {
1058 if ( 'random-default-image' == $header_image_mod || 'random-uploaded-image' == $header_image_mod || ( '' != get_random_header_image() && empty( $header_image_mod ) ) )
1061 if ( "random-$type-image" == $header_image_mod )
1063 elseif ( 'default' == $type && empty( $header_image_mod ) && '' != get_random_header_image() )
1071 * Display header image URL.
1075 function header_image() {
1076 $image = get_header_image();
1078 echo esc_url( $image );
1083 * Get the header images uploaded for the current theme.
1089 function get_uploaded_header_images() {
1090 $header_images = array();
1093 $headers = get_posts( array( 'post_type' => 'attachment', 'meta_key' => '_wp_attachment_is_custom_header', 'meta_value' => get_option('stylesheet'), 'orderby' => 'none', 'nopaging' => true ) );
1095 if ( empty( $headers ) )
1098 foreach ( (array) $headers as $header ) {
1099 $url = esc_url_raw( wp_get_attachment_url( $header->ID ) );
1100 $header_data = wp_get_attachment_metadata( $header->ID );
1101 $header_index = basename($url);
1103 $header_images[$header_index] = array();
1104 $header_images[$header_index]['attachment_id'] = $header->ID;
1105 $header_images[$header_index]['url'] = $url;
1106 $header_images[$header_index]['thumbnail_url'] = $url;
1107 $header_images[$header_index]['alt_text'] = get_post_meta( $header->ID, '_wp_attachment_image_alt', true );
1109 if ( isset( $header_data['width'] ) )
1110 $header_images[$header_index]['width'] = $header_data['width'];
1111 if ( isset( $header_data['height'] ) )
1112 $header_images[$header_index]['height'] = $header_data['height'];
1115 return $header_images;
1119 * Get the header image data.
1123 * @global array $_wp_default_headers
1127 function get_custom_header() {
1128 global $_wp_default_headers;
1130 if ( is_random_header_image() ) {
1131 $data = _get_random_header_data();
1133 $data = get_theme_mod( 'header_image_data' );
1134 if ( ! $data && current_theme_supports( 'custom-header', 'default-image' ) ) {
1135 $directory_args = array( get_template_directory_uri(), get_stylesheet_directory_uri() );
1137 $data['url'] = $data['thumbnail_url'] = vsprintf( get_theme_support( 'custom-header', 'default-image' ), $directory_args );
1138 if ( ! empty( $_wp_default_headers ) ) {
1139 foreach ( (array) $_wp_default_headers as $default_header ) {
1140 $url = vsprintf( $default_header['url'], $directory_args );
1141 if ( $data['url'] == $url ) {
1142 $data = $default_header;
1143 $data['url'] = $url;
1144 $data['thumbnail_url'] = vsprintf( $data['thumbnail_url'], $directory_args );
1154 'thumbnail_url' => '',
1155 'width' => get_theme_support( 'custom-header', 'width' ),
1156 'height' => get_theme_support( 'custom-header', 'height' ),
1158 return (object) wp_parse_args( $data, $default );
1162 * Register a selection of default headers to be displayed by the custom header admin UI.
1166 * @global array $_wp_default_headers
1168 * @param array $headers Array of headers keyed by a string id. The ids point to arrays containing 'url', 'thumbnail_url', and 'description' keys.
1170 function register_default_headers( $headers ) {
1171 global $_wp_default_headers;
1173 $_wp_default_headers = array_merge( (array) $_wp_default_headers, (array) $headers );
1177 * Unregister default headers.
1179 * This function must be called after register_default_headers() has already added the
1180 * header you want to remove.
1182 * @see register_default_headers()
1185 * @global array $_wp_default_headers
1187 * @param string|array $header The header string id (key of array) to remove, or an array thereof.
1188 * @return bool|void A single header returns true on success, false on failure.
1189 * There is currently no return value for multiple headers.
1191 function unregister_default_headers( $header ) {
1192 global $_wp_default_headers;
1193 if ( is_array( $header ) ) {
1194 array_map( 'unregister_default_headers', $header );
1195 } elseif ( isset( $_wp_default_headers[ $header ] ) ) {
1196 unset( $_wp_default_headers[ $header ] );
1204 * Retrieve background image for custom background.
1210 function get_background_image() {
1211 return get_theme_mod('background_image', get_theme_support( 'custom-background', 'default-image' ) );
1215 * Display background image path.
1219 function background_image() {
1220 echo get_background_image();
1224 * Retrieve value for custom background color.
1230 function get_background_color() {
1231 return get_theme_mod('background_color', get_theme_support( 'custom-background', 'default-color' ) );
1235 * Display background color value.
1239 function background_color() {
1240 echo get_background_color();
1244 * Default custom background callback.
1249 function _custom_background_cb() {
1250 // $background is the saved custom image, or the default image.
1251 $background = set_url_scheme( get_background_image() );
1253 // $color is the saved custom color.
1254 // A default has to be specified in style.css. It will not be printed here.
1255 $color = get_background_color();
1257 if ( $color === get_theme_support( 'custom-background', 'default-color' ) ) {
1261 if ( ! $background && ! $color )
1264 $style = $color ? "background-color: #$color;" : '';
1266 if ( $background ) {
1267 $image = " background-image: url('$background');";
1269 $repeat = get_theme_mod( 'background_repeat', get_theme_support( 'custom-background', 'default-repeat' ) );
1270 if ( ! in_array( $repeat, array( 'no-repeat', 'repeat-x', 'repeat-y', 'repeat' ) ) )
1272 $repeat = " background-repeat: $repeat;";
1274 $position = get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) );
1275 if ( ! in_array( $position, array( 'center', 'right', 'left' ) ) )
1277 $position = " background-position: top $position;";
1279 $attachment = get_theme_mod( 'background_attachment', get_theme_support( 'custom-background', 'default-attachment' ) );
1280 if ( ! in_array( $attachment, array( 'fixed', 'scroll' ) ) )
1281 $attachment = 'scroll';
1282 $attachment = " background-attachment: $attachment;";
1284 $style .= $image . $repeat . $position . $attachment;
1287 <style type="text/css" id="custom-background-css">
1288 body.custom-background { <?php echo trim( $style ); ?> }
1294 * Add callback for custom TinyMCE editor stylesheets.
1296 * The parameter $stylesheet is the name of the stylesheet, relative to
1297 * the theme root. It also accepts an array of stylesheets.
1298 * It is optional and defaults to 'editor-style.css'.
1300 * This function automatically adds another stylesheet with -rtl prefix, e.g. editor-style-rtl.css.
1301 * If that file doesn't exist, it is removed before adding the stylesheet(s) to TinyMCE.
1302 * If an array of stylesheets is passed to add_editor_style(),
1303 * RTL is only added for the first stylesheet.
1305 * Since version 3.4 the TinyMCE body has .rtl CSS class.
1306 * It is a better option to use that class and add any RTL styles to the main stylesheet.
1310 * @global array $editor_styles
1312 * @param array|string $stylesheet Optional. Stylesheet name or array thereof, relative to theme root.
1313 * Defaults to 'editor-style.css'
1315 function add_editor_style( $stylesheet = 'editor-style.css' ) {
1316 add_theme_support( 'editor-style' );
1321 global $editor_styles;
1322 $editor_styles = (array) $editor_styles;
1323 $stylesheet = (array) $stylesheet;
1325 $rtl_stylesheet = str_replace('.css', '-rtl.css', $stylesheet[0]);
1326 $stylesheet[] = $rtl_stylesheet;
1329 $editor_styles = array_merge( $editor_styles, $stylesheet );
1333 * Removes all visual editor stylesheets.
1337 * @global array $editor_styles
1339 * @return bool True on success, false if there were no stylesheets to remove.
1341 function remove_editor_styles() {
1342 if ( ! current_theme_supports( 'editor-style' ) )
1344 _remove_theme_support( 'editor-style' );
1346 $GLOBALS['editor_styles'] = array();
1351 * Retrieve any registered editor stylesheets
1355 * @global array $editor_styles Registered editor stylesheets
1357 * @return array If registered, a list of editor stylesheet URLs.
1359 function get_editor_stylesheets() {
1360 $stylesheets = array();
1361 // load editor_style.css if the current theme supports it
1362 if ( ! empty( $GLOBALS['editor_styles'] ) && is_array( $GLOBALS['editor_styles'] ) ) {
1363 $editor_styles = $GLOBALS['editor_styles'];
1365 $editor_styles = array_unique( array_filter( $editor_styles ) );
1366 $style_uri = get_stylesheet_directory_uri();
1367 $style_dir = get_stylesheet_directory();
1369 // Support externally referenced styles (like, say, fonts).
1370 foreach ( $editor_styles as $key => $file ) {
1371 if ( preg_match( '~^(https?:)?//~', $file ) ) {
1372 $stylesheets[] = esc_url_raw( $file );
1373 unset( $editor_styles[ $key ] );
1377 // Look in a parent theme first, that way child theme CSS overrides.
1378 if ( is_child_theme() ) {
1379 $template_uri = get_template_directory_uri();
1380 $template_dir = get_template_directory();
1382 foreach ( $editor_styles as $key => $file ) {
1383 if ( $file && file_exists( "$template_dir/$file" ) ) {
1384 $stylesheets[] = "$template_uri/$file";
1389 foreach ( $editor_styles as $file ) {
1390 if ( $file && file_exists( "$style_dir/$file" ) ) {
1391 $stylesheets[] = "$style_uri/$file";
1397 * Filter the array of stylesheets applied to the editor.
1401 * @param array $stylesheets Array of stylesheets to be applied to the editor.
1403 return apply_filters( 'editor_stylesheets', $stylesheets );
1407 * Allows a theme to register its support of a certain feature
1409 * Must be called in the theme's functions.php file to work.
1410 * If attached to a hook, it must be after_setup_theme.
1411 * The init hook may be too late for some features.
1415 * @global array $_wp_theme_features
1417 * @param string $feature The feature being added.
1418 * @return void|bool False on failure, void otherwise.
1420 function add_theme_support( $feature ) {
1421 global $_wp_theme_features;
1423 if ( func_num_args() == 1 )
1426 $args = array_slice( func_get_args(), 1 );
1428 switch ( $feature ) {
1429 case 'post-formats' :
1430 if ( is_array( $args[0] ) ) {
1431 $post_formats = get_post_format_slugs();
1432 unset( $post_formats['standard'] );
1434 $args[0] = array_intersect( $args[0], array_keys( $post_formats ) );
1439 // You can't just pass 'html5', you need to pass an array of types.
1440 if ( empty( $args[0] ) ) {
1441 // Build an array of types for back-compat.
1442 $args = array( 0 => array( 'comment-list', 'comment-form', 'search-form' ) );
1443 } elseif ( ! is_array( $args[0] ) ) {
1444 _doing_it_wrong( "add_theme_support( 'html5' )", __( 'You need to pass an array of types.' ), '3.6.1' );
1448 // Calling 'html5' again merges, rather than overwrites.
1449 if ( isset( $_wp_theme_features['html5'] ) )
1450 $args[0] = array_merge( $_wp_theme_features['html5'][0], $args[0] );
1453 case 'custom-header-uploads' :
1454 return add_theme_support( 'custom-header', array( 'uploads' => true ) );
1456 case 'custom-header' :
1457 if ( ! is_array( $args ) )
1458 $args = array( 0 => array() );
1461 'default-image' => '',
1462 'random-default' => false,
1465 'flex-height' => false,
1466 'flex-width' => false,
1467 'default-text-color' => '',
1468 'header-text' => true,
1470 'wp-head-callback' => '',
1471 'admin-head-callback' => '',
1472 'admin-preview-callback' => '',
1475 $jit = isset( $args[0]['__jit'] );
1476 unset( $args[0]['__jit'] );
1478 // Merge in data from previous add_theme_support() calls.
1479 // The first value registered wins. (A child theme is set up first.)
1480 if ( isset( $_wp_theme_features['custom-header'] ) )
1481 $args[0] = wp_parse_args( $_wp_theme_features['custom-header'][0], $args[0] );
1483 // Load in the defaults at the end, as we need to insure first one wins.
1484 // This will cause all constants to be defined, as each arg will then be set to the default.
1486 $args[0] = wp_parse_args( $args[0], $defaults );
1488 // If a constant was defined, use that value. Otherwise, define the constant to ensure
1489 // the constant is always accurate (and is not defined later, overriding our value).
1490 // As stated above, the first value wins.
1491 // Once we get to wp_loaded (just-in-time), define any constants we haven't already.
1492 // Constants are lame. Don't reference them. This is just for backwards compatibility.
1494 if ( defined( 'NO_HEADER_TEXT' ) )
1495 $args[0]['header-text'] = ! NO_HEADER_TEXT;
1496 elseif ( isset( $args[0]['header-text'] ) )
1497 define( 'NO_HEADER_TEXT', empty( $args[0]['header-text'] ) );
1499 if ( defined( 'HEADER_IMAGE_WIDTH' ) )
1500 $args[0]['width'] = (int) HEADER_IMAGE_WIDTH;
1501 elseif ( isset( $args[0]['width'] ) )
1502 define( 'HEADER_IMAGE_WIDTH', (int) $args[0]['width'] );
1504 if ( defined( 'HEADER_IMAGE_HEIGHT' ) )
1505 $args[0]['height'] = (int) HEADER_IMAGE_HEIGHT;
1506 elseif ( isset( $args[0]['height'] ) )
1507 define( 'HEADER_IMAGE_HEIGHT', (int) $args[0]['height'] );
1509 if ( defined( 'HEADER_TEXTCOLOR' ) )
1510 $args[0]['default-text-color'] = HEADER_TEXTCOLOR;
1511 elseif ( isset( $args[0]['default-text-color'] ) )
1512 define( 'HEADER_TEXTCOLOR', $args[0]['default-text-color'] );
1514 if ( defined( 'HEADER_IMAGE' ) )
1515 $args[0]['default-image'] = HEADER_IMAGE;
1516 elseif ( isset( $args[0]['default-image'] ) )
1517 define( 'HEADER_IMAGE', $args[0]['default-image'] );
1519 if ( $jit && ! empty( $args[0]['default-image'] ) )
1520 $args[0]['random-default'] = false;
1522 // If headers are supported, and we still don't have a defined width or height,
1523 // we have implicit flex sizes.
1525 if ( empty( $args[0]['width'] ) && empty( $args[0]['flex-width'] ) )
1526 $args[0]['flex-width'] = true;
1527 if ( empty( $args[0]['height'] ) && empty( $args[0]['flex-height'] ) )
1528 $args[0]['flex-height'] = true;
1533 case 'custom-background' :
1534 if ( ! is_array( $args ) )
1535 $args = array( 0 => array() );
1538 'default-image' => '',
1539 'default-repeat' => 'repeat',
1540 'default-position-x' => 'left',
1541 'default-attachment' => 'scroll',
1542 'default-color' => '',
1543 'wp-head-callback' => '_custom_background_cb',
1544 'admin-head-callback' => '',
1545 'admin-preview-callback' => '',
1548 $jit = isset( $args[0]['__jit'] );
1549 unset( $args[0]['__jit'] );
1551 // Merge in data from previous add_theme_support() calls. The first value registered wins.
1552 if ( isset( $_wp_theme_features['custom-background'] ) )
1553 $args[0] = wp_parse_args( $_wp_theme_features['custom-background'][0], $args[0] );
1556 $args[0] = wp_parse_args( $args[0], $defaults );
1558 if ( defined( 'BACKGROUND_COLOR' ) )
1559 $args[0]['default-color'] = BACKGROUND_COLOR;
1560 elseif ( isset( $args[0]['default-color'] ) || $jit )
1561 define( 'BACKGROUND_COLOR', $args[0]['default-color'] );
1563 if ( defined( 'BACKGROUND_IMAGE' ) )
1564 $args[0]['default-image'] = BACKGROUND_IMAGE;
1565 elseif ( isset( $args[0]['default-image'] ) || $jit )
1566 define( 'BACKGROUND_IMAGE', $args[0]['default-image'] );
1570 // Ensure that 'title-tag' is accessible in the admin.
1572 // Can be called in functions.php but must happen before wp_loaded, i.e. not in header.php.
1573 if ( did_action( 'wp_loaded' ) ) {
1574 /* translators: 1: Theme support 2: hook name */
1575 _doing_it_wrong( "add_theme_support( 'title-tag' )", sprintf( __( 'Theme support for %1$s should be registered before the %2$s hook.' ),
1576 '<code>title-tag</code>', '<code>wp_loaded</code>' ), '4.1' );
1582 $_wp_theme_features[ $feature ] = $args;
1586 * Registers the internal custom header and background routines.
1591 * @global Custom_Image_Header $custom_image_header
1592 * @global Custom_Background $custom_background
1594 function _custom_header_background_just_in_time() {
1595 global $custom_image_header, $custom_background;
1597 if ( current_theme_supports( 'custom-header' ) ) {
1598 // In case any constants were defined after an add_custom_image_header() call, re-run.
1599 add_theme_support( 'custom-header', array( '__jit' => true ) );
1601 $args = get_theme_support( 'custom-header' );
1602 if ( $args[0]['wp-head-callback'] )
1603 add_action( 'wp_head', $args[0]['wp-head-callback'] );
1606 require_once( ABSPATH . 'wp-admin/custom-header.php' );
1607 $custom_image_header = new Custom_Image_Header( $args[0]['admin-head-callback'], $args[0]['admin-preview-callback'] );
1611 if ( current_theme_supports( 'custom-background' ) ) {
1612 // In case any constants were defined after an add_custom_background() call, re-run.
1613 add_theme_support( 'custom-background', array( '__jit' => true ) );
1615 $args = get_theme_support( 'custom-background' );
1616 add_action( 'wp_head', $args[0]['wp-head-callback'] );
1619 require_once( ABSPATH . 'wp-admin/custom-background.php' );
1620 $custom_background = new Custom_Background( $args[0]['admin-head-callback'], $args[0]['admin-preview-callback'] );
1626 * Gets the theme support arguments passed when registering that support
1630 * @global array $_wp_theme_features
1632 * @param string $feature the feature to check
1633 * @return mixed The array of extra arguments or the value for the registered feature.
1635 function get_theme_support( $feature ) {
1636 global $_wp_theme_features;
1637 if ( ! isset( $_wp_theme_features[ $feature ] ) )
1640 if ( func_num_args() <= 1 )
1641 return $_wp_theme_features[ $feature ];
1643 $args = array_slice( func_get_args(), 1 );
1644 switch ( $feature ) {
1645 case 'custom-header' :
1646 case 'custom-background' :
1647 if ( isset( $_wp_theme_features[ $feature ][0][ $args[0] ] ) )
1648 return $_wp_theme_features[ $feature ][0][ $args[0] ];
1652 return $_wp_theme_features[ $feature ];
1657 * Allows a theme to de-register its support of a certain feature
1659 * Should be called in the theme's functions.php file. Generally would
1660 * be used for child themes to override support from the parent theme.
1663 * @see add_theme_support()
1664 * @param string $feature the feature being added
1665 * @return bool|void Whether feature was removed.
1667 function remove_theme_support( $feature ) {
1668 // Blacklist: for internal registrations not used directly by themes.
1669 if ( in_array( $feature, array( 'editor-style', 'widgets', 'menus' ) ) )
1672 return _remove_theme_support( $feature );
1676 * Do not use. Removes theme support internally, ignorant of the blacklist.
1681 * @global array $_wp_theme_features
1682 * @global Custom_Image_Header $custom_image_header
1683 * @global Custom_Background $custom_background
1685 * @param string $feature
1687 function _remove_theme_support( $feature ) {
1688 global $_wp_theme_features;
1690 switch ( $feature ) {
1691 case 'custom-header-uploads' :
1692 if ( ! isset( $_wp_theme_features['custom-header'] ) )
1694 add_theme_support( 'custom-header', array( 'uploads' => false ) );
1695 return; // Do not continue - custom-header-uploads no longer exists.
1698 if ( ! isset( $_wp_theme_features[ $feature ] ) )
1701 switch ( $feature ) {
1702 case 'custom-header' :
1703 if ( ! did_action( 'wp_loaded' ) )
1705 $support = get_theme_support( 'custom-header' );
1706 if ( $support[0]['wp-head-callback'] )
1707 remove_action( 'wp_head', $support[0]['wp-head-callback'] );
1708 remove_action( 'admin_menu', array( $GLOBALS['custom_image_header'], 'init' ) );
1709 unset( $GLOBALS['custom_image_header'] );
1712 case 'custom-background' :
1713 if ( ! did_action( 'wp_loaded' ) )
1715 $support = get_theme_support( 'custom-background' );
1716 remove_action( 'wp_head', $support[0]['wp-head-callback'] );
1717 remove_action( 'admin_menu', array( $GLOBALS['custom_background'], 'init' ) );
1718 unset( $GLOBALS['custom_background'] );
1722 unset( $_wp_theme_features[ $feature ] );
1727 * Checks a theme's support for a given feature
1731 * @global array $_wp_theme_features
1733 * @param string $feature the feature being checked
1736 function current_theme_supports( $feature ) {
1737 global $_wp_theme_features;
1739 if ( 'custom-header-uploads' == $feature )
1740 return current_theme_supports( 'custom-header', 'uploads' );
1742 if ( !isset( $_wp_theme_features[$feature] ) )
1745 if ( 'title-tag' == $feature ) {
1746 // Don't confirm support unless called internally.
1747 $trace = debug_backtrace();
1748 if ( ! in_array( $trace[1]['function'], array( '_wp_render_title_tag', 'wp_title' ) ) ) {
1753 // If no args passed then no extra checks need be performed
1754 if ( func_num_args() <= 1 )
1757 $args = array_slice( func_get_args(), 1 );
1759 switch ( $feature ) {
1760 case 'post-thumbnails':
1761 // post-thumbnails can be registered for only certain content/post types by passing
1762 // an array of types to add_theme_support(). If no array was passed, then
1763 // any type is accepted
1764 if ( true === $_wp_theme_features[$feature] ) // Registered for all types
1766 $content_type = $args[0];
1767 return in_array( $content_type, $_wp_theme_features[$feature][0] );
1770 case 'post-formats':
1771 // specific post formats can be registered by passing an array of types to
1772 // add_theme_support()
1774 // Specific areas of HTML5 support *must* be passed via an array to add_theme_support()
1777 return in_array( $type, $_wp_theme_features[$feature][0] );
1779 case 'custom-header':
1780 case 'custom-background' :
1781 // specific custom header and background capabilities can be registered by passing
1782 // an array to add_theme_support()
1783 $header_support = $args[0];
1784 return ( isset( $_wp_theme_features[$feature][0][$header_support] ) && $_wp_theme_features[$feature][0][$header_support] );
1788 * Filter whether the current theme supports a specific feature.
1790 * The dynamic portion of the hook name, `$feature`, refers to the specific theme
1791 * feature. Possible values include 'post-formats', 'post-thumbnails', 'custom-background',
1792 * 'custom-header', 'menus', 'automatic-feed-links', and 'html5'.
1796 * @param bool true Whether the current theme supports the given feature. Default true.
1797 * @param array $args Array of arguments for the feature.
1798 * @param string $feature The theme feature.
1800 return apply_filters( "current_theme_supports-{$feature}", true, $args, $_wp_theme_features[$feature] );
1804 * Checks a theme's support for a given feature before loading the functions which implement it.
1808 * @param string $feature The feature being checked.
1809 * @param string $include Path to the file.
1810 * @return bool True if the current theme supports the supplied feature, false otherwise.
1812 function require_if_theme_supports( $feature, $include ) {
1813 if ( current_theme_supports( $feature ) ) {
1814 require ( $include );
1821 * Checks an attachment being deleted to see if it's a header or background image.
1823 * If true it removes the theme modification which would be pointing at the deleted
1828 * @since 4.3.0 Also removes `header_image_data`.
1830 * @param int $id The attachment id.
1832 function _delete_attachment_theme_mod( $id ) {
1833 $attachment_image = wp_get_attachment_url( $id );
1834 $header_image = get_header_image();
1835 $background_image = get_background_image();
1837 if ( $header_image && $header_image == $attachment_image ) {
1838 remove_theme_mod( 'header_image' );
1839 remove_theme_mod( 'header_image_data' );
1842 if ( $background_image && $background_image == $attachment_image ) {
1843 remove_theme_mod( 'background_image' );
1848 * Checks if a theme has been changed and runs 'after_switch_theme' hook on the next WP load
1852 function check_theme_switched() {
1853 if ( $stylesheet = get_option( 'theme_switched' ) ) {
1854 $old_theme = wp_get_theme( $stylesheet );
1856 // Prevent retrieve_widgets() from running since Customizer already called it up front
1857 if ( get_option( 'theme_switched_via_customizer' ) ) {
1858 remove_action( 'after_switch_theme', '_wp_sidebars_changed' );
1859 update_option( 'theme_switched_via_customizer', false );
1862 if ( $old_theme->exists() ) {
1864 * Fires on the first WP load after a theme switch if the old theme still exists.
1866 * This action fires multiple times and the parameters differs
1867 * according to the context, if the old theme exists or not.
1868 * If the old theme is missing, the parameter will be the slug
1873 * @param string $old_name Old theme name.
1874 * @param WP_Theme $old_theme WP_Theme instance of the old theme.
1876 do_action( 'after_switch_theme', $old_theme->get( 'Name' ), $old_theme );
1878 /** This action is documented in wp-includes/theme.php */
1879 do_action( 'after_switch_theme', $stylesheet );
1882 update_option( 'theme_switched', false );
1887 * Includes and instantiates the WP_Customize_Manager class.
1889 * Fires when ?wp_customize=on or on wp-admin/customize.php.
1893 * @global WP_Customize_Manager $wp_customize
1895 function _wp_customize_include() {
1896 if ( ! ( ( isset( $_REQUEST['wp_customize'] ) && 'on' == $_REQUEST['wp_customize'] )
1897 || ( is_admin() && 'customize.php' == basename( $_SERVER['PHP_SELF'] ) )
1901 require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
1902 $GLOBALS['wp_customize'] = new WP_Customize_Manager();
1906 * Adds settings for the customize-loader script.
1910 function _wp_customize_loader_settings() {
1911 $admin_origin = parse_url( admin_url() );
1912 $home_origin = parse_url( home_url() );
1913 $cross_domain = ( strtolower( $admin_origin[ 'host' ] ) != strtolower( $home_origin[ 'host' ] ) );
1916 'mobile' => wp_is_mobile(),
1917 'ios' => wp_is_mobile() && preg_match( '/iPad|iPod|iPhone/', $_SERVER['HTTP_USER_AGENT'] ),
1921 'url' => esc_url( admin_url( 'customize.php' ) ),
1922 'isCrossDomain' => $cross_domain,
1923 'browser' => $browser,
1925 'saveAlert' => __( 'The changes you made will be lost if you navigate away from this page.' ),
1926 'mainIframeTitle' => __( 'Customizer' ),
1930 $script = 'var _wpCustomizeLoaderSettings = ' . wp_json_encode( $settings ) . ';';
1932 $wp_scripts = wp_scripts();
1933 $data = $wp_scripts->get_data( 'customize-loader', 'data' );
1935 $script = "$data\n$script";
1937 $wp_scripts->add_data( 'customize-loader', 'data', $script );
1941 * Returns a URL to load the Customizer.
1945 * @param string $stylesheet Optional. Theme to customize. Defaults to current theme.
1946 * The theme's stylesheet will be urlencoded if necessary.
1949 function wp_customize_url( $stylesheet = null ) {
1950 $url = admin_url( 'customize.php' );
1952 $url .= '?theme=' . urlencode( $stylesheet );
1953 return esc_url( $url );
1957 * Prints a script to check whether or not the Customizer is supported,
1958 * and apply either the no-customize-support or customize-support class
1961 * This function MUST be called inside the body tag.
1963 * Ideally, call this function immediately after the body tag is opened.
1964 * This prevents a flash of unstyled content.
1966 * It is also recommended that you add the "no-customize-support" class
1967 * to the body tag by default.
1971 function wp_customize_support_script() {
1972 $admin_origin = parse_url( admin_url() );
1973 $home_origin = parse_url( home_url() );
1974 $cross_domain = ( strtolower( $admin_origin[ 'host' ] ) != strtolower( $home_origin[ 'host' ] ) );
1977 <script type="text/javascript">
1979 var request, b = document.body, c = 'className', cs = 'customize-support', rcs = new RegExp('(^|\\s+)(no-)?'+cs+'(\\s+|$)');
1981 <?php if ( $cross_domain ): ?>
1982 request = (function(){ var xhr = new XMLHttpRequest(); return ('withCredentials' in xhr); })();
1987 b[c] = b[c].replace( rcs, ' ' );
1988 b[c] += ( window.postMessage && request ? ' ' : ' no-' ) + cs;
1995 * Whether the site is being previewed in the Customizer.
1999 * @global WP_Customize_Manager $wp_customize Customizer instance.
2001 * @return bool True if the site is being previewed in the Customizer, false otherwise.
2003 function is_customize_preview() {
2004 global $wp_customize;
2006 return ( $wp_customize instanceof WP_Customize_Manager ) && $wp_customize->is_preview();