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'], __( '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 /* Register the default root as a theme directory */
271 register_theme_directory( get_theme_root() );
273 if ( !$theme_files = search_theme_directories() )
276 asort( $theme_files );
278 $wp_themes = array();
280 foreach ( (array) $theme_files as $theme_file ) {
281 $theme_root = $theme_file['theme_root'];
282 $theme_file = $theme_file['theme_file'];
284 if ( !is_readable("$theme_root/$theme_file") ) {
285 $wp_broken_themes[$theme_file] = array('Name' => $theme_file, 'Title' => $theme_file, 'Description' => __('File not readable.'));
289 $theme_data = get_theme_data("$theme_root/$theme_file");
291 $name = $theme_data['Name'];
292 $title = $theme_data['Title'];
293 $description = wptexturize($theme_data['Description']);
294 $version = $theme_data['Version'];
295 $author = $theme_data['Author'];
296 $template = $theme_data['Template'];
297 $stylesheet = dirname($theme_file);
300 foreach ( array('png', 'gif', 'jpg', 'jpeg') as $ext ) {
301 if (file_exists("$theme_root/$stylesheet/screenshot.$ext")) {
302 $screenshot = "screenshot.$ext";
307 if ( empty($name) ) {
308 $name = dirname($theme_file);
312 $parent_template = $template;
314 if ( empty($template) ) {
315 if ( file_exists("$theme_root/$stylesheet/index.php") )
316 $template = $stylesheet;
321 $template = trim( $template );
323 if ( !file_exists("$theme_root/$template/index.php") ) {
324 $parent_dir = dirname(dirname($theme_file));
325 if ( file_exists("$theme_root/$parent_dir/$template/index.php") ) {
326 $template = "$parent_dir/$template";
327 $template_directory = "$theme_root/$template";
330 * The parent theme doesn't exist in the current theme's folder or sub folder
331 * so lets use the theme root for the parent template.
333 if ( isset($theme_files[$template]) && file_exists( $theme_files[$template]['theme_root'] . "/$template/index.php" ) ) {
334 $template_directory = $theme_files[$template]['theme_root'] . "/$template";
336 if ( empty( $parent_template) )
337 $wp_broken_themes[$name] = array('Name' => $name, 'Title' => $title, 'Description' => __('Template is missing.'), 'error' => 'no_template');
339 $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 );
345 $template_directory = trim( $theme_root . '/' . $template );
348 $stylesheet_files = array();
349 $template_files = array();
351 $stylesheet_dir = @ dir("$theme_root/$stylesheet");
352 if ( $stylesheet_dir ) {
353 while ( ($file = $stylesheet_dir->read()) !== false ) {
354 if ( !preg_match('|^\.+$|', $file) ) {
355 if ( preg_match('|\.css$|', $file) )
356 $stylesheet_files[] = "$theme_root/$stylesheet/$file";
357 elseif ( preg_match('|\.php$|', $file) )
358 $template_files[] = "$theme_root/$stylesheet/$file";
361 @ $stylesheet_dir->close();
364 $template_dir = @ dir("$template_directory");
365 if ( $template_dir ) {
366 while ( ($file = $template_dir->read()) !== false ) {
367 if ( preg_match('|^\.+$|', $file) )
369 if ( preg_match('|\.php$|', $file) ) {
370 $template_files[] = "$template_directory/$file";
371 } elseif ( is_dir("$template_directory/$file") ) {
372 $template_subdir = @ dir("$template_directory/$file");
373 if ( !$template_subdir )
375 while ( ($subfile = $template_subdir->read()) !== false ) {
376 if ( preg_match('|^\.+$|', $subfile) )
378 if ( preg_match('|\.php$|', $subfile) )
379 $template_files[] = "$template_directory/$file/$subfile";
381 @ $template_subdir->close();
384 @ $template_dir->close();
387 //Make unique and remove duplicates when stylesheet and template are the same i.e. most themes
388 $template_files = array_unique($template_files);
389 $stylesheet_files = array_unique($stylesheet_files);
391 $template_dir = dirname($template_files[0]);
392 $stylesheet_dir = dirname($stylesheet_files[0]);
394 if ( empty($template_dir) )
396 if ( empty($stylesheet_dir) )
397 $stylesheet_dir = '/';
399 // Check for theme name collision. This occurs if a theme is copied to
400 // a new theme directory and the theme header is not updated. Whichever
401 // theme is first keeps the name. Subsequent themes get a suffix applied.
402 // The Default and Classic themes always trump their pretenders.
403 if ( isset($wp_themes[$name]) ) {
404 if ( ('WordPress Default' == $name || 'WordPress Classic' == $name) &&
405 ('default' == $stylesheet || 'classic' == $stylesheet) ) {
406 // If another theme has claimed to be one of our default themes, move
408 $suffix = $wp_themes[$name]['Stylesheet'];
409 $new_name = "$name/$suffix";
410 $wp_themes[$new_name] = $wp_themes[$name];
411 $wp_themes[$new_name]['Name'] = $new_name;
413 $name = "$name/$stylesheet";
417 $theme_roots[$stylesheet] = str_replace( WP_CONTENT_DIR, '', $theme_root );
418 $wp_themes[$name] = array(
421 'Description' => $description,
423 'Author Name' => $theme_data['AuthorName'],
424 'Author URI' => $theme_data['AuthorURI'],
425 'Version' => $version,
426 'Template' => $template,
427 'Stylesheet' => $stylesheet,
428 'Template Files' => $template_files,
429 'Stylesheet Files' => $stylesheet_files,
430 'Template Dir' => $template_dir,
431 'Stylesheet Dir' => $stylesheet_dir,
432 'Status' => $theme_data['Status'],
433 'Screenshot' => $screenshot,
434 'Tags' => $theme_data['Tags'],
435 'Theme Root' => $theme_root,
436 'Theme Root URI' => str_replace( WP_CONTENT_DIR, content_url(), $theme_root ),
442 /* Store theme roots in the DB */
443 if ( get_site_transient( 'theme_roots' ) != $theme_roots )
444 set_site_transient( 'theme_roots', $theme_roots, 7200 ); // cache for two hours
447 /* Resolve theme dependencies. */
448 $theme_names = array_keys( $wp_themes );
449 foreach ( (array) $theme_names as $theme_name ) {
450 $wp_themes[$theme_name]['Parent Theme'] = '';
451 if ( $wp_themes[$theme_name]['Stylesheet'] != $wp_themes[$theme_name]['Template'] ) {
452 foreach ( (array) $theme_names as $parent_theme_name ) {
453 if ( ($wp_themes[$parent_theme_name]['Stylesheet'] == $wp_themes[$parent_theme_name]['Template']) && ($wp_themes[$parent_theme_name]['Template'] == $wp_themes[$theme_name]['Template']) ) {
454 $wp_themes[$theme_name]['Parent Theme'] = $wp_themes[$parent_theme_name]['Name'];
465 * Retrieve theme roots.
469 * @return array Theme roots
471 function get_theme_roots() {
472 $theme_roots = get_site_transient( 'theme_roots' );
473 if ( false === $theme_roots ) {
475 $theme_roots = get_site_transient( 'theme_roots' ); // this is set in get_theme()
481 * Retrieve theme data.
485 * @param string $theme Theme name.
486 * @return array|null Null, if theme name does not exist. Theme data, if exists.
488 function get_theme($theme) {
489 $themes = get_themes();
491 if ( array_key_exists($theme, $themes) )
492 return $themes[$theme];
498 * Retrieve current theme display name.
500 * If the 'current_theme' option has already been set, then it will be returned
501 * instead. If it is not set, then each theme will be iterated over until both
502 * the current stylesheet and current template name.
508 function get_current_theme() {
509 if ( $theme = get_option('current_theme') )
512 $themes = get_themes();
513 $theme_names = array_keys($themes);
514 $current_template = get_option('template');
515 $current_stylesheet = get_option('stylesheet');
516 $current_theme = 'WordPress Default';
519 foreach ( (array) $theme_names as $theme_name ) {
520 if ( $themes[$theme_name]['Stylesheet'] == $current_stylesheet &&
521 $themes[$theme_name]['Template'] == $current_template ) {
522 $current_theme = $themes[$theme_name]['Name'];
528 update_option('current_theme', $current_theme);
530 return $current_theme;
534 * Register a directory that contains themes.
538 * @param string $directory Either the full filesystem path to a theme folder or a folder within WP_CONTENT_DIR
541 function register_theme_directory( $directory) {
542 global $wp_theme_directories;
544 /* If this folder does not exist, return and do not register */
545 if ( !file_exists( $directory ) )
546 /* Try prepending as the theme directory could be relative to the content directory */
547 $registered_directory = WP_CONTENT_DIR . '/' . $directory;
549 $registered_directory = $directory;
551 /* If this folder does not exist, return and do not register */
552 if ( !file_exists( $registered_directory ) )
555 $wp_theme_directories[] = $registered_directory;
561 * Search all registered theme directories for complete and valid themes.
565 * @return array Valid themes found
567 function search_theme_directories() {
568 global $wp_theme_directories, $wp_broken_themes;
569 if ( empty( $wp_theme_directories ) )
572 $theme_files = array();
573 $wp_broken_themes = array();
575 /* Loop the registered theme directories and extract all themes */
576 foreach ( (array) $wp_theme_directories as $theme_root ) {
577 $theme_loc = $theme_root;
579 /* We don't want to replace all forward slashes, see Trac #4541 */
580 if ( '/' != WP_CONTENT_DIR )
581 $theme_loc = str_replace(WP_CONTENT_DIR, '', $theme_root);
583 /* Files in the root of the current theme directory and one subdir down */
584 $themes_dir = @ opendir($theme_root);
589 while ( ($theme_dir = readdir($themes_dir)) !== false ) {
590 if ( is_dir($theme_root . '/' . $theme_dir) && is_readable($theme_root . '/' . $theme_dir) ) {
591 if ( $theme_dir{0} == '.' || $theme_dir == 'CVS' )
594 $stylish_dir = @opendir($theme_root . '/' . $theme_dir);
595 $found_stylesheet = false;
597 while ( ($theme_file = readdir($stylish_dir)) !== false ) {
598 if ( $theme_file == 'style.css' ) {
599 $theme_files[$theme_dir] = array( 'theme_file' => $theme_dir . '/' . $theme_file, 'theme_root' => $theme_root );
600 $found_stylesheet = true;
604 @closedir($stylish_dir);
606 if ( !$found_stylesheet ) { // look for themes in that dir
607 $subdir = "$theme_root/$theme_dir";
608 $subdir_name = $theme_dir;
609 $theme_subdirs = @opendir( $subdir );
611 $found_subdir_themes = false;
612 while ( ($theme_subdir = readdir($theme_subdirs)) !== false ) {
613 if ( is_dir( $subdir . '/' . $theme_subdir) && is_readable($subdir . '/' . $theme_subdir) ) {
614 if ( $theme_subdir{0} == '.' || $theme_subdir == 'CVS' )
617 $stylish_dir = @opendir($subdir . '/' . $theme_subdir);
618 $found_stylesheet = false;
620 while ( ($theme_file = readdir($stylish_dir)) !== false ) {
621 if ( $theme_file == 'style.css' ) {
622 $theme_files["$theme_dir/$theme_subdir"] = array( 'theme_file' => $subdir_name . '/' . $theme_subdir . '/' . $theme_file, 'theme_root' => $theme_root );
623 $found_stylesheet = true;
624 $found_subdir_themes = true;
628 @closedir($stylish_dir);
631 @closedir($theme_subdirs);
632 if ( !$found_subdir_themes )
633 $wp_broken_themes[$theme_dir] = array('Name' => $theme_dir, 'Title' => $theme_dir, 'Description' => __('Stylesheet is missing.'));
637 @closedir( $themes_dir );
643 * Retrieve path to themes directory.
645 * Does not have trailing slash.
648 * @param $stylesheet_or_template The stylesheet or template name of the theme
649 * @uses apply_filters() Calls 'theme_root' filter on path.
651 * @return string Theme path.
653 function get_theme_root( $stylesheet_or_template = false ) {
654 if ($stylesheet_or_template) {
655 $theme_roots = get_theme_roots();
657 if ( ! empty( $theme_roots[$stylesheet_or_template] ) )
658 $theme_root = WP_CONTENT_DIR . $theme_roots[$stylesheet_or_template];
660 $theme_root = WP_CONTENT_DIR . '/themes';
662 $theme_root = WP_CONTENT_DIR . '/themes';
665 return apply_filters( 'theme_root', $theme_root );
669 * Retrieve URI for themes directory.
671 * Does not have trailing slash.
674 * @param $stylesheet_or_template The stylesheet or template name of the theme
676 * @return string Themes URI.
678 function get_theme_root_uri( $stylesheet_or_template = false ) {
679 $theme_roots = get_theme_roots();
681 if ( isset( $theme_roots[$stylesheet_or_template] ) && $theme_roots[$stylesheet_or_template] )
682 $theme_root_uri = content_url( $theme_roots[$stylesheet_or_template] );
684 $theme_root_uri = content_url( 'themes' );
686 return apply_filters( 'theme_root_uri', $theme_root_uri, get_option('siteurl'), $stylesheet_or_template );
690 * Retrieve path to file without the use of extension.
692 * Used to quickly retrieve the path of file without including the file
693 * extension. It will also check the parent template, if the file exists, with
694 * the use of {@link locate_template()}. Allows for more generic file location
695 * without the use of the other get_*_template() functions.
697 * Can be used with include() or require() to retrieve path.
699 * if( '' != get_query_template( '404' ) )
700 * include( get_query_template( '404' ) );
702 * or the same can be accomplished with
704 * if( '' != get_404_template() )
705 * include( get_404_template() );
710 * @param string $type Filename without extension.
711 * @return string Full path to file.
713 function get_query_template($type) {
714 $type = preg_replace( '|[^a-z0-9-]+|', '', $type );
715 return apply_filters("{$type}_template", locate_template(array("{$type}.php")));
719 * Retrieve path of index template in current or parent template.
725 function get_index_template() {
726 return get_query_template('index');
730 * Retrieve path of 404 template in current or parent template.
736 function get_404_template() {
737 return get_query_template('404');
741 * Retrieve path of archive template in current or parent template.
747 function get_archive_template() {
748 return get_query_template('archive');
752 * Retrieve path of author template in current or parent template.
758 function get_author_template() {
759 $author_id = absint( get_query_var( 'author' ) );
760 $author = get_user_by( 'id', $author_id );
761 $author = $author->user_nicename;
763 $templates = array();
766 $templates[] = "author-{$author}.php";
768 $templates[] = "author-{$author_id}.php";
769 $templates[] = 'author.php';
771 $template = locate_template( $templates );
772 return apply_filters( 'author_template', $template );
776 * Retrieve path of category template in current or parent template.
778 * Works by first retrieving the current slug for example 'category-default.php' and then
779 * trying category ID, for example 'category-1.php' and will finally fallback to category.php
780 * template, if those files don't exist.
783 * @uses apply_filters() Calls 'category_template' on file path of category template.
787 function get_category_template() {
788 $cat_ID = absint( get_query_var('cat') );
789 $category = get_category( $cat_ID );
791 $templates = array();
793 if ( !is_wp_error($category) )
794 $templates[] = "category-{$category->slug}.php";
796 $templates[] = "category-$cat_ID.php";
797 $templates[] = "category.php";
799 $template = locate_template($templates);
800 return apply_filters('category_template', $template);
804 * Retrieve path of tag template in current or parent template.
806 * Works by first retrieving the current tag name, for example 'tag-wordpress.php' and then
807 * trying tag ID, for example 'tag-1.php' and will finally fallback to tag.php
808 * template, if those files don't exist.
811 * @uses apply_filters() Calls 'tag_template' on file path of tag template.
815 function get_tag_template() {
816 $tag_id = absint( get_query_var('tag_id') );
817 $tag_name = get_query_var('tag');
819 $templates = array();
822 $templates[] = "tag-$tag_name.php";
824 $templates[] = "tag-$tag_id.php";
825 $templates[] = "tag.php";
827 $template = locate_template($templates);
828 return apply_filters('tag_template', $template);
832 * Retrieve path of taxonomy template in current or parent template.
834 * Retrieves the taxonomy and term, if term is available. The template is
835 * prepended with 'taxonomy-' and followed by both the taxonomy string and
836 * the taxonomy string followed by a dash and then followed by the term.
838 * The taxonomy and term template is checked and used first, if it exists.
839 * Second, just the taxonomy template is checked, and then finally, taxonomy.php
840 * template is used. If none of the files exist, then it will fall back on to
843 * @since unknown (2.6.0 most likely)
844 * @uses apply_filters() Calls 'taxonomy_template' filter on found path.
848 function get_taxonomy_template() {
849 $taxonomy = get_query_var('taxonomy');
850 $term = get_query_var('term');
852 $templates = array();
853 if ( $taxonomy && $term )
854 $templates[] = "taxonomy-$taxonomy-$term.php";
856 $templates[] = "taxonomy-$taxonomy.php";
858 $templates[] = "taxonomy.php";
860 $template = locate_template($templates);
861 return apply_filters('taxonomy_template', $template);
865 * Retrieve path of date template in current or parent template.
871 function get_date_template() {
872 return get_query_template('date');
876 * Retrieve path of home template in current or parent template.
878 * This is the template used for the page containing the blog posts
880 * Attempts to locate 'home.php' first before falling back to 'index.php'.
883 * @uses apply_filters() Calls 'home_template' on file path of home template.
887 function get_home_template() {
888 $template = locate_template(array('home.php', 'index.php'));
889 return apply_filters('home_template', $template);
893 * Retrieve path of front-page template in current or parent template.
895 * Looks for 'front-page.php'.
898 * @uses apply_filters() Calls 'front_page_template' on file path of template.
902 function get_front_page_template() {
903 return apply_filters( 'front_page_template', locate_template( array('front-page.php') ) );
907 * Retrieve path of page template in current or parent template.
909 * Will first look for the specifically assigned page template
910 * The will search for 'page-{slug}.php' followed by 'page-id.php'
911 * and finally 'page.php'
917 function get_page_template() {
920 $id = (int) $wp_query->get_queried_object_id();
921 $template = get_post_meta($id, '_wp_page_template', true);
922 $pagename = get_query_var('pagename');
924 if ( !$pagename && $id > 0 ) {
925 // If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object
926 $post = $wp_query->get_queried_object();
927 $pagename = $post->post_name;
930 if ( 'default' == $template )
933 $templates = array();
934 if ( !empty($template) && !validate_file($template) )
935 $templates[] = $template;
937 $templates[] = "page-$pagename.php";
939 $templates[] = "page-$id.php";
940 $templates[] = "page.php";
942 return apply_filters('page_template', locate_template($templates));
946 * Retrieve path of paged template in current or parent template.
952 function get_paged_template() {
953 return get_query_template('paged');
957 * Retrieve path of search template in current or parent template.
963 function get_search_template() {
964 return get_query_template('search');
968 * Retrieve path of single template in current or parent template.
974 function get_single_template() {
977 $object = $wp_query->get_queried_object();
978 $templates = array('single-' . $object->post_type . '.php', 'single.php');
979 return apply_filters('single_template', locate_template($templates));
983 * Retrieve path of attachment template in current or parent template.
985 * The attachment path first checks if the first part of the mime type exists.
986 * The second check is for the second part of the mime type. The last check is
987 * for both types separated by an underscore. If neither are found then the file
988 * 'attachment.php' is checked and returned.
990 * Some examples for the 'text/plain' mime type are 'text.php', 'plain.php', and
991 * finally 'text_plain.php'.
997 function get_attachment_template() {
999 $type = explode('/', $posts[0]->post_mime_type);
1000 if ( $template = get_query_template($type[0]) )
1002 elseif ( $template = get_query_template($type[1]) )
1004 elseif ( $template = get_query_template("$type[0]_$type[1]") )
1007 return get_query_template('attachment');
1011 * Retrieve path of comment popup template in current or parent template.
1013 * Checks for comment popup template in current template, if it exists or in the
1017 * @uses apply_filters() Calls 'comments_popup_template' filter on path.
1021 function get_comments_popup_template() {
1022 $template = locate_template(array("comments-popup.php"));
1024 // Backward compat code will be removed in a future release
1025 if ('' == $template)
1026 $template = WPINC . '/theme-compat/comments-popup.php';
1028 return apply_filters('comments_popup_template', $template);
1032 * Retrieve the name of the highest priority template file that exists.
1034 * Searches in the STYLESHEETPATH before TEMPLATEPATH so that themes which
1035 * inherit from a parent theme can just overload one file.
1039 * @param array $template_names Array of template files to search for in priority order.
1040 * @param bool $load If true the template file will be loaded if it is found.
1041 * @param bool $require_once Whether to require_once or require. Default true. Has no effect if $load is false.
1042 * @return string The template filename if one is located.
1044 function locate_template($template_names, $load = false, $require_once = true ) {
1045 if ( !is_array($template_names) )
1049 foreach ( $template_names as $template_name ) {
1050 if ( !$template_name )
1052 if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
1053 $located = STYLESHEETPATH . '/' . $template_name;
1055 } else if ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
1056 $located = TEMPLATEPATH . '/' . $template_name;
1061 if ( $load && '' != $located )
1062 load_template( $located, $require_once );
1068 * Require the template file with WordPress environment.
1070 * The globals are set up for the template file to ensure that the WordPress
1071 * environment is available from within the function. The query variables are
1076 * @param string $_template_file Path to template file.
1077 * @param bool $require_once Whether to require_once or require. Default true.
1079 function load_template( $_template_file, $require_once = true ) {
1080 global $posts, $post, $wp_did_header, $wp_did_template_redirect, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
1082 if ( is_array( $wp_query->query_vars ) )
1083 extract( $wp_query->query_vars, EXTR_SKIP );
1085 if ( $require_once )
1086 require_once( $_template_file );
1088 require( $_template_file );
1092 * Display localized stylesheet link element.
1096 function locale_stylesheet() {
1097 $stylesheet = get_locale_stylesheet_uri();
1098 if ( empty($stylesheet) )
1100 echo '<link rel="stylesheet" href="' . $stylesheet . '" type="text/css" media="screen" />';
1104 * Start preview theme output buffer.
1106 * Will only preform task if the user has permissions and template and preview
1107 * query variables exist.
1111 function preview_theme() {
1112 if ( ! (isset($_GET['template']) && isset($_GET['preview'])) )
1115 if ( !current_user_can( 'switch_themes' ) )
1118 $_GET['template'] = preg_replace('|[^a-z0-9_./-]|i', '', $_GET['template']);
1120 if ( validate_file($_GET['template']) )
1123 add_filter( 'template', '_preview_theme_template_filter' );
1125 if ( isset($_GET['stylesheet']) ) {
1126 $_GET['stylesheet'] = preg_replace('|[^a-z0-9_./-]|i', '', $_GET['stylesheet']);
1127 if ( validate_file($_GET['stylesheet']) )
1129 add_filter( 'stylesheet', '_preview_theme_stylesheet_filter' );
1132 // Prevent theme mods to current theme being used on theme being previewed
1133 add_filter( 'pre_option_mods_' . get_current_theme(), '__return_empty_array' );
1135 ob_start( 'preview_theme_ob_filter' );
1137 add_action('setup_theme', 'preview_theme');
1140 * Private function to modify the current template when previewing a theme
1147 function _preview_theme_template_filter() {
1148 return isset($_GET['template']) ? $_GET['template'] : '';
1152 * Private function to modify the current stylesheet when previewing a theme
1159 function _preview_theme_stylesheet_filter() {
1160 return isset($_GET['stylesheet']) ? $_GET['stylesheet'] : '';
1164 * Callback function for ob_start() to capture all links in the theme.
1169 * @param string $content
1172 function preview_theme_ob_filter( $content ) {
1173 return preg_replace_callback( "|(<a.*?href=([\"']))(.*?)([\"'].*?>)|", 'preview_theme_ob_filter_callback', $content );
1177 * Manipulates preview theme links in order to control and maintain location.
1179 * Callback function for preg_replace_callback() to accept and filter matches.
1184 * @param array $matches
1187 function preview_theme_ob_filter_callback( $matches ) {
1188 if ( strpos($matches[4], 'onclick') !== false )
1189 $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.
1191 ( false !== strpos($matches[3], '/wp-admin/') )
1193 ( false !== strpos( $matches[3], '://' ) && 0 !== strpos( $matches[3], home_url() ) )
1195 ( false !== strpos($matches[3], '/feed/') )
1197 ( false !== strpos($matches[3], '/trackback/') )
1199 return $matches[1] . "#$matches[2] onclick=$matches[2]return false;" . $matches[4];
1201 $link = add_query_arg( array('preview' => 1, 'template' => $_GET['template'], 'stylesheet' => @$_GET['stylesheet'] ), $matches[3] );
1202 if ( 0 === strpos($link, 'preview=1') )
1204 return $matches[1] . esc_attr( $link ) . $matches[4];
1208 * Switches current theme to new template and stylesheet names.
1211 * @uses do_action() Calls 'switch_theme' action on updated theme display name.
1213 * @param string $template Template name
1214 * @param string $stylesheet Stylesheet name.
1216 function switch_theme($template, $stylesheet) {
1217 update_option('template', $template);
1218 update_option('stylesheet', $stylesheet);
1219 delete_option('current_theme');
1220 $theme = get_current_theme();
1221 do_action('switch_theme', $theme);
1225 * Checks that current theme files 'index.php' and 'style.css' exists.
1227 * Does not check the default theme, which is the fallback and should always exist.
1228 * Will switch theme to the fallback theme if current theme does not validate.
1229 * You can use the 'validate_current_theme' filter to return FALSE to
1230 * disable this functionality.
1233 * @see WP_DEFAULT_THEME
1237 function validate_current_theme() {
1238 // Don't validate during an install/upgrade.
1239 if ( defined('WP_INSTALLING') || !apply_filters( 'validate_current_theme', true ) )
1242 if ( get_template() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/index.php') ) {
1243 switch_theme( WP_DEFAULT_THEME, WP_DEFAULT_THEME );
1247 if ( get_stylesheet() != WP_DEFAULT_THEME && !file_exists(get_template_directory() . '/style.css') ) {
1248 switch_theme( WP_DEFAULT_THEME, WP_DEFAULT_THEME );
1256 * Retrieve theme modification value for the current theme.
1258 * If the modification name does not exist, then the $default will be passed
1259 * through {@link http://php.net/sprintf sprintf()} PHP function with the first
1260 * string the template directory URI and the second string the stylesheet
1264 * @uses apply_filters() Calls 'theme_mod_$name' filter on the value.
1266 * @param string $name Theme modification name.
1267 * @param bool|string $default
1270 function get_theme_mod($name, $default = false) {
1271 $theme = get_current_theme();
1273 $mods = get_option( "mods_$theme" );
1275 if ( isset($mods[$name]) )
1276 return apply_filters( "theme_mod_$name", $mods[$name] );
1278 return apply_filters( "theme_mod_$name", sprintf($default, get_template_directory_uri(), get_stylesheet_directory_uri()) );
1282 * Update theme modification value for the current theme.
1286 * @param string $name Theme modification name.
1287 * @param string $value theme modification value.
1289 function set_theme_mod($name, $value) {
1290 $theme = get_current_theme();
1292 $mods = get_option("mods_$theme");
1294 $mods[$name] = $value;
1296 update_option("mods_$theme", $mods);
1297 wp_cache_delete("mods_$theme", 'options');
1301 * Remove theme modification name from current theme list.
1303 * If removing the name also removes all elements, then the entire option will
1308 * @param string $name Theme modification name.
1311 function remove_theme_mod( $name ) {
1312 $theme = get_current_theme();
1314 $mods = get_option("mods_$theme");
1316 if ( !isset($mods[$name]) )
1319 unset($mods[$name]);
1322 return remove_theme_mods();
1324 update_option("mods_$theme", $mods);
1325 wp_cache_delete("mods_$theme", 'options');
1329 * Remove theme modifications option for current theme.
1333 function remove_theme_mods() {
1334 $theme = get_current_theme();
1336 delete_option("mods_$theme");
1340 * Retrieve text color for custom header.
1343 * @uses HEADER_TEXTCOLOR
1347 function get_header_textcolor() {
1348 $default = defined('HEADER_TEXTCOLOR') ? HEADER_TEXTCOLOR : '';
1350 return get_theme_mod('header_textcolor', $default);
1354 * Display text color for custom header.
1358 function header_textcolor() {
1359 echo get_header_textcolor();
1363 * Retrieve header image for custom header.
1366 * @uses HEADER_IMAGE
1370 function get_header_image() {
1371 $default = defined('HEADER_IMAGE') ? HEADER_IMAGE : '';
1373 return get_theme_mod('header_image', $default);
1377 * Display header image path.
1381 function header_image() {
1382 echo get_header_image();
1386 * Add callbacks for image header display.
1388 * The parameter $header_callback callback will be required to display the
1389 * content for the 'wp_head' action. The parameter $admin_header_callback
1390 * callback will be added to Custom_Image_Header class and that will be added
1391 * to the 'admin_menu' action.
1394 * @uses Custom_Image_Header Sets up for $admin_header_callback for administration panel display.
1396 * @param callback $header_callback Call on 'wp_head' action.
1397 * @param callback $admin_header_callback Call on custom header administration screen.
1398 * @param callback $admin_image_div_callback Output a custom header image div on the custom header administration screen. Optional.
1400 function add_custom_image_header($header_callback, $admin_header_callback, $admin_image_div_callback = '') {
1401 if ( ! empty($header_callback) )
1402 add_action('wp_head', $header_callback);
1404 add_theme_support( 'custom-header' );
1408 require_once(ABSPATH . 'wp-admin/custom-header.php');
1409 $GLOBALS['custom_image_header'] =& new Custom_Image_Header($admin_header_callback, $admin_image_div_callback);
1410 add_action('admin_menu', array(&$GLOBALS['custom_image_header'], 'init'));
1414 * Register a selection of default headers to be displayed by the custom header admin UI.
1418 * @param array $headers Array of headers keyed by a string id. The ids point to arrays containing 'url', 'thumbnail_url', and 'description' keys.
1420 function register_default_headers( $headers ) {
1421 global $_wp_default_headers;
1423 $_wp_default_headers = array_merge( (array) $_wp_default_headers, (array) $headers );
1427 * Unregister default headers.
1429 * This function must be called after register_default_headers() has already added the
1430 * header you want to remove.
1432 * @see register_default_headers()
1435 * @param string|array The header string id (key of array) to remove, or an array thereof.
1436 * @return True on success, false on failure.
1438 function unregister_default_headers( $header ) {
1439 global $_wp_default_headers;
1440 if ( is_array( $header ) ) {
1441 array_map( 'unregister_default_headers', $header );
1442 } elseif ( isset( $_wp_default_headers[ $header ] ) ) {
1443 unset( $_wp_default_headers[ $header ] );
1451 * Retrieve background image for custom background.
1457 function get_background_image() {
1458 $default = defined('BACKGROUND_IMAGE') ? BACKGROUND_IMAGE : '';
1460 return get_theme_mod('background_image', $default);
1464 * Display background image path.
1468 function background_image() {
1469 echo get_background_image();
1473 * Retrieve value for custom background color.
1476 * @uses BACKGROUND_COLOR
1480 function get_background_color() {
1481 $default = defined('BACKGROUND_COLOR') ? BACKGROUND_COLOR : '';
1483 return get_theme_mod('background_color', $default);
1487 * Display background color value.
1491 function background_color() {
1492 echo get_background_color();
1496 * Add callbacks for background image display.
1498 * The parameter $header_callback callback will be required to display the
1499 * content for the 'wp_head' action. The parameter $admin_header_callback
1500 * callback will be added to Custom_Background class and that will be added
1501 * to the 'admin_menu' action.
1504 * @uses Custom_Background Sets up for $admin_header_callback for administration panel display.
1506 * @param callback $header_callback Call on 'wp_head' action.
1507 * @param callback $admin_header_callback Call on custom background administration screen.
1508 * @param callback $admin_image_div_callback Output a custom background image div on the custom background administration screen. Optional.
1510 function add_custom_background($header_callback = '', $admin_header_callback = '', $admin_image_div_callback = '') {
1511 if ( isset($GLOBALS['custom_background']) )
1514 if ( empty($header_callback) )
1515 $header_callback = '_custom_background_cb';
1517 add_action('wp_head', $header_callback);
1519 add_theme_support( 'custom-background' );
1523 require_once(ABSPATH . 'wp-admin/custom-background.php');
1524 $GLOBALS['custom_background'] =& new Custom_Background($admin_header_callback, $admin_image_div_callback);
1525 add_action('admin_menu', array(&$GLOBALS['custom_background'], 'init'));
1529 * Default custom background callback.
1532 * @see add_custom_background()
1535 function _custom_background_cb() {
1536 $background = get_background_image();
1537 $color = get_background_color();
1538 if ( ! $background && ! $color )
1541 $style = $color ? "background-color: #$color;" : '';
1543 if ( $background ) {
1544 $image = " background-image: url('$background');";
1546 $repeat = get_theme_mod( 'background_repeat', 'repeat' );
1547 if ( ! in_array( $repeat, array( 'no-repeat', 'repeat-x', 'repeat-y', 'repeat' ) ) )
1549 $repeat = " background-repeat: $repeat;";
1551 $position = get_theme_mod( 'background_position_x', 'left' );
1552 if ( ! in_array( $position, array( 'center', 'right', 'left' ) ) )
1554 $position = " background-position: top $position;";
1556 $attachment = get_theme_mod( 'background_attachment', 'scroll' );
1557 if ( ! in_array( $attachment, array( 'fixed', 'scroll' ) ) )
1558 $attachment = 'scroll';
1559 $attachment = " background-attachment: $attachment;";
1561 $style .= $image . $repeat . $position . $attachment;
1564 <style type="text/css">
1565 body { <?php echo trim( $style ); ?> }
1571 * Add callback for custom TinyMCE editor stylesheets.
1573 * The parameter $stylesheet is the name of the stylesheet, relative to
1574 * the theme root. It also accepts an array of stylesheets.
1575 * It is optional and defaults to 'editor-style.css'.
1579 * @param mixed $stylesheet Optional. Stylesheet name or array thereof, relative to theme root.
1580 * Defaults to 'editor-style.css'
1582 function add_editor_style( $stylesheet = 'editor-style.css' ) {
1584 add_theme_support( 'editor-style' );
1589 global $editor_styles;
1590 $editor_styles = (array) $editor_styles;
1591 $stylesheet = (array) $stylesheet;
1593 $rtl_stylesheet = str_replace('.css', '-rtl.css', $stylesheet[0]);
1594 $stylesheet[] = $rtl_stylesheet;
1597 $editor_styles = array_merge( $editor_styles, $stylesheet );
1601 * Allows a theme to register its support of a certain feature
1603 * Must be called in the theme's functions.php file to work.
1604 * If attached to a hook, it must be after_setup_theme.
1605 * The init hook may be too late for some features.
1608 * @param string $feature the feature being added
1610 function add_theme_support( $feature ) {
1611 global $_wp_theme_features;
1613 if ( func_num_args() == 1 )
1614 $_wp_theme_features[$feature] = true;
1616 $_wp_theme_features[$feature] = array_slice( func_get_args(), 1 );
1620 * Allows a theme to de-register its support of a certain feature
1622 * Should be called in the theme's functions.php file. Generally would
1623 * be used for child themes to override support from the parent theme.
1626 * @see add_theme_support()
1627 * @param string $feature the feature being added
1628 * @return bool Whether feature was removed.
1630 function remove_theme_support( $feature ) {
1631 // Blacklist: for internal registrations not used directly by themes.
1632 if ( in_array( $feature, array( 'custom-background', 'custom-header', 'editor-style', 'widgets' ) ) )
1635 global $_wp_theme_features;
1637 if ( ! isset( $_wp_theme_features[$feature] ) )
1639 unset( $_wp_theme_features[$feature] );
1644 * Checks a theme's support for a given feature
1647 * @param string $feature the feature being checked
1650 function current_theme_supports( $feature ) {
1651 global $_wp_theme_features;
1653 if ( !isset( $_wp_theme_features[$feature] ) )
1656 // If no args passed then no extra checks need be performed
1657 if ( func_num_args() <= 1 )
1660 $args = array_slice( func_get_args(), 1 );
1662 // @todo Allow pluggable arg checking
1663 switch ( $feature ) {
1664 case 'post-thumbnails':
1665 // post-thumbnails can be registered for only certain content/post types by passing
1666 // an array of types to add_theme_support(). If no array was passed, then
1667 // any type is accepted
1668 if ( true === $_wp_theme_features[$feature] ) // Registered for all types
1670 $content_type = $args[0];
1671 if ( in_array($content_type, $_wp_theme_features[$feature][0]) )
1682 * Checks a theme's support for a given feature before loading the functions which implement it.
1685 * @param string $feature the feature being checked
1686 * @param string $include the file containing the functions that implement the feature
1688 function require_if_theme_supports( $feature, $include) {
1689 if ( current_theme_supports( $feature ) )
1690 require ( $include );
1694 * Checks an attachment being deleted to see if it's a header or background image.
1696 * If true it removes the theme modification which would be pointing at the deleted
1701 * @param int $id the attachment id
1703 function _delete_attachment_theme_mod( $id ) {
1704 $attachment_image = wp_get_attachment_url( $id );
1705 $header_image = get_header_image();
1706 $background_image = get_background_image();
1708 if ( $header_image && $header_image == $attachment_image )
1709 remove_theme_mod( 'header_image' );
1711 if ( $background_image && $background_image == $attachment_image )
1712 remove_theme_mod( 'background_image' );
1715 add_action( 'delete_attachment', '_delete_attachment_theme_mod' );