3 * Theme, template, and stylesheet functions.
10 * Returns an array of WP_Theme objects based on the arguments.
12 * Despite advances over get_themes(), this function is quite expensive, and grows
13 * linearly with additional themes. Stick to wp_get_theme() if possible.
17 * @param array $args The search arguments. Optional.
18 * - errors mixed True to return themes with errors, false to return themes without errors, null
19 * to return all themes. Defaults to false.
20 * - allowed mixed (Multisite) True to return only allowed themes for a site. False to return only
21 * disallowed themes for a site. 'site' to return only site-allowed themes. 'network'
22 * to return only network-allowed themes. Null to return all themes. Defaults to null.
23 * - blog_id int (Multisite) The blog ID used to calculate which themes are allowed. Defaults to 0,
24 * synonymous for the current blog.
25 * @return Array of WP_Theme objects.
27 function wp_get_themes( $args = array() ) {
28 global $wp_theme_directories;
30 $defaults = array( 'errors' => false, 'allowed' => null, 'blog_id' => 0 );
31 $args = wp_parse_args( $args, $defaults );
33 $theme_directories = search_theme_directories();
35 if ( count( $wp_theme_directories ) > 1 ) {
36 // Make sure the current theme wins out, in case search_theme_directories() picks the wrong
37 // one in the case of a conflict. (Normally, last registered theme root wins.)
38 $current_theme = get_stylesheet();
39 if ( isset( $theme_directories[ $current_theme ] ) ) {
40 $root_of_current_theme = get_raw_theme_root( $current_theme );
41 if ( ! in_array( $root_of_current_theme, $wp_theme_directories ) )
42 $root_of_current_theme = WP_CONTENT_DIR . $root_of_current_theme;
43 $theme_directories[ $current_theme ]['theme_root'] = $root_of_current_theme;
47 if ( empty( $theme_directories ) )
50 if ( is_multisite() && null !== $args['allowed'] ) {
51 $allowed = $args['allowed'];
52 if ( 'network' === $allowed )
53 $theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed_on_network() );
54 elseif ( 'site' === $allowed )
55 $theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed_on_site( $args['blog_id'] ) );
57 $theme_directories = array_intersect_key( $theme_directories, WP_Theme::get_allowed( $args['blog_id'] ) );
59 $theme_directories = array_diff_key( $theme_directories, WP_Theme::get_allowed( $args['blog_id'] ) );
63 static $_themes = array();
65 foreach ( $theme_directories as $theme => $theme_root ) {
66 if ( isset( $_themes[ $theme_root['theme_root'] . '/' . $theme ] ) )
67 $themes[ $theme ] = $_themes[ $theme_root['theme_root'] . '/' . $theme ];
69 $themes[ $theme ] = $_themes[ $theme_root['theme_root'] . '/' . $theme ] = new WP_Theme( $theme, $theme_root['theme_root'] );
72 if ( null !== $args['errors'] ) {
73 foreach ( $themes as $theme => $wp_theme ) {
74 if ( $wp_theme->errors() != $args['errors'] )
75 unset( $themes[ $theme ] );
83 * Gets a WP_Theme object for a theme.
87 * @param string $stylesheet Directory name for the theme. Optional. Defaults to current theme.
88 * @param string $theme_root Absolute path of the theme root to look in. Optional. If not specified, get_raw_theme_root()
89 * is used to calculate the theme root for the $stylesheet provided (or current theme).
90 * @return WP_Theme Theme object. Be sure to check the object's exists() method if you need to confirm the theme's existence.
92 function wp_get_theme( $stylesheet = null, $theme_root = null ) {
93 global $wp_theme_directories;
95 if ( empty( $stylesheet ) )
96 $stylesheet = get_stylesheet();
98 if ( empty( $theme_root ) ) {
99 $theme_root = get_raw_theme_root( $stylesheet );
100 if ( false === $theme_root )
101 $theme_root = WP_CONTENT_DIR . '/themes';
102 elseif ( ! in_array( $theme_root, (array) $wp_theme_directories ) )
103 $theme_root = WP_CONTENT_DIR . $theme_root;
106 return new WP_Theme( $stylesheet, $theme_root );
110 * Clears the cache held by get_theme_roots() and WP_Theme.
113 * @param bool $clear_update_cache Whether to clear the Theme updates cache
115 function wp_clean_themes_cache( $clear_update_cache = true ) {
116 if ( $clear_update_cache )
117 delete_site_transient( 'update_themes' );
118 search_theme_directories( true );
119 foreach ( wp_get_themes( array( 'errors' => null ) ) as $theme )
120 $theme->cache_delete();
124 * Whether a child theme is in use.
128 * @return bool true if a child theme is in use, false otherwise.
130 function is_child_theme() {
131 return ( TEMPLATEPATH !== STYLESHEETPATH );
135 * Retrieve name of the current stylesheet.
137 * The theme name that the administrator has currently set the front end theme
140 * For all intents and purposes, the template name and the stylesheet name are
141 * going to be the same for most cases.
145 * @return string Stylesheet name.
147 function get_stylesheet() {
149 * Filter the name of current stylesheet.
153 * @param string $stylesheet Name of the current stylesheet.
155 return apply_filters( 'stylesheet', get_option( 'stylesheet' ) );
159 * Retrieve stylesheet directory path for current theme.
163 * @return string Path to current theme directory.
165 function get_stylesheet_directory() {
166 $stylesheet = get_stylesheet();
167 $theme_root = get_theme_root( $stylesheet );
168 $stylesheet_dir = "$theme_root/$stylesheet";
171 * Filter the stylesheet directory path for current theme.
175 * @param string $stylesheet_dir Absolute path to the current them.
176 * @param string $stylesheet Directory name of the current theme.
177 * @param string $theme_root Absolute path to themes directory.
179 return apply_filters( 'stylesheet_directory', $stylesheet_dir, $stylesheet, $theme_root );
183 * Retrieve stylesheet directory URI.
189 function get_stylesheet_directory_uri() {
190 $stylesheet = str_replace( '%2F', '/', rawurlencode( get_stylesheet() ) );
191 $theme_root_uri = get_theme_root_uri( $stylesheet );
192 $stylesheet_dir_uri = "$theme_root_uri/$stylesheet";
195 * Filter the stylesheet directory URI.
199 * @param string $stylesheet_dir_uri Stylesheet directory URI.
200 * @param string $stylesheet Name of the activated theme's directory.
201 * @param string $theme_root_uri Themes root URI.
203 return apply_filters( 'stylesheet_directory_uri', $stylesheet_dir_uri, $stylesheet, $theme_root_uri );
207 * Retrieve URI of current theme stylesheet.
209 * The stylesheet file name is 'style.css' which is appended to {@link
210 * get_stylesheet_directory_uri() stylesheet directory URI} path.
216 function get_stylesheet_uri() {
217 $stylesheet_dir_uri = get_stylesheet_directory_uri();
218 $stylesheet_uri = $stylesheet_dir_uri . '/style.css';
220 * Filter the URI of the current theme stylesheet.
224 * @param string $stylesheet_uri Stylesheet URI for the current theme/child theme.
225 * @param string $stylesheet_dir_uri Stylesheet directory URI for the current theme/child theme.
227 return apply_filters( 'stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri );
231 * Retrieve localized stylesheet URI.
233 * The stylesheet directory for the localized stylesheet files are located, by
234 * default, in the base theme directory. The name of the locale file will be the
235 * locale followed by '.css'. If that does not exist, then the text direction
236 * stylesheet will be checked for existence, for example 'ltr.css'.
238 * The theme may change the location of the stylesheet directory by either using
239 * the 'stylesheet_directory_uri' filter or the 'locale_stylesheet_uri' filter.
240 * If you want to change the location of the stylesheet files for the entire
241 * WordPress workflow, then change the former. If you just have the locale in a
242 * separate folder, then change the latter.
248 function get_locale_stylesheet_uri() {
250 $stylesheet_dir_uri = get_stylesheet_directory_uri();
251 $dir = get_stylesheet_directory();
252 $locale = get_locale();
253 if ( file_exists("$dir/$locale.css") )
254 $stylesheet_uri = "$stylesheet_dir_uri/$locale.css";
255 elseif ( !empty($wp_locale->text_direction) && file_exists("$dir/{$wp_locale->text_direction}.css") )
256 $stylesheet_uri = "$stylesheet_dir_uri/{$wp_locale->text_direction}.css";
258 $stylesheet_uri = '';
260 * Filter the localized stylesheet URI.
264 * @param string $stylesheet_uri Localized stylesheet URI.
265 * @param string $stylesheet_dir_uri Stylesheet directory URI.
267 return apply_filters( 'locale_stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri );
271 * Retrieve name of the current theme.
275 * @return string Template name.
277 function get_template() {
279 * Filter the name of the current theme.
283 * @param string $template Current theme's directory name.
285 return apply_filters( 'template', get_option( 'template' ) );
289 * Retrieve current theme directory.
293 * @return string Template directory path.
295 function get_template_directory() {
296 $template = get_template();
297 $theme_root = get_theme_root( $template );
298 $template_dir = "$theme_root/$template";
301 * Filter the current theme directory path.
305 * @param string $template_dir The URI of the current theme directory.
306 * @param string $template Directory name of the current theme.
307 * @param string $theme_root Absolute path to the themes directory.
309 return apply_filters( 'template_directory', $template_dir, $template, $theme_root );
313 * Retrieve theme directory URI.
317 * @return string Template directory URI.
319 function get_template_directory_uri() {
320 $template = str_replace( '%2F', '/', rawurlencode( get_template() ) );
321 $theme_root_uri = get_theme_root_uri( $template );
322 $template_dir_uri = "$theme_root_uri/$template";
325 * Filter the current theme directory URI.
329 * @param string $template_dir_uri The URI of the current theme directory.
330 * @param string $template Directory name of the current theme.
331 * @param string $theme_root_uri The themes root URI.
333 return apply_filters( 'template_directory_uri', $template_dir_uri, $template, $theme_root_uri );
337 * Retrieve theme roots.
341 * @return array|string An array of theme roots keyed by template/stylesheet or a single theme root if all themes have the same root.
343 function get_theme_roots() {
344 global $wp_theme_directories;
346 if ( count($wp_theme_directories) <= 1 )
349 $theme_roots = get_site_transient( 'theme_roots' );
350 if ( false === $theme_roots ) {
351 search_theme_directories( true ); // Regenerate the transient.
352 $theme_roots = get_site_transient( 'theme_roots' );
358 * Register a directory that contains themes.
362 * @param string $directory Either the full filesystem path to a theme folder or a folder within WP_CONTENT_DIR
365 function register_theme_directory( $directory ) {
366 global $wp_theme_directories;
368 if ( ! file_exists( $directory ) ) {
369 // Try prepending as the theme directory could be relative to the content directory
370 $directory = WP_CONTENT_DIR . '/' . $directory;
371 // If this directory does not exist, return and do not register
372 if ( ! file_exists( $directory ) ) {
377 if ( ! is_array( $wp_theme_directories ) ) {
378 $wp_theme_directories = array();
381 $untrailed = untrailingslashit( $directory );
382 if ( ! empty( $untrailed ) && ! in_array( $untrailed, $wp_theme_directories ) ) {
383 $wp_theme_directories[] = $untrailed;
390 * Search all registered theme directories for complete and valid themes.
394 * @param bool $force Optional. Whether to force a new directory scan. Defaults to false.
395 * @return array Valid themes found
397 function search_theme_directories( $force = false ) {
398 global $wp_theme_directories;
399 if ( empty( $wp_theme_directories ) )
402 static $found_themes;
403 if ( ! $force && isset( $found_themes ) )
404 return $found_themes;
406 $found_themes = array();
408 $wp_theme_directories = (array) $wp_theme_directories;
410 // Set up maybe-relative, maybe-absolute array of theme directories.
411 // We always want to return absolute, but we need to cache relative
412 // to use in get_theme_root().
413 foreach ( $wp_theme_directories as $theme_root ) {
414 if ( 0 === strpos( $theme_root, WP_CONTENT_DIR ) )
415 $relative_theme_roots[ str_replace( WP_CONTENT_DIR, '', $theme_root ) ] = $theme_root;
417 $relative_theme_roots[ $theme_root ] = $theme_root;
421 * Filter whether to get the cache of the registered theme directories.
425 * @param bool $cache_expiration Whether to get the cache of the theme directories. Default false.
426 * @param string $cache_directory Directory to be searched for the cache.
428 if ( $cache_expiration = apply_filters( 'wp_cache_themes_persistently', false, 'search_theme_directories' ) ) {
429 $cached_roots = get_site_transient( 'theme_roots' );
430 if ( is_array( $cached_roots ) ) {
431 foreach ( $cached_roots as $theme_dir => $theme_root ) {
432 // A cached theme root is no longer around, so skip it.
433 if ( ! isset( $relative_theme_roots[ $theme_root ] ) )
435 $found_themes[ $theme_dir ] = array(
436 'theme_file' => $theme_dir . '/style.css',
437 'theme_root' => $relative_theme_roots[ $theme_root ], // Convert relative to absolute.
440 return $found_themes;
442 if ( ! is_int( $cache_expiration ) )
443 $cache_expiration = 1800; // half hour
445 $cache_expiration = 1800; // half hour
448 /* Loop the registered theme directories and extract all themes */
449 foreach ( $wp_theme_directories as $theme_root ) {
451 // Start with directories in the root of the current theme directory.
452 $dirs = @ scandir( $theme_root );
454 trigger_error( "$theme_root is not readable", E_USER_NOTICE );
457 foreach ( $dirs as $dir ) {
458 if ( ! is_dir( $theme_root . '/' . $dir ) || $dir[0] == '.' || $dir == 'CVS' )
460 if ( file_exists( $theme_root . '/' . $dir . '/style.css' ) ) {
461 // wp-content/themes/a-single-theme
462 // wp-content/themes is $theme_root, a-single-theme is $dir
463 $found_themes[ $dir ] = array(
464 'theme_file' => $dir . '/style.css',
465 'theme_root' => $theme_root,
468 $found_theme = false;
469 // wp-content/themes/a-folder-of-themes/*
470 // wp-content/themes is $theme_root, a-folder-of-themes is $dir, then themes are $sub_dirs
471 $sub_dirs = @ scandir( $theme_root . '/' . $dir );
473 trigger_error( "$theme_root/$dir is not readable", E_USER_NOTICE );
476 foreach ( $sub_dirs as $sub_dir ) {
477 if ( ! is_dir( $theme_root . '/' . $dir . '/' . $sub_dir ) || $dir[0] == '.' || $dir == 'CVS' )
479 if ( ! file_exists( $theme_root . '/' . $dir . '/' . $sub_dir . '/style.css' ) )
481 $found_themes[ $dir . '/' . $sub_dir ] = array(
482 'theme_file' => $dir . '/' . $sub_dir . '/style.css',
483 'theme_root' => $theme_root,
487 // Never mind the above, it's just a theme missing a style.css.
488 // Return it; WP_Theme will catch the error.
489 if ( ! $found_theme )
490 $found_themes[ $dir ] = array(
491 'theme_file' => $dir . '/style.css',
492 'theme_root' => $theme_root,
498 asort( $found_themes );
500 $theme_roots = array();
501 $relative_theme_roots = array_flip( $relative_theme_roots );
503 foreach ( $found_themes as $theme_dir => $theme_data ) {
504 $theme_roots[ $theme_dir ] = $relative_theme_roots[ $theme_data['theme_root'] ]; // Convert absolute to relative.
507 if ( $theme_roots != get_site_transient( 'theme_roots' ) )
508 set_site_transient( 'theme_roots', $theme_roots, $cache_expiration );
510 return $found_themes;
514 * Retrieve path to themes directory.
516 * Does not have trailing slash.
520 * @param string $stylesheet_or_template The stylesheet or template name of the theme
521 * @return string Theme path.
523 function get_theme_root( $stylesheet_or_template = false ) {
524 global $wp_theme_directories;
526 if ( $stylesheet_or_template && $theme_root = get_raw_theme_root( $stylesheet_or_template ) ) {
527 // Always prepend WP_CONTENT_DIR unless the root currently registered as a theme directory.
528 // This gives relative theme roots the benefit of the doubt when things go haywire.
529 if ( ! in_array( $theme_root, (array) $wp_theme_directories ) )
530 $theme_root = WP_CONTENT_DIR . $theme_root;
532 $theme_root = WP_CONTENT_DIR . '/themes';
536 * Filter the absolute path to the themes directory.
540 * @param string $theme_root Absolute path to themes directory.
542 return apply_filters( 'theme_root', $theme_root );
546 * Retrieve URI for themes directory.
548 * Does not have trailing slash.
552 * @param string $stylesheet_or_template Optional. The stylesheet or template name of the theme.
553 * Default is to leverage the main theme root.
554 * @param string $theme_root Optional. The theme root for which calculations will be based, preventing
555 * the need for a get_raw_theme_root() call.
556 * @return string Themes URI.
558 function get_theme_root_uri( $stylesheet_or_template = false, $theme_root = false ) {
559 global $wp_theme_directories;
561 if ( $stylesheet_or_template && ! $theme_root )
562 $theme_root = get_raw_theme_root( $stylesheet_or_template );
564 if ( $stylesheet_or_template && $theme_root ) {
565 if ( in_array( $theme_root, (array) $wp_theme_directories ) ) {
566 // Absolute path. Make an educated guess. YMMV -- but note the filter below.
567 if ( 0 === strpos( $theme_root, WP_CONTENT_DIR ) )
568 $theme_root_uri = content_url( str_replace( WP_CONTENT_DIR, '', $theme_root ) );
569 elseif ( 0 === strpos( $theme_root, ABSPATH ) )
570 $theme_root_uri = site_url( str_replace( ABSPATH, '', $theme_root ) );
571 elseif ( 0 === strpos( $theme_root, WP_PLUGIN_DIR ) || 0 === strpos( $theme_root, WPMU_PLUGIN_DIR ) )
572 $theme_root_uri = plugins_url( basename( $theme_root ), $theme_root );
574 $theme_root_uri = $theme_root;
576 $theme_root_uri = content_url( $theme_root );
579 $theme_root_uri = content_url( 'themes' );
583 * Filter the URI for themes directory.
587 * @param string $theme_root_uri The URI for themes directory.
588 * @param string $siteurl WordPress web address which is set in General Options.
589 * @param string $stylesheet_or_template Stylesheet or template name of the theme.
591 return apply_filters( 'theme_root_uri', $theme_root_uri, get_option( 'siteurl' ), $stylesheet_or_template );
595 * Get the raw theme root relative to the content directory with no filters applied.
599 * @param string $stylesheet_or_template The stylesheet or template name of the theme
600 * @param bool $skip_cache Optional. Whether to skip the cache. Defaults to false, meaning the cache is used.
601 * @return string Theme root
603 function get_raw_theme_root( $stylesheet_or_template, $skip_cache = false ) {
604 global $wp_theme_directories;
606 if ( count($wp_theme_directories) <= 1 )
611 // If requesting the root for the current theme, consult options to avoid calling get_theme_roots()
612 if ( ! $skip_cache ) {
613 if ( get_option('stylesheet') == $stylesheet_or_template )
614 $theme_root = get_option('stylesheet_root');
615 elseif ( get_option('template') == $stylesheet_or_template )
616 $theme_root = get_option('template_root');
619 if ( empty($theme_root) ) {
620 $theme_roots = get_theme_roots();
621 if ( !empty($theme_roots[$stylesheet_or_template]) )
622 $theme_root = $theme_roots[$stylesheet_or_template];
629 * Display localized stylesheet link element.
633 function locale_stylesheet() {
634 $stylesheet = get_locale_stylesheet_uri();
635 if ( empty($stylesheet) )
637 echo '<link rel="stylesheet" href="' . $stylesheet . '" type="text/css" media="screen" />';
641 * Start preview theme output buffer.
643 * Will only perform task if the user has permissions and template and preview
644 * query variables exist.
648 function preview_theme() {
649 if ( ! (isset($_GET['template']) && isset($_GET['preview'])) )
652 if ( !current_user_can( 'switch_themes' ) )
655 // Admin Thickbox requests
656 if ( isset( $_GET['preview_iframe'] ) )
657 show_admin_bar( false );
659 $_GET['template'] = preg_replace('|[^a-z0-9_./-]|i', '', $_GET['template']);
661 if ( validate_file($_GET['template']) )
664 add_filter( 'template', '_preview_theme_template_filter' );
666 if ( isset($_GET['stylesheet']) ) {
667 $_GET['stylesheet'] = preg_replace('|[^a-z0-9_./-]|i', '', $_GET['stylesheet']);
668 if ( validate_file($_GET['stylesheet']) )
670 add_filter( 'stylesheet', '_preview_theme_stylesheet_filter' );
673 // Prevent theme mods to current theme being used on theme being previewed
674 add_filter( 'pre_option_theme_mods_' . get_option( 'stylesheet' ), '__return_empty_array' );
676 ob_start( 'preview_theme_ob_filter' );
678 add_action('setup_theme', 'preview_theme');
681 * Private function to modify the current template when previewing a theme
688 function _preview_theme_template_filter() {
689 return isset($_GET['template']) ? $_GET['template'] : '';
693 * Private function to modify the current stylesheet when previewing a theme
700 function _preview_theme_stylesheet_filter() {
701 return isset($_GET['stylesheet']) ? $_GET['stylesheet'] : '';
705 * Callback function for ob_start() to capture all links in the theme.
710 * @param string $content
713 function preview_theme_ob_filter( $content ) {
714 return preg_replace_callback( "|(<a.*?href=([\"']))(.*?)([\"'].*?>)|", 'preview_theme_ob_filter_callback', $content );
718 * Manipulates preview theme links in order to control and maintain location.
720 * Callback function for preg_replace_callback() to accept and filter matches.
725 * @param array $matches
728 function preview_theme_ob_filter_callback( $matches ) {
729 if ( strpos($matches[4], 'onclick') !== false )
730 $matches[4] = preg_replace('#onclick=([\'"]).*?(?<!\\\)\\1#i', '', $matches[4]); //Strip out any onclicks from rest of <a>. (?<!\\\) means to ignore the '" if it's escaped by \ to prevent breaking mid-attribute.
732 ( false !== strpos($matches[3], '/wp-admin/') )
734 ( false !== strpos( $matches[3], '://' ) && 0 !== strpos( $matches[3], home_url() ) )
736 ( false !== strpos($matches[3], '/feed/') )
738 ( false !== strpos($matches[3], '/trackback/') )
740 return $matches[1] . "#$matches[2] onclick=$matches[2]return false;" . $matches[4];
742 $stylesheet = isset( $_GET['stylesheet'] ) ? $_GET['stylesheet'] : '';
743 $template = isset( $_GET['template'] ) ? $_GET['template'] : '';
745 $link = add_query_arg( array( 'preview' => 1, 'template' => $template, 'stylesheet' => $stylesheet, 'preview_iframe' => 1 ), $matches[3] );
746 if ( 0 === strpos($link, 'preview=1') )
748 return $matches[1] . esc_attr( $link ) . $matches[4];
752 * Switches the theme.
754 * Accepts one argument: $stylesheet of the theme. It also accepts an additional function signature
755 * of two arguments: $template then $stylesheet. This is for backwards compatibility.
759 * @param string $stylesheet Stylesheet name
761 function switch_theme( $stylesheet ) {
762 global $wp_theme_directories, $wp_customize, $sidebars_widgets;
764 $_sidebars_widgets = null;
765 if ( 'wp_ajax_customize_save' === current_action() ) {
766 $_sidebars_widgets = $wp_customize->post_value( $wp_customize->get_setting( 'old_sidebars_widgets_data' ) );
767 } elseif ( is_array( $sidebars_widgets ) ) {
768 $_sidebars_widgets = $sidebars_widgets;
771 if ( is_array( $_sidebars_widgets ) ) {
772 set_theme_mod( 'sidebars_widgets', array( 'time' => time(), 'data' => $_sidebars_widgets ) );
775 $old_theme = wp_get_theme();
776 $new_theme = wp_get_theme( $stylesheet );
778 if ( func_num_args() > 1 ) {
779 $template = $stylesheet;
780 $stylesheet = func_get_arg( 1 );
782 $template = $new_theme->get_template();
785 update_option( 'template', $template );
786 update_option( 'stylesheet', $stylesheet );
788 if ( count( $wp_theme_directories ) > 1 ) {
789 update_option( 'template_root', get_raw_theme_root( $template, true ) );
790 update_option( 'stylesheet_root', get_raw_theme_root( $stylesheet, true ) );
792 delete_option( 'template_root' );
793 delete_option( 'stylesheet_root' );
796 $new_name = $new_theme->get('Name');
798 update_option( 'current_theme', $new_name );
800 // Migrate from the old mods_{name} option to theme_mods_{slug}.
801 if ( is_admin() && false === get_option( 'theme_mods_' . $stylesheet ) ) {
802 $default_theme_mods = (array) get_option( 'mods_' . $new_name );
803 add_option( "theme_mods_$stylesheet", $default_theme_mods );
806 * Since retrieve_widgets() is called when initializing a theme in the Customizer,
807 * we need to to remove the theme mods to avoid overwriting changes made via
808 * the Customizer when accessing wp-admin/widgets.php.
810 if ( 'wp_ajax_customize_save' === current_action() ) {
811 remove_theme_mod( 'sidebars_widgets' );
815 update_option( 'theme_switched', $old_theme->get_stylesheet() );
817 * Fires after the theme is switched.
821 * @param string $new_name Name of the new theme.
822 * @param WP_Theme $new_theme WP_Theme instance of the new theme.
824 do_action( 'switch_theme', $new_name, $new_theme );
828 * Checks that current theme files 'index.php' and 'style.css' exists.
830 * Does not check the default theme, which is the fallback and should always exist.
831 * Will switch theme to the fallback theme if current theme does not validate.
832 * You can use the 'validate_current_theme' filter to return false to
833 * disable this functionality.
836 * @see WP_DEFAULT_THEME
840 function validate_current_theme() {
842 * Filter whether to validate the current theme.
846 * @param bool true Validation flag to check the current theme.
848 if ( defined('WP_INSTALLING') || ! apply_filters( 'validate_current_theme', true ) )
851 if ( get_template() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/index.php') ) {
852 switch_theme( WP_DEFAULT_THEME );
856 if ( get_stylesheet() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/style.css') ) {
857 switch_theme( WP_DEFAULT_THEME );
861 if ( is_child_theme() && ! file_exists( get_stylesheet_directory() . '/style.css' ) ) {
862 switch_theme( WP_DEFAULT_THEME );
870 * Retrieve all theme modifications.
874 * @return array|null Theme modifications.
876 function get_theme_mods() {
877 $theme_slug = get_option( 'stylesheet' );
878 if ( false === ( $mods = get_option( "theme_mods_$theme_slug" ) ) ) {
879 $theme_name = get_option( 'current_theme' );
880 if ( false === $theme_name )
881 $theme_name = wp_get_theme()->get('Name');
882 $mods = get_option( "mods_$theme_name" ); // Deprecated location.
883 if ( is_admin() && false !== $mods ) {
884 update_option( "theme_mods_$theme_slug", $mods );
885 delete_option( "mods_$theme_name" );
892 * Retrieve theme modification value for the current theme.
894 * If the modification name does not exist, then the $default will be passed
895 * through {@link http://php.net/sprintf sprintf()} PHP function with the first
896 * string the template directory URI and the second string the stylesheet
901 * @param string $name Theme modification name.
902 * @param bool|string $default
905 function get_theme_mod( $name, $default = false ) {
906 $mods = get_theme_mods();
908 if ( isset( $mods[$name] ) ) {
910 * Filter the theme modification, or 'theme_mod', value.
912 * The dynamic portion of the hook name, `$name`, refers to
913 * the key name of the modification array. For example,
914 * 'header_textcolor', 'header_image', and so on depending
915 * on the theme options.
919 * @param string $current_mod The value of the current theme modification.
921 return apply_filters( "theme_mod_{$name}", $mods[$name] );
924 if ( is_string( $default ) )
925 $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
927 /** This filter is documented in wp-includes/theme.php */
928 return apply_filters( "theme_mod_{$name}", $default );
932 * Update theme modification value for the current theme.
936 * @param string $name Theme modification name.
937 * @param string $value theme modification value.
939 function set_theme_mod( $name, $value ) {
940 $mods = get_theme_mods();
941 $old_value = isset( $mods[ $name ] ) ? $mods[ $name ] : false;
944 * Filter the theme mod value on save.
946 * The dynamic portion of the hook name, `$name`, refers to the key name of
947 * the modification array. For example, 'header_textcolor', 'header_image',
948 * and so on depending on the theme options.
952 * @param string $value The new value of the theme mod.
953 * @param string $old_value The current value of the theme mod.
955 $mods[ $name ] = apply_filters( "pre_set_theme_mod_$name", $value, $old_value );
957 $theme = get_option( 'stylesheet' );
958 update_option( "theme_mods_$theme", $mods );
962 * Remove theme modification name from current theme list.
964 * If removing the name also removes all elements, then the entire option will
969 * @param string $name Theme modification name.
972 function remove_theme_mod( $name ) {
973 $mods = get_theme_mods();
975 if ( ! isset( $mods[ $name ] ) )
978 unset( $mods[ $name ] );
980 if ( empty( $mods ) )
981 return remove_theme_mods();
983 $theme = get_option( 'stylesheet' );
984 update_option( "theme_mods_$theme", $mods );
988 * Remove theme modifications option for current theme.
992 function remove_theme_mods() {
993 delete_option( 'theme_mods_' . get_option( 'stylesheet' ) );
996 $theme_name = get_option( 'current_theme' );
997 if ( false === $theme_name )
998 $theme_name = wp_get_theme()->get('Name');
999 delete_option( 'mods_' . $theme_name );
1003 * Retrieve text color for custom header.
1009 function get_header_textcolor() {
1010 return get_theme_mod('header_textcolor', get_theme_support( 'custom-header', 'default-text-color' ) );
1014 * Display text color for custom header.
1018 function header_textcolor() {
1019 echo get_header_textcolor();
1023 * Whether to display the header text.
1029 function display_header_text() {
1030 if ( ! current_theme_supports( 'custom-header', 'header-text' ) )
1033 $text_color = get_theme_mod( 'header_textcolor', get_theme_support( 'custom-header', 'default-text-color' ) );
1034 return 'blank' != $text_color;
1038 * Retrieve header image for custom header.
1044 function get_header_image() {
1045 $url = get_theme_mod( 'header_image', get_theme_support( 'custom-header', 'default-image' ) );
1047 if ( 'remove-header' == $url )
1050 if ( is_random_header_image() )
1051 $url = get_random_header_image();
1053 return esc_url_raw( set_url_scheme( $url ) );
1057 * Get random header image data from registered images in theme.
1063 * @return string Path to header image
1066 function _get_random_header_data() {
1067 static $_wp_random_header;
1069 if ( empty( $_wp_random_header ) ) {
1070 global $_wp_default_headers;
1071 $header_image_mod = get_theme_mod( 'header_image', '' );
1074 if ( 'random-uploaded-image' == $header_image_mod )
1075 $headers = get_uploaded_header_images();
1076 elseif ( ! empty( $_wp_default_headers ) ) {
1077 if ( 'random-default-image' == $header_image_mod ) {
1078 $headers = $_wp_default_headers;
1080 if ( current_theme_supports( 'custom-header', 'random-default' ) )
1081 $headers = $_wp_default_headers;
1085 if ( empty( $headers ) )
1086 return new stdClass;
1088 $_wp_random_header = (object) $headers[ array_rand( $headers ) ];
1090 $_wp_random_header->url = sprintf( $_wp_random_header->url, get_template_directory_uri(), get_stylesheet_directory_uri() );
1091 $_wp_random_header->thumbnail_url = sprintf( $_wp_random_header->thumbnail_url, get_template_directory_uri(), get_stylesheet_directory_uri() );
1093 return $_wp_random_header;
1097 * Get random header image url from registered images in theme.
1101 * @return string Path to header image
1104 function get_random_header_image() {
1105 $random_image = _get_random_header_data();
1106 if ( empty( $random_image->url ) )
1108 return $random_image->url;
1112 * Check if random header image is in use.
1114 * Always true if user expressly chooses the option in Appearance > Header.
1115 * Also true if theme has multiple header images registered, no specific header image
1116 * is chosen, and theme turns on random headers with add_theme_support().
1120 * @param string $type The random pool to use. any|default|uploaded
1123 function is_random_header_image( $type = 'any' ) {
1124 $header_image_mod = get_theme_mod( 'header_image', get_theme_support( 'custom-header', 'default-image' ) );
1126 if ( 'any' == $type ) {
1127 if ( 'random-default-image' == $header_image_mod || 'random-uploaded-image' == $header_image_mod || ( '' != get_random_header_image() && empty( $header_image_mod ) ) )
1130 if ( "random-$type-image" == $header_image_mod )
1132 elseif ( 'default' == $type && empty( $header_image_mod ) && '' != get_random_header_image() )
1140 * Display header image URL.
1144 function header_image() {
1145 echo esc_url( get_header_image() );
1149 * Get the header images uploaded for the current theme.
1155 function get_uploaded_header_images() {
1156 $header_images = array();
1159 $headers = get_posts( array( 'post_type' => 'attachment', 'meta_key' => '_wp_attachment_is_custom_header', 'meta_value' => get_option('stylesheet'), 'orderby' => 'none', 'nopaging' => true ) );
1161 if ( empty( $headers ) )
1164 foreach ( (array) $headers as $header ) {
1165 $url = esc_url_raw( wp_get_attachment_url( $header->ID ) );
1166 $header_data = wp_get_attachment_metadata( $header->ID );
1167 $header_index = basename($url);
1168 $header_images[$header_index] = array();
1169 $header_images[$header_index]['attachment_id'] = $header->ID;
1170 $header_images[$header_index]['url'] = $url;
1171 $header_images[$header_index]['thumbnail_url'] = $url;
1172 if ( isset( $header_data['width'] ) )
1173 $header_images[$header_index]['width'] = $header_data['width'];
1174 if ( isset( $header_data['height'] ) )
1175 $header_images[$header_index]['height'] = $header_data['height'];
1178 return $header_images;
1182 * Get the header image data.
1188 function get_custom_header() {
1189 global $_wp_default_headers;
1191 if ( is_random_header_image() ) {
1192 $data = _get_random_header_data();
1194 $data = get_theme_mod( 'header_image_data' );
1195 if ( ! $data && current_theme_supports( 'custom-header', 'default-image' ) ) {
1196 $directory_args = array( get_template_directory_uri(), get_stylesheet_directory_uri() );
1198 $data['url'] = $data['thumbnail_url'] = vsprintf( get_theme_support( 'custom-header', 'default-image' ), $directory_args );
1199 if ( ! empty( $_wp_default_headers ) ) {
1200 foreach ( (array) $_wp_default_headers as $default_header ) {
1201 $url = vsprintf( $default_header['url'], $directory_args );
1202 if ( $data['url'] == $url ) {
1203 $data = $default_header;
1204 $data['url'] = $url;
1205 $data['thumbnail_url'] = vsprintf( $data['thumbnail_url'], $directory_args );
1215 'thumbnail_url' => '',
1216 'width' => get_theme_support( 'custom-header', 'width' ),
1217 'height' => get_theme_support( 'custom-header', 'height' ),
1219 return (object) wp_parse_args( $data, $default );
1223 * Register a selection of default headers to be displayed by the custom header admin UI.
1227 * @param array $headers Array of headers keyed by a string id. The ids point to arrays containing 'url', 'thumbnail_url', and 'description' keys.
1229 function register_default_headers( $headers ) {
1230 global $_wp_default_headers;
1232 $_wp_default_headers = array_merge( (array) $_wp_default_headers, (array) $headers );
1236 * Unregister default headers.
1238 * This function must be called after register_default_headers() has already added the
1239 * header you want to remove.
1241 * @see register_default_headers()
1244 * @param string|array $header The header string id (key of array) to remove, or an array thereof.
1245 * @return bool|void A single header returns true on success, false on failure.
1246 * There is currently no return value for multiple headers.
1248 function unregister_default_headers( $header ) {
1249 global $_wp_default_headers;
1250 if ( is_array( $header ) ) {
1251 array_map( 'unregister_default_headers', $header );
1252 } elseif ( isset( $_wp_default_headers[ $header ] ) ) {
1253 unset( $_wp_default_headers[ $header ] );
1261 * Retrieve background image for custom background.
1267 function get_background_image() {
1268 return get_theme_mod('background_image', get_theme_support( 'custom-background', 'default-image' ) );
1272 * Display background image path.
1276 function background_image() {
1277 echo get_background_image();
1281 * Retrieve value for custom background color.
1287 function get_background_color() {
1288 return get_theme_mod('background_color', get_theme_support( 'custom-background', 'default-color' ) );
1292 * Display background color value.
1296 function background_color() {
1297 echo get_background_color();
1301 * Default custom background callback.
1306 function _custom_background_cb() {
1307 // $background is the saved custom image, or the default image.
1308 $background = set_url_scheme( get_background_image() );
1310 // $color is the saved custom color.
1311 // A default has to be specified in style.css. It will not be printed here.
1312 $color = get_background_color();
1314 if ( $color === get_theme_support( 'custom-background', 'default-color' ) ) {
1318 if ( ! $background && ! $color )
1321 $style = $color ? "background-color: #$color;" : '';
1323 if ( $background ) {
1324 $image = " background-image: url('$background');";
1326 $repeat = get_theme_mod( 'background_repeat', get_theme_support( 'custom-background', 'default-repeat' ) );
1327 if ( ! in_array( $repeat, array( 'no-repeat', 'repeat-x', 'repeat-y', 'repeat' ) ) )
1329 $repeat = " background-repeat: $repeat;";
1331 $position = get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) );
1332 if ( ! in_array( $position, array( 'center', 'right', 'left' ) ) )
1334 $position = " background-position: top $position;";
1336 $attachment = get_theme_mod( 'background_attachment', get_theme_support( 'custom-background', 'default-attachment' ) );
1337 if ( ! in_array( $attachment, array( 'fixed', 'scroll' ) ) )
1338 $attachment = 'scroll';
1339 $attachment = " background-attachment: $attachment;";
1341 $style .= $image . $repeat . $position . $attachment;
1344 <style type="text/css" id="custom-background-css">
1345 body.custom-background { <?php echo trim( $style ); ?> }
1351 * Add callback for custom TinyMCE editor stylesheets.
1353 * The parameter $stylesheet is the name of the stylesheet, relative to
1354 * the theme root. It also accepts an array of stylesheets.
1355 * It is optional and defaults to 'editor-style.css'.
1357 * This function automatically adds another stylesheet with -rtl prefix, e.g. editor-style-rtl.css.
1358 * If that file doesn't exist, it is removed before adding the stylesheet(s) to TinyMCE.
1359 * If an array of stylesheets is passed to add_editor_style(),
1360 * RTL is only added for the first stylesheet.
1362 * Since version 3.4 the TinyMCE body has .rtl CSS class.
1363 * It is a better option to use that class and add any RTL styles to the main stylesheet.
1367 * @param array|string $stylesheet Optional. Stylesheet name or array thereof, relative to theme root.
1368 * Defaults to 'editor-style.css'
1370 function add_editor_style( $stylesheet = 'editor-style.css' ) {
1372 add_theme_support( 'editor-style' );
1377 global $editor_styles;
1378 $editor_styles = (array) $editor_styles;
1379 $stylesheet = (array) $stylesheet;
1381 $rtl_stylesheet = str_replace('.css', '-rtl.css', $stylesheet[0]);
1382 $stylesheet[] = $rtl_stylesheet;
1385 $editor_styles = array_merge( $editor_styles, $stylesheet );
1389 * Removes all visual editor stylesheets.
1393 * @return bool True on success, false if there were no stylesheets to remove.
1395 function remove_editor_styles() {
1396 if ( ! current_theme_supports( 'editor-style' ) )
1398 _remove_theme_support( 'editor-style' );
1400 $GLOBALS['editor_styles'] = array();
1405 * Retrieve any registered editor stylesheets
1409 * @global $editor_styles Registered editor stylesheets
1411 * @return array If registered, a list of editor stylesheet URLs.
1413 function get_editor_stylesheets() {
1414 $stylesheets = array();
1415 // load editor_style.css if the current theme supports it
1416 if ( ! empty( $GLOBALS['editor_styles'] ) && is_array( $GLOBALS['editor_styles'] ) ) {
1417 $editor_styles = $GLOBALS['editor_styles'];
1419 $editor_styles = array_unique( array_filter( $editor_styles ) );
1420 $style_uri = get_stylesheet_directory_uri();
1421 $style_dir = get_stylesheet_directory();
1423 // Support externally referenced styles (like, say, fonts).
1424 foreach ( $editor_styles as $key => $file ) {
1425 if ( preg_match( '~^(https?:)?//~', $file ) ) {
1426 $stylesheets[] = esc_url_raw( $file );
1427 unset( $editor_styles[ $key ] );
1431 // Look in a parent theme first, that way child theme CSS overrides.
1432 if ( is_child_theme() ) {
1433 $template_uri = get_template_directory_uri();
1434 $template_dir = get_template_directory();
1436 foreach ( $editor_styles as $key => $file ) {
1437 if ( $file && file_exists( "$template_dir/$file" ) ) {
1438 $stylesheets[] = "$template_uri/$file";
1443 foreach ( $editor_styles as $file ) {
1444 if ( $file && file_exists( "$style_dir/$file" ) ) {
1445 $stylesheets[] = "$style_uri/$file";
1449 return $stylesheets;
1453 * Allows a theme to register its support of a certain feature
1455 * Must be called in the theme's functions.php file to work.
1456 * If attached to a hook, it must be after_setup_theme.
1457 * The init hook may be too late for some features.
1461 * @param string $feature The feature being added.
1462 * @return void|bool False on failure, void otherwise.
1464 function add_theme_support( $feature ) {
1465 global $_wp_theme_features;
1467 if ( func_num_args() == 1 )
1470 $args = array_slice( func_get_args(), 1 );
1472 switch ( $feature ) {
1473 case 'post-formats' :
1474 if ( is_array( $args[0] ) ) {
1475 $post_formats = get_post_format_slugs();
1476 unset( $post_formats['standard'] );
1478 $args[0] = array_intersect( $args[0], array_keys( $post_formats ) );
1483 // You can't just pass 'html5', you need to pass an array of types.
1484 if ( empty( $args[0] ) ) {
1485 // Build an array of types for back-compat.
1486 $args = array( 0 => array( 'comment-list', 'comment-form', 'search-form' ) );
1487 } elseif ( ! is_array( $args[0] ) ) {
1488 _doing_it_wrong( "add_theme_support( 'html5' )", __( 'You need to pass an array of types.' ), '3.6.1' );
1492 // Calling 'html5' again merges, rather than overwrites.
1493 if ( isset( $_wp_theme_features['html5'] ) )
1494 $args[0] = array_merge( $_wp_theme_features['html5'][0], $args[0] );
1497 case 'custom-header-uploads' :
1498 return add_theme_support( 'custom-header', array( 'uploads' => true ) );
1500 case 'custom-header' :
1501 if ( ! is_array( $args ) )
1502 $args = array( 0 => array() );
1505 'default-image' => '',
1506 'random-default' => false,
1509 'flex-height' => false,
1510 'flex-width' => false,
1511 'default-text-color' => '',
1512 'header-text' => true,
1514 'wp-head-callback' => '',
1515 'admin-head-callback' => '',
1516 'admin-preview-callback' => '',
1519 $jit = isset( $args[0]['__jit'] );
1520 unset( $args[0]['__jit'] );
1522 // Merge in data from previous add_theme_support() calls.
1523 // The first value registered wins. (A child theme is set up first.)
1524 if ( isset( $_wp_theme_features['custom-header'] ) )
1525 $args[0] = wp_parse_args( $_wp_theme_features['custom-header'][0], $args[0] );
1527 // Load in the defaults at the end, as we need to insure first one wins.
1528 // This will cause all constants to be defined, as each arg will then be set to the default.
1530 $args[0] = wp_parse_args( $args[0], $defaults );
1532 // If a constant was defined, use that value. Otherwise, define the constant to ensure
1533 // the constant is always accurate (and is not defined later, overriding our value).
1534 // As stated above, the first value wins.
1535 // Once we get to wp_loaded (just-in-time), define any constants we haven't already.
1536 // Constants are lame. Don't reference them. This is just for backwards compatibility.
1538 if ( defined( 'NO_HEADER_TEXT' ) )
1539 $args[0]['header-text'] = ! NO_HEADER_TEXT;
1540 elseif ( isset( $args[0]['header-text'] ) )
1541 define( 'NO_HEADER_TEXT', empty( $args[0]['header-text'] ) );
1543 if ( defined( 'HEADER_IMAGE_WIDTH' ) )
1544 $args[0]['width'] = (int) HEADER_IMAGE_WIDTH;
1545 elseif ( isset( $args[0]['width'] ) )
1546 define( 'HEADER_IMAGE_WIDTH', (int) $args[0]['width'] );
1548 if ( defined( 'HEADER_IMAGE_HEIGHT' ) )
1549 $args[0]['height'] = (int) HEADER_IMAGE_HEIGHT;
1550 elseif ( isset( $args[0]['height'] ) )
1551 define( 'HEADER_IMAGE_HEIGHT', (int) $args[0]['height'] );
1553 if ( defined( 'HEADER_TEXTCOLOR' ) )
1554 $args[0]['default-text-color'] = HEADER_TEXTCOLOR;
1555 elseif ( isset( $args[0]['default-text-color'] ) )
1556 define( 'HEADER_TEXTCOLOR', $args[0]['default-text-color'] );
1558 if ( defined( 'HEADER_IMAGE' ) )
1559 $args[0]['default-image'] = HEADER_IMAGE;
1560 elseif ( isset( $args[0]['default-image'] ) )
1561 define( 'HEADER_IMAGE', $args[0]['default-image'] );
1563 if ( $jit && ! empty( $args[0]['default-image'] ) )
1564 $args[0]['random-default'] = false;
1566 // If headers are supported, and we still don't have a defined width or height,
1567 // we have implicit flex sizes.
1569 if ( empty( $args[0]['width'] ) && empty( $args[0]['flex-width'] ) )
1570 $args[0]['flex-width'] = true;
1571 if ( empty( $args[0]['height'] ) && empty( $args[0]['flex-height'] ) )
1572 $args[0]['flex-height'] = true;
1577 case 'custom-background' :
1578 if ( ! is_array( $args ) )
1579 $args = array( 0 => array() );
1582 'default-image' => '',
1583 'default-repeat' => 'repeat',
1584 'default-position-x' => 'left',
1585 'default-attachment' => 'scroll',
1586 'default-color' => '',
1587 'wp-head-callback' => '_custom_background_cb',
1588 'admin-head-callback' => '',
1589 'admin-preview-callback' => '',
1592 $jit = isset( $args[0]['__jit'] );
1593 unset( $args[0]['__jit'] );
1595 // Merge in data from previous add_theme_support() calls. The first value registered wins.
1596 if ( isset( $_wp_theme_features['custom-background'] ) )
1597 $args[0] = wp_parse_args( $_wp_theme_features['custom-background'][0], $args[0] );
1600 $args[0] = wp_parse_args( $args[0], $defaults );
1602 if ( defined( 'BACKGROUND_COLOR' ) )
1603 $args[0]['default-color'] = BACKGROUND_COLOR;
1604 elseif ( isset( $args[0]['default-color'] ) || $jit )
1605 define( 'BACKGROUND_COLOR', $args[0]['default-color'] );
1607 if ( defined( 'BACKGROUND_IMAGE' ) )
1608 $args[0]['default-image'] = BACKGROUND_IMAGE;
1609 elseif ( isset( $args[0]['default-image'] ) || $jit )
1610 define( 'BACKGROUND_IMAGE', $args[0]['default-image'] );
1614 // Ensure that 'title-tag' is accessible in the admin.
1616 // Can be called in functions.php but must happen before wp_loaded, i.e. not in header.php.
1617 if ( did_action( 'wp_loaded' ) ) {
1618 /* translators: 1: Theme support 2: hook name */
1619 _doing_it_wrong( "add_theme_support( 'title-tag' )", sprintf( __( 'Theme support for %1$s should be registered before the %2$s hook.' ),
1620 '<code>title-tag</code>', '<code>wp_loaded</code>' ), '4.1' );
1626 $_wp_theme_features[ $feature ] = $args;
1630 * Registers the internal custom header and background routines.
1635 function _custom_header_background_just_in_time() {
1636 global $custom_image_header, $custom_background;
1638 if ( current_theme_supports( 'custom-header' ) ) {
1639 // In case any constants were defined after an add_custom_image_header() call, re-run.
1640 add_theme_support( 'custom-header', array( '__jit' => true ) );
1642 $args = get_theme_support( 'custom-header' );
1643 if ( $args[0]['wp-head-callback'] )
1644 add_action( 'wp_head', $args[0]['wp-head-callback'] );
1647 require_once( ABSPATH . 'wp-admin/custom-header.php' );
1648 $custom_image_header = new Custom_Image_Header( $args[0]['admin-head-callback'], $args[0]['admin-preview-callback'] );
1652 if ( current_theme_supports( 'custom-background' ) ) {
1653 // In case any constants were defined after an add_custom_background() call, re-run.
1654 add_theme_support( 'custom-background', array( '__jit' => true ) );
1656 $args = get_theme_support( 'custom-background' );
1657 add_action( 'wp_head', $args[0]['wp-head-callback'] );
1660 require_once( ABSPATH . 'wp-admin/custom-background.php' );
1661 $custom_background = new Custom_Background( $args[0]['admin-head-callback'], $args[0]['admin-preview-callback'] );
1665 add_action( 'wp_loaded', '_custom_header_background_just_in_time' );
1668 * Gets the theme support arguments passed when registering that support
1672 * @param string $feature the feature to check
1673 * @return mixed The array of extra arguments or the value for the registered feature.
1675 function get_theme_support( $feature ) {
1676 global $_wp_theme_features;
1677 if ( ! isset( $_wp_theme_features[ $feature ] ) )
1680 if ( func_num_args() <= 1 )
1681 return $_wp_theme_features[ $feature ];
1683 $args = array_slice( func_get_args(), 1 );
1684 switch ( $feature ) {
1685 case 'custom-header' :
1686 case 'custom-background' :
1687 if ( isset( $_wp_theme_features[ $feature ][0][ $args[0] ] ) )
1688 return $_wp_theme_features[ $feature ][0][ $args[0] ];
1692 return $_wp_theme_features[ $feature ];
1697 * Allows a theme to de-register its support of a certain feature
1699 * Should be called in the theme's functions.php file. Generally would
1700 * be used for child themes to override support from the parent theme.
1703 * @see add_theme_support()
1704 * @param string $feature the feature being added
1705 * @return null|bool Whether feature was removed.
1707 function remove_theme_support( $feature ) {
1708 // Blacklist: for internal registrations not used directly by themes.
1709 if ( in_array( $feature, array( 'editor-style', 'widgets', 'menus' ) ) )
1712 return _remove_theme_support( $feature );
1716 * Do not use. Removes theme support internally, ignorant of the blacklist.
1720 * @param string $feature
1722 function _remove_theme_support( $feature ) {
1723 global $_wp_theme_features;
1725 switch ( $feature ) {
1726 case 'custom-header-uploads' :
1727 if ( ! isset( $_wp_theme_features['custom-header'] ) )
1729 add_theme_support( 'custom-header', array( 'uploads' => false ) );
1730 return; // Do not continue - custom-header-uploads no longer exists.
1733 if ( ! isset( $_wp_theme_features[ $feature ] ) )
1736 switch ( $feature ) {
1737 case 'custom-header' :
1738 if ( ! did_action( 'wp_loaded' ) )
1740 $support = get_theme_support( 'custom-header' );
1741 if ( $support[0]['wp-head-callback'] )
1742 remove_action( 'wp_head', $support[0]['wp-head-callback'] );
1743 remove_action( 'admin_menu', array( $GLOBALS['custom_image_header'], 'init' ) );
1744 unset( $GLOBALS['custom_image_header'] );
1747 case 'custom-background' :
1748 if ( ! did_action( 'wp_loaded' ) )
1750 $support = get_theme_support( 'custom-background' );
1751 remove_action( 'wp_head', $support[0]['wp-head-callback'] );
1752 remove_action( 'admin_menu', array( $GLOBALS['custom_background'], 'init' ) );
1753 unset( $GLOBALS['custom_background'] );
1757 unset( $_wp_theme_features[ $feature ] );
1762 * Checks a theme's support for a given feature
1765 * @param string $feature the feature being checked
1768 function current_theme_supports( $feature ) {
1769 global $_wp_theme_features;
1771 if ( 'custom-header-uploads' == $feature )
1772 return current_theme_supports( 'custom-header', 'uploads' );
1774 if ( !isset( $_wp_theme_features[$feature] ) )
1777 if ( 'title-tag' == $feature ) {
1778 // Don't confirm support unless called internally.
1779 $trace = debug_backtrace();
1780 if ( ! in_array( $trace[1]['function'], array( '_wp_render_title_tag', 'wp_title' ) ) ) {
1785 // If no args passed then no extra checks need be performed
1786 if ( func_num_args() <= 1 )
1789 $args = array_slice( func_get_args(), 1 );
1791 switch ( $feature ) {
1792 case 'post-thumbnails':
1793 // post-thumbnails can be registered for only certain content/post types by passing
1794 // an array of types to add_theme_support(). If no array was passed, then
1795 // any type is accepted
1796 if ( true === $_wp_theme_features[$feature] ) // Registered for all types
1798 $content_type = $args[0];
1799 return in_array( $content_type, $_wp_theme_features[$feature][0] );
1802 case 'post-formats':
1803 // specific post formats can be registered by passing an array of types to
1804 // add_theme_support()
1806 // Specific areas of HTML5 support *must* be passed via an array to add_theme_support()
1809 return in_array( $type, $_wp_theme_features[$feature][0] );
1811 case 'custom-header':
1812 case 'custom-background' :
1813 // specific custom header and background capabilities can be registered by passing
1814 // an array to add_theme_support()
1815 $header_support = $args[0];
1816 return ( isset( $_wp_theme_features[$feature][0][$header_support] ) && $_wp_theme_features[$feature][0][$header_support] );
1820 * Filter whether the current theme supports a specific feature.
1822 * The dynamic portion of the hook name, `$feature`, refers to the specific theme
1823 * feature. Possible values include 'post-formats', 'post-thumbnails', 'custom-background',
1824 * 'custom-header', 'menus', 'automatic-feed-links', and 'html5'.
1828 * @param bool true Whether the current theme supports the given feature. Default true.
1829 * @param array $args Array of arguments for the feature.
1830 * @param string $feature The theme feature.
1832 return apply_filters( "current_theme_supports-{$feature}", true, $args, $_wp_theme_features[$feature] );
1836 * Checks a theme's support for a given feature before loading the functions which implement it.
1840 * @param string $feature The feature being checked.
1841 * @param string $include Path to the file.
1842 * @return bool True if the current theme supports the supplied feature, false otherwise.
1844 function require_if_theme_supports( $feature, $include ) {
1845 if ( current_theme_supports( $feature ) ) {
1846 require ( $include );
1853 * Checks an attachment being deleted to see if it's a header or background image.
1855 * If true it removes the theme modification which would be pointing at the deleted
1860 * @param int $id the attachment id
1862 function _delete_attachment_theme_mod( $id ) {
1863 $attachment_image = wp_get_attachment_url( $id );
1864 $header_image = get_header_image();
1865 $background_image = get_background_image();
1867 if ( $header_image && $header_image == $attachment_image )
1868 remove_theme_mod( 'header_image' );
1870 if ( $background_image && $background_image == $attachment_image )
1871 remove_theme_mod( 'background_image' );
1874 add_action( 'delete_attachment', '_delete_attachment_theme_mod' );
1877 * Checks if a theme has been changed and runs 'after_switch_theme' hook on the next WP load
1881 function check_theme_switched() {
1882 if ( $stylesheet = get_option( 'theme_switched' ) ) {
1883 $old_theme = wp_get_theme( $stylesheet );
1885 // Prevent retrieve_widgets() from running since Customizer already called it up front
1886 if ( get_option( 'theme_switched_via_customizer' ) ) {
1887 remove_action( 'after_switch_theme', '_wp_sidebars_changed' );
1888 update_option( 'theme_switched_via_customizer', false );
1891 if ( $old_theme->exists() ) {
1893 * Fires on the first WP load after a theme switch if the old theme still exists.
1895 * This action fires multiple times and the parameters differs
1896 * according to the context, if the old theme exists or not.
1897 * If the old theme is missing, the parameter will be the slug
1902 * @param string $old_name Old theme name.
1903 * @param WP_Theme $old_theme WP_Theme instance of the old theme.
1905 do_action( 'after_switch_theme', $old_theme->get( 'Name' ), $old_theme );
1907 /** This action is documented in wp-includes/theme.php */
1908 do_action( 'after_switch_theme', $stylesheet );
1911 update_option( 'theme_switched', false );
1916 * Includes and instantiates the WP_Customize_Manager class.
1918 * Fires when ?wp_customize=on or on wp-admin/customize.php.
1922 function _wp_customize_include() {
1923 if ( ! ( ( isset( $_REQUEST['wp_customize'] ) && 'on' == $_REQUEST['wp_customize'] )
1924 || ( is_admin() && 'customize.php' == basename( $_SERVER['PHP_SELF'] ) )
1928 require( ABSPATH . WPINC . '/class-wp-customize-manager.php' );
1929 // Init Customize class
1930 $GLOBALS['wp_customize'] = new WP_Customize_Manager;
1932 add_action( 'plugins_loaded', '_wp_customize_include' );
1935 * Adds settings for the customize-loader script.
1939 function _wp_customize_loader_settings() {
1942 $admin_origin = parse_url( admin_url() );
1943 $home_origin = parse_url( home_url() );
1944 $cross_domain = ( strtolower( $admin_origin[ 'host' ] ) != strtolower( $home_origin[ 'host' ] ) );
1947 'mobile' => wp_is_mobile(),
1948 'ios' => wp_is_mobile() && preg_match( '/iPad|iPod|iPhone/', $_SERVER['HTTP_USER_AGENT'] ),
1952 'url' => esc_url( admin_url( 'customize.php' ) ),
1953 'isCrossDomain' => $cross_domain,
1954 'browser' => $browser,
1956 'saveAlert' => __( 'The changes you made will be lost if you navigate away from this page.' ),
1960 $script = 'var _wpCustomizeLoaderSettings = ' . wp_json_encode( $settings ) . ';';
1962 $data = $wp_scripts->get_data( 'customize-loader', 'data' );
1964 $script = "$data\n$script";
1966 $wp_scripts->add_data( 'customize-loader', 'data', $script );
1968 add_action( 'admin_enqueue_scripts', '_wp_customize_loader_settings' );
1971 * Returns a URL to load the Customizer.
1975 * @param string $stylesheet Optional. Theme to customize. Defaults to current theme.
1976 * The theme's stylesheet will be urlencoded if necessary.
1978 function wp_customize_url( $stylesheet = null ) {
1979 $url = admin_url( 'customize.php' );
1981 $url .= '?theme=' . urlencode( $stylesheet );
1982 return esc_url( $url );
1986 * Prints a script to check whether or not the Customizer is supported,
1987 * and apply either the no-customize-support or customize-support class
1990 * This function MUST be called inside the body tag.
1992 * Ideally, call this function immediately after the body tag is opened.
1993 * This prevents a flash of unstyled content.
1995 * It is also recommended that you add the "no-customize-support" class
1996 * to the body tag by default.
2000 function wp_customize_support_script() {
2001 $admin_origin = parse_url( admin_url() );
2002 $home_origin = parse_url( home_url() );
2003 $cross_domain = ( strtolower( $admin_origin[ 'host' ] ) != strtolower( $home_origin[ 'host' ] ) );
2006 <script type="text/javascript">
2008 var request, b = document.body, c = 'className', cs = 'customize-support', rcs = new RegExp('(^|\\s+)(no-)?'+cs+'(\\s+|$)');
2010 <?php if ( $cross_domain ): ?>
2011 request = (function(){ var xhr = new XMLHttpRequest(); return ('withCredentials' in xhr); })();
2016 b[c] = b[c].replace( rcs, ' ' );
2017 b[c] += ( window.postMessage && request ? ' ' : ' no-' ) + cs;
2024 * Whether the site is being previewed in the Customizer.
2028 * @global WP_Customize_Manager $wp_customize Customizer instance.
2030 * @return bool True if the site is being previewed in the Customizer, false otherwise.
2032 function is_customize_preview() {
2033 global $wp_customize;
2035 return is_a( $wp_customize, 'WP_Customize_Manager' ) && $wp_customize->is_preview();