3 * Theme, template, and stylesheet functions.
10 * Whether a child theme is in use.
14 * @return bool true if a child theme is in use, false otherwise.
16 function is_child_theme() {
17 return ( TEMPLATEPATH !== STYLESHEETPATH );
21 * Retrieve name of the current stylesheet.
23 * The theme name that the administrator has currently set the front end theme
26 * For all extensive purposes, the template name and the stylesheet name are
27 * going to be the same for most cases.
30 * @uses apply_filters() Calls 'stylesheet' filter on stylesheet name.
32 * @return string Stylesheet name.
34 function get_stylesheet() {
35 return apply_filters('stylesheet', get_option('stylesheet'));
39 * Retrieve stylesheet directory path for current theme.
42 * @uses apply_filters() Calls 'stylesheet_directory' filter on stylesheet directory and theme name.
44 * @return string Path to current theme directory.
46 function get_stylesheet_directory() {
47 $stylesheet = get_stylesheet();
48 $theme_root = get_theme_root( $stylesheet );
49 $stylesheet_dir = "$theme_root/$stylesheet";
51 return apply_filters( 'stylesheet_directory', $stylesheet_dir, $stylesheet, $theme_root );
55 * Retrieve stylesheet directory URI.
61 function get_stylesheet_directory_uri() {
62 $stylesheet = get_stylesheet();
63 $theme_root_uri = get_theme_root_uri( $stylesheet );
64 $stylesheet_dir_uri = "$theme_root_uri/$stylesheet";
66 return apply_filters( 'stylesheet_directory_uri', $stylesheet_dir_uri, $stylesheet, $theme_root_uri );
70 * Retrieve URI of current theme stylesheet.
72 * The stylesheet file name is 'style.css' which is appended to {@link
73 * get_stylesheet_directory_uri() stylesheet directory URI} path.
76 * @uses apply_filters() Calls 'stylesheet_uri' filter on stylesheet URI path and stylesheet directory URI.
80 function get_stylesheet_uri() {
81 $stylesheet_dir_uri = get_stylesheet_directory_uri();
82 $stylesheet_uri = $stylesheet_dir_uri . '/style.css';
83 return apply_filters('stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri);
87 * Retrieve localized stylesheet URI.
89 * The stylesheet directory for the localized stylesheet files are located, by
90 * default, in the base theme directory. The name of the locale file will be the
91 * locale followed by '.css'. If that does not exist, then the text direction
92 * stylesheet will be checked for existence, for example 'ltr.css'.
94 * The theme may change the location of the stylesheet directory by either using
95 * the 'stylesheet_directory_uri' filter or the 'locale_stylesheet_uri' filter.
96 * If you want to change the location of the stylesheet files for the entire
97 * WordPress workflow, then change the former. If you just have the locale in a
98 * separate folder, then change the latter.
101 * @uses apply_filters() Calls 'locale_stylesheet_uri' filter on stylesheet URI path and stylesheet directory URI.
105 function get_locale_stylesheet_uri() {
107 $stylesheet_dir_uri = get_stylesheet_directory_uri();
108 $dir = get_stylesheet_directory();
109 $locale = get_locale();
110 if ( file_exists("$dir/$locale.css") )
111 $stylesheet_uri = "$stylesheet_dir_uri/$locale.css";
112 elseif ( !empty($wp_locale->text_direction) && file_exists("$dir/{$wp_locale->text_direction}.css") )
113 $stylesheet_uri = "$stylesheet_dir_uri/{$wp_locale->text_direction}.css";
115 $stylesheet_uri = '';
116 return apply_filters('locale_stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri);
120 * Retrieve name of the current theme.
123 * @uses apply_filters() Calls 'template' filter on template option.
125 * @return string Template name.
127 function get_template() {
128 return apply_filters('template', get_option('template'));
132 * Retrieve current theme directory.
135 * @uses apply_filters() Calls 'template_directory' filter on template directory path and template name.
137 * @return string Template directory path.
139 function get_template_directory() {
140 $template = get_template();
141 $theme_root = get_theme_root( $template );
142 $template_dir = "$theme_root/$template";
144 return apply_filters( 'template_directory', $template_dir, $template, $theme_root );
148 * Retrieve theme directory URI.
151 * @uses apply_filters() Calls 'template_directory_uri' filter on template directory URI path and template name.
153 * @return string Template directory URI.
155 function get_template_directory_uri() {
156 $template = get_template();
157 $theme_root_uri = get_theme_root_uri( $template );
158 $template_dir_uri = "$theme_root_uri/$template";
160 return apply_filters( 'template_directory_uri', $template_dir_uri, $template, $theme_root_uri );
164 * Retrieve theme data from parsed theme file.
166 * The description will have the tags filtered with the following HTML elements
167 * whitelisted. The <b>'a'</b> element with the <em>href</em> and <em>title</em>
168 * attributes. The <b>abbr</b> element with the <em>title</em> attribute. The
169 * <b>acronym</b> element with the <em>title</em> attribute allowed. The
170 * <b>code</b>, <b>em</b>, and <b>strong</b> elements also allowed.
172 * The style.css file must contain theme name, theme URI, and description. The
173 * data can also contain author URI, author, template (parent template),
174 * version, status, and finally tags. Some of these are not used by WordPress
175 * administration panels, but are used by theme directory web sites which list
180 * @param string $theme_file Theme file path.
181 * @return array Theme data.
183 function get_theme_data( $theme_file ) {
184 $default_headers = array(
185 'Name' => 'Theme Name',
186 'URI' => 'Theme URI',
187 'Description' => 'Description',
188 'Author' => 'Author',
189 'AuthorURI' => 'Author URI',
190 'Version' => 'Version',
191 'Template' => 'Template',
192 'Status' => 'Status',
196 $themes_allowed_tags = array(
198 'href' => array(),'title' => array()
211 $theme_data = get_file_data( $theme_file, $default_headers, 'theme' );
213 $theme_data['Name'] = $theme_data['Title'] = wp_kses( $theme_data['Name'], $themes_allowed_tags );
215 $theme_data['URI'] = esc_url( $theme_data['URI'] );
217 $theme_data['Description'] = wptexturize( wp_kses( $theme_data['Description'], $themes_allowed_tags ) );
219 $theme_data['AuthorURI'] = esc_url( $theme_data['AuthorURI'] );
221 $theme_data['Template'] = wp_kses( $theme_data['Template'], $themes_allowed_tags );
223 $theme_data['Version'] = wp_kses( $theme_data['Version'], $themes_allowed_tags );
225 if ( $theme_data['Status'] == '' )
226 $theme_data['Status'] = 'publish';
228 $theme_data['Status'] = wp_kses( $theme_data['Status'], $themes_allowed_tags );
230 if ( $theme_data['Tags'] == '' )
231 $theme_data['Tags'] = array();
233 $theme_data['Tags'] = array_map( 'trim', explode( ',', wp_kses( $theme_data['Tags'], array() ) ) );
235 if ( $theme_data['Author'] == '' ) {
236 $theme_data['Author'] = $theme_data['AuthorName'] = __('Anonymous');
238 $theme_data['AuthorName'] = wp_kses( $theme_data['Author'], $themes_allowed_tags );
239 if ( empty( $theme_data['AuthorURI'] ) ) {
240 $theme_data['Author'] = $theme_data['AuthorName'];
242 $theme_data['Author'] = sprintf( '<a href="%1$s" title="%2$s">%3$s</a>', $theme_data['AuthorURI'], esc_attr__( 'Visit author homepage' ), $theme_data['AuthorName'] );
250 * Retrieve list of themes with theme data in theme directory.
252 * The theme is broken, if it doesn't have a parent theme and is missing either
253 * style.css and, or index.php. If the theme has a parent theme then it is
254 * broken, if it is missing style.css; index.php is optional. The broken theme
255 * list is saved in the {@link $wp_broken_themes} global, which is displayed on
256 * the theme list in the administration panels.
259 * @global array $wp_broken_themes Stores the broken themes.
260 * @global array $wp_themes Stores the working themes.
262 * @return array Theme list with theme data.
264 function get_themes() {
265 global $wp_themes, $wp_broken_themes;
267 if ( isset($wp_themes) )
270 if ( !$theme_files = search_theme_directories() )
273 asort( $theme_files );
275 $wp_themes = array();
277 foreach ( (array) $theme_files as $theme_file ) {
278 $theme_root = $theme_file['theme_root'];
279 $theme_file = $theme_file['theme_file'];
281 if ( !is_readable("$theme_root/$theme_file") ) {
282 $wp_broken_themes[$theme_file] = array('Name' => $theme_file, 'Title' => $theme_file, 'Description' => __('File not readable.'));
286 $theme_data = get_theme_data("$theme_root/$theme_file");
288 $name = $theme_data['Name'];
289 $title = $theme_data['Title'];
290 $description = wptexturize($theme_data['Description']);
291 $version = $theme_data['Version'];
292 $author = $theme_data['Author'];
293 $template = $theme_data['Template'];
294 $stylesheet = dirname($theme_file);
297 foreach ( array('png', 'gif', 'jpg', 'jpeg') as $ext ) {
298 if (file_exists("$theme_root/$stylesheet/screenshot.$ext")) {
299 $screenshot = "screenshot.$ext";
304 if ( empty($name) ) {
305 $name = dirname($theme_file);
309 $parent_template = $template;
311 if ( empty($template) ) {
312 if ( file_exists("$theme_root/$stylesheet/index.php") )
313 $template = $stylesheet;
318 $template = trim( $template );
320 if ( !file_exists("$theme_root/$template/index.php") ) {
321 $parent_dir = dirname(dirname($theme_file));
322 if ( file_exists("$theme_root/$parent_dir/$template/index.php") ) {
323 $template = "$parent_dir/$template";
324 $template_directory = "$theme_root/$template";
327 * The parent theme doesn't exist in the current theme's folder or sub folder
328 * so lets use the theme root for the parent template.
330 if ( isset($theme_files[$template]) && file_exists( $theme_files[$template]['theme_root'] . "/$template/index.php" ) ) {
331 $template_directory = $theme_files[$template]['theme_root'] . "/$template";
333 if ( empty( $parent_template) )
334 $wp_broken_themes[$name] = array('Name' => $name, 'Title' => $title, 'Description' => __('Template is missing.'), 'error' => 'no_template');
336 $wp_broken_themes[$name] = array('Name' => $name, 'Title' => $title, 'Description' => sprintf( __('The parent theme is missing. Please install the "%s" parent theme.'), $parent_template ), 'error' => 'no_parent', 'parent' => $parent_template );
342 $template_directory = trim( $theme_root . '/' . $template );
345 $stylesheet_files = array();
346 $template_files = array();
348 $stylesheet_dir = @ dir("$theme_root/$stylesheet");
349 if ( $stylesheet_dir ) {
350 while ( ($file = $stylesheet_dir->read()) !== false ) {
351 if ( !preg_match('|^\.+$|', $file) ) {
352 if ( preg_match('|\.css$|', $file) )
353 $stylesheet_files[] = "$theme_root/$stylesheet/$file";
354 elseif ( preg_match('|\.php$|', $file) )
355 $template_files[] = "$theme_root/$stylesheet/$file";
358 @ $stylesheet_dir->close();
361 $template_dir = @ dir("$template_directory");
362 if ( $template_dir ) {
363 while ( ($file = $template_dir->read()) !== false ) {
364 if ( preg_match('|^\.+$|', $file) )
366 if ( preg_match('|\.php$|', $file) ) {
367 $template_files[] = "$template_directory/$file";
368 } elseif ( is_dir("$template_directory/$file") ) {
369 $template_subdir = @ dir("$template_directory/$file");
370 if ( !$template_subdir )
372 while ( ($subfile = $template_subdir->read()) !== false ) {
373 if ( preg_match('|^\.+$|', $subfile) )
375 if ( preg_match('|\.php$|', $subfile) )
376 $template_files[] = "$template_directory/$file/$subfile";
378 @ $template_subdir->close();
381 @ $template_dir->close();
384 //Make unique and remove duplicates when stylesheet and template are the same i.e. most themes
385 $template_files = array_unique($template_files);
386 $stylesheet_files = array_unique($stylesheet_files);
388 $template_dir = $template_directory;
389 $stylesheet_dir = $theme_root . '/' . $stylesheet;
391 if ( empty($template_dir) )
393 if ( empty($stylesheet_dir) )
394 $stylesheet_dir = '/';
396 // Check for theme name collision. This occurs if a theme is copied to
397 // a new theme directory and the theme header is not updated. Whichever
398 // theme is first keeps the name. Subsequent themes get a suffix applied.
399 // The Twenty Eleven, Twenty Ten, Default and Classic themes always trump
401 if ( isset($wp_themes[$name]) ) {
402 $trump_cards = array(
403 'classic' => 'WordPress Classic',
404 'default' => 'WordPress Default',
405 'twentyten' => 'Twenty Ten',
406 'twentyeleven' => 'Twenty Eleven',
408 if ( isset( $trump_cards[ $stylesheet ] ) && $name == $trump_cards[ $stylesheet ] ) {
409 // If another theme has claimed to be one of our default themes, move
411 $suffix = $wp_themes[$name]['Stylesheet'];
412 $new_name = "$name/$suffix";
413 $wp_themes[$new_name] = $wp_themes[$name];
414 $wp_themes[$new_name]['Name'] = $new_name;
416 $name = "$name/$stylesheet";
420 $theme_roots[$stylesheet] = str_replace( WP_CONTENT_DIR, '', $theme_root );
421 $wp_themes[$name] = array(
424 'Description' => $description,
426 'Author Name' => $theme_data['AuthorName'],
427 'Author URI' => $theme_data['AuthorURI'],
428 'Version' => $version,
429 'Template' => $template,
430 'Stylesheet' => $stylesheet,
431 'Template Files' => $template_files,
432 'Stylesheet Files' => $stylesheet_files,
433 'Template Dir' => $template_dir,
434 'Stylesheet Dir' => $stylesheet_dir,
435 'Status' => $theme_data['Status'],
436 'Screenshot' => $screenshot,
437 'Tags' => $theme_data['Tags'],
438 'Theme Root' => $theme_root,
439 'Theme Root URI' => str_replace( WP_CONTENT_DIR, content_url(), $theme_root ),
445 /* Store theme roots in the DB */
446 if ( get_site_transient( 'theme_roots' ) != $theme_roots )
447 set_site_transient( 'theme_roots', $theme_roots, 7200 ); // cache for two hours
450 /* Resolve theme dependencies. */
451 $theme_names = array_keys( $wp_themes );
452 foreach ( (array) $theme_names as $theme_name ) {
453 $wp_themes[$theme_name]['Parent Theme'] = '';
454 if ( $wp_themes[$theme_name]['Stylesheet'] != $wp_themes[$theme_name]['Template'] ) {
455 foreach ( (array) $theme_names as $parent_theme_name ) {
456 if ( ($wp_themes[$parent_theme_name]['Stylesheet'] == $wp_themes[$parent_theme_name]['Template']) && ($wp_themes[$parent_theme_name]['Template'] == $wp_themes[$theme_name]['Template']) ) {
457 $wp_themes[$theme_name]['Parent Theme'] = $wp_themes[$parent_theme_name]['Name'];
468 * Retrieve theme roots.
472 * @return array|string An array of theme roots keyed by template/stylesheet or a single theme root if all themes have the same root.
474 function get_theme_roots() {
475 global $wp_theme_directories;
477 if ( count($wp_theme_directories) <= 1 )
480 $theme_roots = get_site_transient( 'theme_roots' );
481 if ( false === $theme_roots ) {
483 $theme_roots = get_site_transient( 'theme_roots' ); // this is set in get_theme()
489 * Retrieve theme data.
493 * @param string $theme Theme name.
494 * @return array|null Null, if theme name does not exist. Theme data, if exists.
496 function get_theme($theme) {
497 $themes = get_themes();
499 if ( is_array( $themes ) && array_key_exists( $theme, $themes ) )
500 return $themes[$theme];
506 * Retrieve current theme display name.
508 * If the 'current_theme' option has already been set, then it will be returned
509 * instead. If it is not set, then each theme will be iterated over until both
510 * the current stylesheet and current template name.
516 function get_current_theme() {
517 if ( $theme = get_option('current_theme') )
520 $themes = get_themes();
521 $current_theme = 'Twenty Eleven';
524 $theme_names = array_keys( $themes );
525 $current_template = get_option( 'template' );
526 $current_stylesheet = get_option( 'stylesheet' );
528 foreach ( (array) $theme_names as $theme_name ) {
529 if ( $themes[$theme_name]['Stylesheet'] == $current_stylesheet &&
530 $themes[$theme_name]['Template'] == $current_template ) {
531 $current_theme = $themes[$theme_name]['Name'];
537 update_option('current_theme', $current_theme);
539 return $current_theme;
543 * Register a directory that contains themes.
547 * @param string $directory Either the full filesystem path to a theme folder or a folder within WP_CONTENT_DIR
550 function register_theme_directory( $directory) {
551 global $wp_theme_directories;
553 /* If this folder does not exist, return and do not register */
554 if ( !file_exists( $directory ) )
555 /* Try prepending as the theme directory could be relative to the content directory */
556 $registered_directory = WP_CONTENT_DIR . '/' . $directory;
558 $registered_directory = $directory;
560 /* If this folder does not exist, return and do not register */
561 if ( !file_exists( $registered_directory ) )
564 $wp_theme_directories[] = $registered_directory;
570 * Search all registered theme directories for complete and valid themes.
574 * @return array Valid themes found
576 function search_theme_directories() {
577 global $wp_theme_directories, $wp_broken_themes;
578 if ( empty( $wp_theme_directories ) )
581 $theme_files = array();
582 $wp_broken_themes = array();
584 /* Loop the registered theme directories and extract all themes */
585 foreach ( (array) $wp_theme_directories as $theme_root ) {
586 $theme_loc = $theme_root;
588 /* We don't want to replace all forward slashes, see Trac #4541 */
589 if ( '/' != WP_CONTENT_DIR )
590 $theme_loc = str_replace(WP_CONTENT_DIR, '', $theme_root);
592 /* Files in the root of the current theme directory and one subdir down */
593 $themes_dir = @ opendir($theme_root);
598 while ( ($theme_dir = readdir($themes_dir)) !== false ) {
599 if ( is_dir($theme_root . '/' . $theme_dir) && is_readable($theme_root . '/' . $theme_dir) ) {
600 if ( $theme_dir[0] == '.' || $theme_dir == 'CVS' )
603 $stylish_dir = @opendir($theme_root . '/' . $theme_dir);
604 $found_stylesheet = false;
606 while ( ($theme_file = readdir($stylish_dir)) !== false ) {
607 if ( $theme_file == 'style.css' ) {
608 $theme_files[$theme_dir] = array( 'theme_file' => $theme_dir . '/' . $theme_file, 'theme_root' => $theme_root );
609 $found_stylesheet = true;
613 @closedir($stylish_dir);
615 if ( !$found_stylesheet ) { // look for themes in that dir
616 $subdir = "$theme_root/$theme_dir";
617 $subdir_name = $theme_dir;
618 $theme_subdirs = @opendir( $subdir );
620 $found_subdir_themes = false;
621 while ( ($theme_subdir = readdir($theme_subdirs)) !== false ) {
622 if ( is_dir( $subdir . '/' . $theme_subdir) && is_readable($subdir . '/' . $theme_subdir) ) {
623 if ( $theme_subdir[0] == '.' || $theme_subdir == 'CVS' )
626 $stylish_dir = @opendir($subdir . '/' . $theme_subdir);
627 $found_stylesheet = false;
629 while ( ($theme_file = readdir($stylish_dir)) !== false ) {
630 if ( $theme_file == 'style.css' ) {
631 $theme_files["$theme_dir/$theme_subdir"] = array( 'theme_file' => $subdir_name . '/' . $theme_subdir . '/' . $theme_file, 'theme_root' => $theme_root );
632 $found_stylesheet = true;
633 $found_subdir_themes = true;
637 @closedir($stylish_dir);
640 @closedir($theme_subdirs);
641 if ( !$found_subdir_themes )
642 $wp_broken_themes[$theme_dir] = array('Name' => $theme_dir, 'Title' => $theme_dir, 'Description' => __('Stylesheet is missing.'));
646 @closedir( $themes_dir );
652 * Retrieve path to themes directory.
654 * Does not have trailing slash.
657 * @uses apply_filters() Calls 'theme_root' filter on path.
659 * @param string $stylesheet_or_template The stylesheet or template name of the theme
660 * @return string Theme path.
662 function get_theme_root( $stylesheet_or_template = false ) {
663 if ( $stylesheet_or_template ) {
664 if ( $theme_root = get_raw_theme_root($stylesheet_or_template) )
665 $theme_root = WP_CONTENT_DIR . $theme_root;
667 $theme_root = WP_CONTENT_DIR . '/themes';
669 $theme_root = WP_CONTENT_DIR . '/themes';
672 return apply_filters( 'theme_root', $theme_root );
676 * Retrieve URI for themes directory.
678 * Does not have trailing slash.
682 * @param string $stylesheet_or_template The stylesheet or template name of the theme
683 * @return string Themes URI.
685 function get_theme_root_uri( $stylesheet_or_template = false ) {
686 if ( $stylesheet_or_template ) {
687 if ( $theme_root = get_raw_theme_root($stylesheet_or_template) )
688 $theme_root_uri = content_url( $theme_root );
690 $theme_root_uri = content_url( 'themes' );
692 $theme_root_uri = content_url( 'themes' );
695 return apply_filters( 'theme_root_uri', $theme_root_uri, get_option('siteurl'), $stylesheet_or_template );
699 * Get the raw theme root relative to the content directory with no filters applied.
703 * @param string $stylesheet_or_template The stylesheet or template name of the theme
704 * @return string Theme root
706 function get_raw_theme_root( $stylesheet_or_template, $no_cache = false ) {
707 global $wp_theme_directories;
709 if ( count($wp_theme_directories) <= 1 )
714 // If requesting the root for the current theme, consult options to avoid calling get_theme_roots()
716 if ( get_option('stylesheet') == $stylesheet_or_template )
717 $theme_root = get_option('stylesheet_root');
718 elseif ( get_option('template') == $stylesheet_or_template )
719 $theme_root = get_option('template_root');
722 if ( empty($theme_root) ) {
723 $theme_roots = get_theme_roots();
724 if ( !empty($theme_roots[$stylesheet_or_template]) )
725 $theme_root = $theme_roots[$stylesheet_or_template];
732 * Retrieve path to a template
734 * Used to quickly retrieve the path of a template without including the file
735 * extension. It will also check the parent theme, if the file exists, with
736 * the use of {@link locate_template()}. Allows for more generic template location
737 * without the use of the other get_*_template() functions.
741 * @param string $type Filename without extension.
742 * @param array $templates An optional list of template candidates
743 * @return string Full path to file.
745 function get_query_template( $type, $templates = array() ) {
746 $type = preg_replace( '|[^a-z0-9-]+|', '', $type );
748 if ( empty( $templates ) )
749 $templates = array("{$type}.php");
751 return apply_filters( "{$type}_template", locate_template( $templates ) );
755 * Retrieve path of index template in current or parent template.
761 function get_index_template() {
762 return get_query_template('index');
766 * Retrieve path of 404 template in current or parent template.
772 function get_404_template() {
773 return get_query_template('404');
777 * Retrieve path of archive template in current or parent template.
783 function get_archive_template() {
784 $post_type = get_query_var( 'post_type' );
786 $templates = array();
789 $templates[] = "archive-{$post_type}.php";
790 $templates[] = 'archive.php';
792 return get_query_template( 'archive', $templates );
796 * Retrieve path of author template in current or parent template.
802 function get_author_template() {
803 $author = get_queried_object();
805 $templates = array();
807 $templates[] = "author-{$author->user_nicename}.php";
808 $templates[] = "author-{$author->ID}.php";
809 $templates[] = 'author.php';
811 return get_query_template( 'author', $templates );
815 * Retrieve path of category template in current or parent template.
817 * Works by first retrieving the current slug for example 'category-default.php' and then
818 * trying category ID, for example 'category-1.php' and will finally fallback to category.php
819 * template, if those files don't exist.
822 * @uses apply_filters() Calls 'category_template' on file path of category template.
826 function get_category_template() {
827 $category = get_queried_object();
829 $templates = array();
831 $templates[] = "category-{$category->slug}.php";
832 $templates[] = "category-{$category->term_id}.php";
833 $templates[] = 'category.php';
835 return get_query_template( 'category', $templates );
839 * Retrieve path of tag template in current or parent template.
841 * Works by first retrieving the current tag name, for example 'tag-wordpress.php' and then
842 * trying tag ID, for example 'tag-1.php' and will finally fallback to tag.php
843 * template, if those files don't exist.
846 * @uses apply_filters() Calls 'tag_template' on file path of tag template.
850 function get_tag_template() {
851 $tag = get_queried_object();
853 $templates = array();
855 $templates[] = "tag-{$tag->slug}.php";
856 $templates[] = "tag-{$tag->term_id}.php";
857 $templates[] = 'tag.php';
859 return get_query_template( 'tag', $templates );
863 * Retrieve path of taxonomy template in current or parent template.
865 * Retrieves the taxonomy and term, if term is available. The template is
866 * prepended with 'taxonomy-' and followed by both the taxonomy string and
867 * the taxonomy string followed by a dash and then followed by the term.
869 * The taxonomy and term template is checked and used first, if it exists.
870 * Second, just the taxonomy template is checked, and then finally, taxonomy.php
871 * template is used. If none of the files exist, then it will fall back on to
875 * @uses apply_filters() Calls 'taxonomy_template' filter on found path.
879 function get_taxonomy_template() {
880 $term = get_queried_object();
881 $taxonomy = $term->taxonomy;
883 $templates = array();
885 $templates[] = "taxonomy-$taxonomy-{$term->slug}.php";
886 $templates[] = "taxonomy-$taxonomy.php";
887 $templates[] = 'taxonomy.php';
889 return get_query_template( 'taxonomy', $templates );
893 * Retrieve path of date template in current or parent template.
899 function get_date_template() {
900 return get_query_template('date');
904 * Retrieve path of home template in current or parent template.
906 * This is the template used for the page containing the blog posts
908 * Attempts to locate 'home.php' first before falling back to 'index.php'.
911 * @uses apply_filters() Calls 'home_template' on file path of home template.
915 function get_home_template() {
916 $templates = array( 'home.php', 'index.php' );
918 return get_query_template( 'home', $templates );
922 * Retrieve path of front-page template in current or parent template.
924 * Looks for 'front-page.php'.
927 * @uses apply_filters() Calls 'front_page_template' on file path of template.
931 function get_front_page_template() {
932 $templates = array('front-page.php');
934 return get_query_template( 'front_page', $templates );
938 * Retrieve path of page template in current or parent template.
940 * Will first look for the specifically assigned page template
941 * The will search for 'page-{slug}.php' followed by 'page-id.php'
942 * and finally 'page.php'
948 function get_page_template() {
949 $id = get_queried_object_id();
950 $template = get_post_meta($id, '_wp_page_template', true);
951 $pagename = get_query_var('pagename');
953 if ( !$pagename && $id > 0 ) {
954 // If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object
955 $post = get_queried_object();
956 $pagename = $post->post_name;
959 if ( 'default' == $template )
962 $templates = array();
963 if ( !empty($template) && !validate_file($template) )
964 $templates[] = $template;
966 $templates[] = "page-$pagename.php";
968 $templates[] = "page-$id.php";
969 $templates[] = 'page.php';
971 return get_query_template( 'page', $templates );
975 * Retrieve path of paged template in current or parent template.
981 function get_paged_template() {
982 return get_query_template('paged');
986 * Retrieve path of search template in current or parent template.
992 function get_search_template() {
993 return get_query_template('search');
997 * Retrieve path of single template in current or parent template.
1003 function get_single_template() {
1004 $object = get_queried_object();
1006 $templates = array();
1008 $templates[] = "single-{$object->post_type}.php";
1009 $templates[] = "single.php";
1011 return get_query_template( 'single', $templates );
1015 * Retrieve path of attachment template in current or parent template.
1017 * The attachment path first checks if the first part of the mime type exists.
1018 * The second check is for the second part of the mime type. The last check is
1019 * for both types separated by an underscore. If neither are found then the file
1020 * 'attachment.php' is checked and returned.
1022 * Some examples for the 'text/plain' mime type are 'text.php', 'plain.php', and
1023 * finally 'text_plain.php'.
1029 function get_attachment_template() {
1031 $type = explode('/', $posts[0]->post_mime_type);
1032 if ( $template = get_query_template($type[0]) )
1034 elseif ( $template = get_query_template($type[1]) )
1036 elseif ( $template = get_query_template("$type[0]_$type[1]") )
1039 return get_query_template('attachment');
1043 * Retrieve path of comment popup template in current or parent template.
1045 * Checks for comment popup template in current template, if it exists or in the
1049 * @uses apply_filters() Calls 'comments_popup_template' filter on path.
1053 function get_comments_popup_template() {
1054 $template = get_query_template( 'comments_popup', array( 'comments-popup.php' ) );
1056 // Backward compat code will be removed in a future release
1057 if ('' == $template)
1058 $template = ABSPATH . WPINC . '/theme-compat/comments-popup.php';
1064 * Retrieve the name of the highest priority template file that exists.
1066 * Searches in the STYLESHEETPATH before TEMPLATEPATH so that themes which
1067 * inherit from a parent theme can just overload one file.
1071 * @param string|array $template_names Template file(s) to search for, in order.
1072 * @param bool $load If true the template file will be loaded if it is found.
1073 * @param bool $require_once Whether to require_once or require. Default true. Has no effect if $load is false.
1074 * @return string The template filename if one is located.
1076 function locate_template($template_names, $load = false, $require_once = true ) {
1078 foreach ( (array) $template_names as $template_name ) {
1079 if ( !$template_name )
1081 if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
1082 $located = STYLESHEETPATH . '/' . $template_name;
1084 } else if ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
1085 $located = TEMPLATEPATH . '/' . $template_name;
1090 if ( $load && '' != $located )
1091 load_template( $located, $require_once );
1097 * Require the template file with WordPress environment.
1099 * The globals are set up for the template file to ensure that the WordPress
1100 * environment is available from within the function. The query variables are
1105 * @param string $_template_file Path to template file.
1106 * @param bool $require_once Whether to require_once or require. Default true.
1108 function load_template( $_template_file, $require_once = true ) {
1109 global $posts, $post, $wp_did_header, $wp_did_template_redirect, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
1111 if ( is_array( $wp_query->query_vars ) )
1112 extract( $wp_query->query_vars, EXTR_SKIP );
1114 if ( $require_once )
1115 require_once( $_template_file );
1117 require( $_template_file );
1121 * Display localized stylesheet link element.
1125 function locale_stylesheet() {
1126 $stylesheet = get_locale_stylesheet_uri();
1127 if ( empty($stylesheet) )
1129 echo '<link rel="stylesheet" href="' . $stylesheet . '" type="text/css" media="screen" />';
1133 * Start preview theme output buffer.
1135 * Will only preform task if the user has permissions and template and preview
1136 * query variables exist.
1140 function preview_theme() {
1141 if ( ! (isset($_GET['template']) && isset($_GET['preview'])) )
1144 if ( !current_user_can( 'switch_themes' ) )
1147 // Admin Thickbox requests
1148 if ( isset( $_GET['preview_iframe'] ) )
1149 show_admin_bar( false );
1151 $_GET['template'] = preg_replace('|[^a-z0-9_./-]|i', '', $_GET['template']);
1153 if ( validate_file($_GET['template']) )
1156 add_filter( 'template', '_preview_theme_template_filter' );
1158 if ( isset($_GET['stylesheet']) ) {
1159 $_GET['stylesheet'] = preg_replace('|[^a-z0-9_./-]|i', '', $_GET['stylesheet']);
1160 if ( validate_file($_GET['stylesheet']) )
1162 add_filter( 'stylesheet', '_preview_theme_stylesheet_filter' );
1165 // Prevent theme mods to current theme being used on theme being previewed
1166 add_filter( 'pre_option_mods_' . get_current_theme(), '__return_empty_array' );
1168 ob_start( 'preview_theme_ob_filter' );
1170 add_action('setup_theme', 'preview_theme');
1173 * Private function to modify the current template when previewing a theme
1180 function _preview_theme_template_filter() {
1181 return isset($_GET['template']) ? $_GET['template'] : '';
1185 * Private function to modify the current stylesheet when previewing a theme
1192 function _preview_theme_stylesheet_filter() {
1193 return isset($_GET['stylesheet']) ? $_GET['stylesheet'] : '';
1197 * Callback function for ob_start() to capture all links in the theme.
1202 * @param string $content
1205 function preview_theme_ob_filter( $content ) {
1206 return preg_replace_callback( "|(<a.*?href=([\"']))(.*?)([\"'].*?>)|", 'preview_theme_ob_filter_callback', $content );
1210 * Manipulates preview theme links in order to control and maintain location.
1212 * Callback function for preg_replace_callback() to accept and filter matches.
1217 * @param array $matches
1220 function preview_theme_ob_filter_callback( $matches ) {
1221 if ( strpos($matches[4], 'onclick') !== false )
1222 $matches[4] = preg_replace('#onclick=([\'"]).*?(?<!\\\)\\1#i', '', $matches[4]); //Strip out any onclicks from rest of <a>. (?<!\\\) means to ignore the '" if its escaped by \ to prevent breaking mid-attribute.
1224 ( false !== strpos($matches[3], '/wp-admin/') )
1226 ( false !== strpos( $matches[3], '://' ) && 0 !== strpos( $matches[3], home_url() ) )
1228 ( false !== strpos($matches[3], '/feed/') )
1230 ( false !== strpos($matches[3], '/trackback/') )
1232 return $matches[1] . "#$matches[2] onclick=$matches[2]return false;" . $matches[4];
1234 $link = add_query_arg( array('preview' => 1, 'template' => $_GET['template'], 'stylesheet' => @$_GET['stylesheet'] ), $matches[3] );
1235 if ( 0 === strpos($link, 'preview=1') )
1237 return $matches[1] . esc_attr( $link ) . $matches[4];
1241 * Switches current theme to new template and stylesheet names.
1244 * @uses do_action() Calls 'switch_theme' action on updated theme display name.
1246 * @param string $template Template name
1247 * @param string $stylesheet Stylesheet name.
1249 function switch_theme($template, $stylesheet) {
1250 global $wp_theme_directories, $sidebars_widgets;
1252 if ( is_array( $sidebars_widgets ) )
1253 set_theme_mod( 'sidebars_widgets', array( 'time' => time(), 'data' => $sidebars_widgets ) );
1255 $old_theme = get_current_theme();
1257 update_option('template', $template);
1258 update_option('stylesheet', $stylesheet);
1260 if ( count($wp_theme_directories) > 1 ) {
1261 update_option('template_root', get_raw_theme_root($template, true));
1262 update_option('stylesheet_root', get_raw_theme_root($stylesheet, true));
1265 delete_option('current_theme');
1266 $theme = get_current_theme();
1268 if ( is_admin() && false === get_option( "theme_mods_$stylesheet" ) ) {
1269 $default_theme_mods = (array) get_option( "mods_$theme" );
1270 add_option( "theme_mods_$stylesheet", $default_theme_mods );
1273 update_option( 'theme_switched', $old_theme );
1274 do_action( 'switch_theme', $theme );
1278 * Checks that current theme files 'index.php' and 'style.css' exists.
1280 * Does not check the default theme, which is the fallback and should always exist.
1281 * Will switch theme to the fallback theme if current theme does not validate.
1282 * You can use the 'validate_current_theme' filter to return FALSE to
1283 * disable this functionality.
1286 * @see WP_DEFAULT_THEME
1290 function validate_current_theme() {
1291 // Don't validate during an install/upgrade.
1292 if ( defined('WP_INSTALLING') || !apply_filters( 'validate_current_theme', true ) )
1295 if ( get_template() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/index.php') ) {
1296 switch_theme( WP_DEFAULT_THEME, WP_DEFAULT_THEME );
1300 if ( get_stylesheet() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/style.css') ) {
1301 switch_theme( WP_DEFAULT_THEME, WP_DEFAULT_THEME );
1305 if ( is_child_theme() && ! file_exists( get_stylesheet_directory() . '/style.css' ) ) {
1306 switch_theme( WP_DEFAULT_THEME, WP_DEFAULT_THEME );
1314 * Retrieve all theme modifications.
1318 * @return array Theme modifications.
1320 function get_theme_mods() {
1321 $theme_slug = get_option( 'stylesheet' );
1322 if ( false === ( $mods = get_option( "theme_mods_$theme_slug" ) ) ) {
1323 $theme_name = get_current_theme();
1324 $mods = get_option( "mods_$theme_name" ); // Deprecated location.
1325 if ( is_admin() && false !== $mods ) {
1326 update_option( "theme_mods_$theme_slug", $mods );
1327 delete_option( "mods_$theme_name" );
1334 * Retrieve theme modification value for the current theme.
1336 * If the modification name does not exist, then the $default will be passed
1337 * through {@link http://php.net/sprintf sprintf()} PHP function with the first
1338 * string the template directory URI and the second string the stylesheet
1342 * @uses apply_filters() Calls 'theme_mod_$name' filter on the value.
1344 * @param string $name Theme modification name.
1345 * @param bool|string $default
1348 function get_theme_mod( $name, $default = false ) {
1349 $mods = get_theme_mods();
1351 if ( isset( $mods[ $name ] ) )
1352 return apply_filters( "theme_mod_$name", $mods[ $name ] );
1354 return apply_filters( "theme_mod_$name", sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() ) );
1358 * Update theme modification value for the current theme.
1362 * @param string $name Theme modification name.
1363 * @param string $value theme modification value.
1365 function set_theme_mod( $name, $value ) {
1366 $mods = get_theme_mods();
1368 $mods[ $name ] = $value;
1370 $theme = get_option( 'stylesheet' );
1371 update_option( "theme_mods_$theme", $mods );
1375 * Remove theme modification name from current theme list.
1377 * If removing the name also removes all elements, then the entire option will
1382 * @param string $name Theme modification name.
1385 function remove_theme_mod( $name ) {
1386 $mods = get_theme_mods();
1388 if ( ! isset( $mods[ $name ] ) )
1391 unset( $mods[ $name ] );
1393 if ( empty( $mods ) )
1394 return remove_theme_mods();
1396 $theme = get_option( 'stylesheet' );
1397 update_option( "theme_mods_$theme", $mods );
1401 * Remove theme modifications option for current theme.
1405 function remove_theme_mods() {
1406 delete_option( 'theme_mods_' . get_option( 'stylesheet' ) );
1407 delete_option( 'mods_' . get_current_theme() );
1411 * Retrieve text color for custom header.
1414 * @uses HEADER_TEXTCOLOR
1418 function get_header_textcolor() {
1419 $default = defined('HEADER_TEXTCOLOR') ? HEADER_TEXTCOLOR : '';
1421 return get_theme_mod('header_textcolor', $default);
1425 * Display text color for custom header.
1429 function header_textcolor() {
1430 echo get_header_textcolor();
1434 * Retrieve header image for custom header.
1437 * @uses HEADER_IMAGE
1441 function get_header_image() {
1442 $default = defined( 'HEADER_IMAGE' ) ? HEADER_IMAGE : '';
1443 $url = get_theme_mod( 'header_image', $default );
1445 if ( 'remove-header' == $url )
1448 if ( is_random_header_image() )
1449 $url = get_random_header_image();
1452 $url = str_replace( 'http://', 'https://', $url );
1454 $url = str_replace( 'https://', 'http://', $url );
1456 return esc_url_raw( $url );
1460 * Get random header image from registered images in theme.
1464 * @return string Path to header image
1466 function get_random_header_image() {
1467 global $_wp_default_headers;
1469 $header_image_mod = get_theme_mod( 'header_image', '' );
1472 if ( 'random-uploaded-image' == $header_image_mod )
1473 $headers = get_uploaded_header_images();
1474 elseif ( ! empty( $_wp_default_headers ) ) {
1475 if ( 'random-default-image' == $header_image_mod ) {
1476 $headers = $_wp_default_headers;
1478 $is_random = get_theme_support( 'custom-header' );
1479 if ( isset( $is_random[ 0 ] ) && !empty( $is_random[ 0 ][ 'random-default' ] ) )
1480 $headers = $_wp_default_headers;
1484 if ( empty( $headers ) )
1487 $random_image = array_rand( $headers );
1488 $header_url = sprintf( $headers[$random_image]['url'], get_template_directory_uri(), get_stylesheet_directory_uri() );
1494 * Check if random header image is in use.
1496 * Always true if user expressly chooses the option in Appearance > Header.
1497 * Also true if theme has multiple header images registered, no specific header image
1498 * is chosen, and theme turns on random headers with add_theme_support().
1501 * @uses HEADER_IMAGE
1503 * @param string $type The random pool to use. any|default|uploaded
1506 function is_random_header_image( $type = 'any' ) {
1507 $default = defined( 'HEADER_IMAGE' ) ? HEADER_IMAGE : '';
1508 $header_image_mod = get_theme_mod( 'header_image', $default );
1510 if ( 'any' == $type ) {
1511 if ( 'random-default-image' == $header_image_mod || 'random-uploaded-image' == $header_image_mod || ( '' != get_random_header_image() && empty( $header_image_mod ) ) )
1514 if ( "random-$type-image" == $header_image_mod )
1516 elseif ( 'default' == $type && empty( $header_image_mod ) && '' != get_random_header_image() )
1524 * Display header image path.
1528 function header_image() {
1529 echo get_header_image();
1533 * Get the header images uploaded for the current theme.
1539 function get_uploaded_header_images() {
1540 $header_images = array();
1543 $headers = get_posts( array( 'post_type' => 'attachment', 'meta_key' => '_wp_attachment_is_custom_header', 'meta_value' => get_option('stylesheet'), 'orderby' => 'none', 'nopaging' => true ) );
1545 if ( empty( $headers ) )
1548 foreach ( (array) $headers as $header ) {
1549 $url = esc_url_raw( $header->guid );
1550 $header = basename($url);
1551 $header_images[$header] = array();
1552 $header_images[$header]['url'] = $url;
1553 $header_images[$header]['thumbnail_url'] = $url;
1554 $header_images[$header]['uploaded'] = true;
1557 return $header_images;
1561 * Add callbacks for image header display.
1563 * The parameter $header_callback callback will be required to display the
1564 * content for the 'wp_head' action. The parameter $admin_header_callback
1565 * callback will be added to Custom_Image_Header class and that will be added
1566 * to the 'admin_menu' action.
1569 * @uses Custom_Image_Header Sets up for $admin_header_callback for administration panel display.
1571 * @param callback $header_callback Call on 'wp_head' action.
1572 * @param callback $admin_header_callback Call on custom header administration screen.
1573 * @param callback $admin_image_div_callback Output a custom header image div on the custom header administration screen. Optional.
1575 function add_custom_image_header( $header_callback, $admin_header_callback, $admin_image_div_callback = '' ) {
1576 if ( ! empty( $header_callback ) )
1577 add_action('wp_head', $header_callback);
1579 $support = array( 'callback' => $header_callback );
1580 $theme_support = get_theme_support( 'custom-header' );
1581 if ( ! empty( $theme_support ) && is_array( $theme_support[ 0 ] ) )
1582 $support = array_merge( $theme_support[ 0 ], $support );
1583 add_theme_support( 'custom-header', $support );
1584 add_theme_support( 'custom-header-uploads' );
1589 global $custom_image_header;
1591 require_once( ABSPATH . 'wp-admin/custom-header.php' );
1592 $custom_image_header = new Custom_Image_Header( $admin_header_callback, $admin_image_div_callback );
1593 add_action( 'admin_menu', array( &$custom_image_header, 'init' ) );
1597 * Remove image header support.
1600 * @see add_custom_image_header()
1602 * @return bool Whether support was removed.
1604 function remove_custom_image_header() {
1605 if ( ! current_theme_supports( 'custom-header' ) )
1608 $callback = get_theme_support( 'custom-header' );
1609 remove_action( 'wp_head', $callback[0]['callback'] );
1610 _remove_theme_support( 'custom-header' );
1611 remove_theme_support( 'custom-header-uploads' );
1614 remove_action( 'admin_menu', array( &$GLOBALS['custom_image_header'], 'init' ) );
1615 unset( $GLOBALS['custom_image_header'] );
1622 * Register a selection of default headers to be displayed by the custom header admin UI.
1626 * @param array $headers Array of headers keyed by a string id. The ids point to arrays containing 'url', 'thumbnail_url', and 'description' keys.
1628 function register_default_headers( $headers ) {
1629 global $_wp_default_headers;
1631 $_wp_default_headers = array_merge( (array) $_wp_default_headers, (array) $headers );
1635 * Unregister default headers.
1637 * This function must be called after register_default_headers() has already added the
1638 * header you want to remove.
1640 * @see register_default_headers()
1643 * @param string|array $header The header string id (key of array) to remove, or an array thereof.
1644 * @return True on success, false on failure.
1646 function unregister_default_headers( $header ) {
1647 global $_wp_default_headers;
1648 if ( is_array( $header ) ) {
1649 array_map( 'unregister_default_headers', $header );
1650 } elseif ( isset( $_wp_default_headers[ $header ] ) ) {
1651 unset( $_wp_default_headers[ $header ] );
1659 * Retrieve background image for custom background.
1665 function get_background_image() {
1666 $default = defined('BACKGROUND_IMAGE') ? BACKGROUND_IMAGE : '';
1668 return get_theme_mod('background_image', $default);
1672 * Display background image path.
1676 function background_image() {
1677 echo get_background_image();
1681 * Retrieve value for custom background color.
1684 * @uses BACKGROUND_COLOR
1688 function get_background_color() {
1689 $default = defined('BACKGROUND_COLOR') ? BACKGROUND_COLOR : '';
1691 return get_theme_mod('background_color', $default);
1695 * Display background color value.
1699 function background_color() {
1700 echo get_background_color();
1704 * Add callbacks for background image display.
1706 * The parameter $header_callback callback will be required to display the
1707 * content for the 'wp_head' action. The parameter $admin_header_callback
1708 * callback will be added to Custom_Background class and that will be added
1709 * to the 'admin_menu' action.
1712 * @uses Custom_Background Sets up for $admin_header_callback for administration panel display.
1714 * @param callback $header_callback Call on 'wp_head' action.
1715 * @param callback $admin_header_callback Call on custom background administration screen.
1716 * @param callback $admin_image_div_callback Output a custom background image div on the custom background administration screen. Optional.
1718 function add_custom_background( $header_callback = '', $admin_header_callback = '', $admin_image_div_callback = '' ) {
1719 if ( isset( $GLOBALS['custom_background'] ) )
1722 if ( empty( $header_callback ) )
1723 $header_callback = '_custom_background_cb';
1725 add_action( 'wp_head', $header_callback );
1727 add_theme_support( 'custom-background', array( 'callback' => $header_callback ) );
1731 require_once( ABSPATH . 'wp-admin/custom-background.php' );
1732 $GLOBALS['custom_background'] = new Custom_Background( $admin_header_callback, $admin_image_div_callback );
1733 add_action( 'admin_menu', array( &$GLOBALS['custom_background'], 'init' ) );
1737 * Remove custom background support.
1740 * @see add_custom_background()
1742 * @return bool Whether support was removed.
1744 function remove_custom_background() {
1745 if ( ! current_theme_supports( 'custom-background' ) )
1748 $callback = get_theme_support( 'custom-background' );
1749 remove_action( 'wp_head', $callback[0]['callback'] );
1750 _remove_theme_support( 'custom-background' );
1753 remove_action( 'admin_menu', array( &$GLOBALS['custom_background'], 'init' ) );
1754 unset( $GLOBALS['custom_background'] );
1761 * Default custom background callback.
1764 * @see add_custom_background()
1767 function _custom_background_cb() {
1768 $background = get_background_image();
1769 $color = get_background_color();
1770 if ( ! $background && ! $color )
1773 $style = $color ? "background-color: #$color;" : '';
1775 if ( $background ) {
1776 $image = " background-image: url('$background');";
1778 $repeat = get_theme_mod( 'background_repeat', 'repeat' );
1779 if ( ! in_array( $repeat, array( 'no-repeat', 'repeat-x', 'repeat-y', 'repeat' ) ) )
1781 $repeat = " background-repeat: $repeat;";
1783 $position = get_theme_mod( 'background_position_x', 'left' );
1784 if ( ! in_array( $position, array( 'center', 'right', 'left' ) ) )
1786 $position = " background-position: top $position;";
1788 $attachment = get_theme_mod( 'background_attachment', 'scroll' );
1789 if ( ! in_array( $attachment, array( 'fixed', 'scroll' ) ) )
1790 $attachment = 'scroll';
1791 $attachment = " background-attachment: $attachment;";
1793 $style .= $image . $repeat . $position . $attachment;
1796 <style type="text/css">
1797 body.custom-background { <?php echo trim( $style ); ?> }
1803 * Add callback for custom TinyMCE editor stylesheets.
1805 * The parameter $stylesheet is the name of the stylesheet, relative to
1806 * the theme root. It also accepts an array of stylesheets.
1807 * It is optional and defaults to 'editor-style.css'.
1809 * Supports RTL stylesheets automatically by searching for the -rtl prefix, e.g.
1810 * editor-style-rtl.css. If an array of stylesheets is passed to add_editor_style(),
1811 * RTL is only added for the first stylesheet.
1815 * @param mixed $stylesheet Optional. Stylesheet name or array thereof, relative to theme root.
1816 * Defaults to 'editor-style.css'
1818 function add_editor_style( $stylesheet = 'editor-style.css' ) {
1820 add_theme_support( 'editor-style' );
1825 global $editor_styles;
1826 $editor_styles = (array) $editor_styles;
1827 $stylesheet = (array) $stylesheet;
1829 $rtl_stylesheet = str_replace('.css', '-rtl.css', $stylesheet[0]);
1830 $stylesheet[] = $rtl_stylesheet;
1833 $editor_styles = array_merge( $editor_styles, $stylesheet );
1837 * Removes all visual editor stylesheets.
1841 * @return bool True on success, false if there were no stylesheets to remove.
1843 function remove_editor_styles() {
1844 if ( ! current_theme_supports( 'editor-style' ) )
1846 _remove_theme_support( 'editor-style' );
1848 $GLOBALS['editor_styles'] = array();
1853 * Allows a theme to register its support of a certain feature
1855 * Must be called in the theme's functions.php file to work.
1856 * If attached to a hook, it must be after_setup_theme.
1857 * The init hook may be too late for some features.
1860 * @param string $feature the feature being added
1862 function add_theme_support( $feature ) {
1863 global $_wp_theme_features;
1865 if ( func_num_args() == 1 )
1866 $_wp_theme_features[$feature] = true;
1868 $_wp_theme_features[$feature] = array_slice( func_get_args(), 1 );
1870 if ( $feature == 'post-formats' && is_array( $_wp_theme_features[$feature][0] ) )
1871 $_wp_theme_features[$feature][0] = array_intersect( $_wp_theme_features[$feature][0], array_keys( get_post_format_slugs() ) );
1875 * Gets the theme support arguments passed when registering that support
1878 * @param string $feature the feature to check
1879 * @return array The array of extra arguments
1881 function get_theme_support( $feature ) {
1882 global $_wp_theme_features;
1883 if ( !isset( $_wp_theme_features[$feature] ) )
1886 return $_wp_theme_features[$feature];
1890 * Allows a theme to de-register its support of a certain feature
1892 * Should be called in the theme's functions.php file. Generally would
1893 * be used for child themes to override support from the parent theme.
1896 * @see add_theme_support()
1897 * @param string $feature the feature being added
1898 * @return bool Whether feature was removed.
1900 function remove_theme_support( $feature ) {
1901 // Blacklist: for internal registrations not used directly by themes.
1902 if ( in_array( $feature, array( 'custom-background', 'custom-header', 'editor-style', 'widgets', 'menus' ) ) )
1904 return _remove_theme_support( $feature );
1908 * Do not use. Removes theme support internally, ignorant of the blacklist.
1913 function _remove_theme_support( $feature ) {
1914 global $_wp_theme_features;
1916 if ( ! isset( $_wp_theme_features[$feature] ) )
1918 unset( $_wp_theme_features[$feature] );
1923 * Checks a theme's support for a given feature
1926 * @param string $feature the feature being checked
1929 function current_theme_supports( $feature ) {
1930 global $_wp_theme_features;
1932 if ( !isset( $_wp_theme_features[$feature] ) )
1935 // If no args passed then no extra checks need be performed
1936 if ( func_num_args() <= 1 )
1939 $args = array_slice( func_get_args(), 1 );
1941 // @todo Allow pluggable arg checking
1942 switch ( $feature ) {
1943 case 'post-thumbnails':
1944 // post-thumbnails can be registered for only certain content/post types by passing
1945 // an array of types to add_theme_support(). If no array was passed, then
1946 // any type is accepted
1947 if ( true === $_wp_theme_features[$feature] ) // Registered for all types
1949 $content_type = $args[0];
1950 return in_array( $content_type, $_wp_theme_features[$feature][0] );
1953 case 'post-formats':
1954 // specific post formats can be registered by passing an array of types to
1955 // add_theme_support()
1956 $post_format = $args[0];
1957 return in_array( $post_format, $_wp_theme_features[$feature][0] );
1965 * Checks a theme's support for a given feature before loading the functions which implement it.
1968 * @param string $feature the feature being checked
1969 * @param string $include the file containing the functions that implement the feature
1971 function require_if_theme_supports( $feature, $include) {
1972 if ( current_theme_supports( $feature ) )
1973 require ( $include );
1977 * Checks an attachment being deleted to see if it's a header or background image.
1979 * If true it removes the theme modification which would be pointing at the deleted
1984 * @param int $id the attachment id
1986 function _delete_attachment_theme_mod( $id ) {
1987 $attachment_image = wp_get_attachment_url( $id );
1988 $header_image = get_header_image();
1989 $background_image = get_background_image();
1991 if ( $header_image && $header_image == $attachment_image )
1992 remove_theme_mod( 'header_image' );
1994 if ( $background_image && $background_image == $attachment_image )
1995 remove_theme_mod( 'background_image' );
1998 add_action( 'delete_attachment', '_delete_attachment_theme_mod' );
2001 * Checks if a theme has been changed and runs 'after_switch_theme' hook on the next WP load
2005 function check_theme_switched() {
2006 if ( false !== ( $old_theme = get_option( 'theme_switched' ) ) && !empty( $old_theme ) ) {
2007 do_action( 'after_switch_theme', $old_theme );
2008 update_option( 'theme_switched', false );