3 * Theme, template, and stylesheet functions.
10 * Retrieve name of the current stylesheet.
12 * The theme name that the administrator has currently set the front end theme
15 * For all extensive purposes, the template name and the stylesheet name are
16 * going to be the same for most cases.
19 * @uses apply_filters() Calls 'stylesheet' filter on stylesheet name.
21 * @return string Stylesheet name.
23 function get_stylesheet() {
24 return apply_filters('stylesheet', get_option('stylesheet'));
28 * Retrieve stylesheet directory path for current theme.
31 * @uses apply_filters() Calls 'stylesheet_directory' filter on stylesheet directory and theme name.
33 * @return string Path to current theme directory.
35 function get_stylesheet_directory() {
36 $stylesheet = get_stylesheet();
37 $theme_root = get_theme_root( $stylesheet );
38 $stylesheet_dir = "$theme_root/$stylesheet";
40 return apply_filters( 'stylesheet_directory', $stylesheet_dir, $stylesheet, $theme_root );
44 * Retrieve stylesheet directory URI.
50 function get_stylesheet_directory_uri() {
51 $stylesheet = get_stylesheet();
52 $theme_root_uri = get_theme_root_uri( $stylesheet );
53 $stylesheet_dir_uri = "$theme_root_uri/$stylesheet";
55 return apply_filters( 'stylesheet_directory_uri', $stylesheet_dir_uri, $stylesheet, $theme_root_uri );
59 * Retrieve URI of current theme stylesheet.
61 * The stylesheet file name is 'style.css' which is appended to {@link
62 * get_stylesheet_directory_uri() stylesheet directory URI} path.
65 * @uses apply_filters() Calls 'stylesheet_uri' filter on stylesheet URI path and stylesheet directory URI.
69 function get_stylesheet_uri() {
70 $stylesheet_dir_uri = get_stylesheet_directory_uri();
71 $stylesheet_uri = $stylesheet_dir_uri . "/style.css";
72 return apply_filters('stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri);
76 * Retrieve localized stylesheet URI.
78 * The stylesheet directory for the localized stylesheet files are located, by
79 * default, in the base theme directory. The name of the locale file will be the
80 * locale followed by '.css'. If that does not exist, then the text direction
81 * stylesheet will be checked for existence, for example 'ltr.css'.
83 * The theme may change the location of the stylesheet directory by either using
84 * the 'stylesheet_directory_uri' filter or the 'locale_stylesheet_uri' filter.
85 * If you want to change the location of the stylesheet files for the entire
86 * WordPress workflow, then change the former. If you just have the locale in a
87 * separate folder, then change the latter.
90 * @uses apply_filters() Calls 'locale_stylesheet_uri' filter on stylesheet URI path and stylesheet directory URI.
94 function get_locale_stylesheet_uri() {
96 $stylesheet_dir_uri = get_stylesheet_directory_uri();
97 $dir = get_stylesheet_directory();
98 $locale = get_locale();
99 if ( file_exists("$dir/$locale.css") )
100 $stylesheet_uri = "$stylesheet_dir_uri/$locale.css";
101 elseif ( !empty($wp_locale->text_direction) && file_exists("$dir/{$wp_locale->text_direction}.css") )
102 $stylesheet_uri = "$stylesheet_dir_uri/{$wp_locale->text_direction}.css";
104 $stylesheet_uri = '';
105 return apply_filters('locale_stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri);
109 * Retrieve name of the current theme.
112 * @uses apply_filters() Calls 'template' filter on template option.
114 * @return string Template name.
116 function get_template() {
117 return apply_filters('template', get_option('template'));
121 * Retrieve current theme directory.
124 * @uses apply_filters() Calls 'template_directory' filter on template directory path and template name.
126 * @return string Template directory path.
128 function get_template_directory() {
129 $template = get_template();
130 $theme_root = get_theme_root( $template );
131 $template_dir = "$theme_root/$template";
133 return apply_filters( 'template_directory', $template_dir, $template, $theme_root );
137 * Retrieve theme directory URI.
140 * @uses apply_filters() Calls 'template_directory_uri' filter on template directory URI path and template name.
142 * @return string Template directory URI.
144 function get_template_directory_uri() {
145 $template = get_template();
146 $theme_root_uri = get_theme_root_uri( $template );
147 $template_dir_uri = "$theme_root_uri/$template";
149 return apply_filters( 'template_directory_uri', $template_dir_uri, $template, $theme_root_uri );
153 * Retrieve theme data from parsed theme file.
155 * The description will have the tags filtered with the following HTML elements
156 * whitelisted. The <b>'a'</b> element with the <em>href</em> and <em>title</em>
157 * attributes. The <b>abbr</b> element with the <em>title</em> attribute. The
158 * <b>acronym<b> element with the <em>title</em> attribute allowed. The
159 * <b>code</b>, <b>em</b>, and <b>strong</b> elements also allowed.
161 * The style.css file must contain theme name, theme URI, and description. The
162 * data can also contain author URI, author, template (parent template),
163 * version, status, and finally tags. Some of these are not used by WordPress
164 * administration panels, but are used by theme directory web sites which list
169 * @param string $theme_file Theme file path.
170 * @return array Theme data.
172 function get_theme_data( $theme_file ) {
173 $default_headers = array(
174 'Name' => 'Theme Name',
175 'URI' => 'Theme URI',
176 'Description' => 'Description',
177 'Author' => 'Author',
178 'AuthorURI' => 'Author URI',
179 'Version' => 'Version',
180 'Template' => 'Template',
181 'Status' => 'Status',
185 $themes_allowed_tags = array(
187 'href' => array(),'title' => array()
200 $theme_data = get_file_data( $theme_file, $default_headers, 'theme' );
202 $theme_data['Name'] = $theme_data['Title'] = wp_kses( $theme_data['Name'], $themes_allowed_tags );
204 $theme_data['URI'] = esc_url( $theme_data['URI'] );
206 $theme_data['Description'] = wptexturize( wp_kses( $theme_data['Description'], $themes_allowed_tags ) );
208 $theme_data['AuthorURI'] = esc_url( $theme_data['AuthorURI'] );
210 $theme_data['Template'] = wp_kses( $theme_data['Template'], $themes_allowed_tags );
212 $theme_data['Version'] = wp_kses( $theme_data['Version'], $themes_allowed_tags );
214 if ( $theme_data['Status'] == '' )
215 $theme_data['Status'] = 'publish';
217 $theme_data['Status'] = wp_kses( $theme_data['Status'], $themes_allowed_tags );
219 if ( $theme_data['Tags'] == '' )
220 $theme_data['Tags'] = array();
222 $theme_data['Tags'] = array_map( 'trim', explode( ',', wp_kses( $theme_data['Tags'], array() ) ) );
224 if ( $theme_data['Author'] == '' ) {
225 $theme_data['Author'] = __('Anonymous');
227 if ( empty( $theme_data['AuthorURI'] ) ) {
228 $theme_data['Author'] = wp_kses( $theme_data['Author'], $themes_allowed_tags );
230 $theme_data['Author'] = sprintf( '<a href="%1$s" title="%2$s">%3$s</a>', $theme_data['AuthorURI'], __( 'Visit author homepage' ), wp_kses( $theme_data['Author'], $themes_allowed_tags ) );
238 * Retrieve list of themes with theme data in theme directory.
240 * The theme is broken, if it doesn't have a parent theme and is missing either
241 * style.css and, or index.php. If the theme has a parent theme then it is
242 * broken, if it is missing style.css; index.php is optional. The broken theme
243 * list is saved in the {@link $wp_broken_themes} global, which is displayed on
244 * the theme list in the administration panels.
247 * @global array $wp_broken_themes Stores the broken themes.
248 * @global array $wp_themes Stores the working themes.
250 * @return array Theme list with theme data.
252 function get_themes() {
253 global $wp_themes, $wp_broken_themes;
255 if ( isset($wp_themes) )
258 /* Register the default root as a theme directory */
259 register_theme_directory( get_theme_root() );
261 if ( !$theme_files = search_theme_directories() )
264 asort( $theme_files );
266 $wp_themes = array();
268 foreach ( (array) $theme_files as $theme_file ) {
269 $theme_root = $theme_file['theme_root'];
270 $theme_file = $theme_file['theme_file'];
272 if ( !is_readable("$theme_root/$theme_file") ) {
273 $wp_broken_themes[$theme_file] = array('Name' => $theme_file, 'Title' => $theme_file, 'Description' => __('File not readable.'));
277 $theme_data = get_theme_data("$theme_root/$theme_file");
279 $name = $theme_data['Name'];
280 $title = $theme_data['Title'];
281 $description = wptexturize($theme_data['Description']);
282 $version = $theme_data['Version'];
283 $author = $theme_data['Author'];
284 $template = $theme_data['Template'];
285 $stylesheet = dirname($theme_file);
288 foreach ( array('png', 'gif', 'jpg', 'jpeg') as $ext ) {
289 if (file_exists("$theme_root/$stylesheet/screenshot.$ext")) {
290 $screenshot = "screenshot.$ext";
295 if ( empty($name) ) {
296 $name = dirname($theme_file);
300 if ( empty($template) ) {
301 if ( file_exists("$theme_root/$stylesheet/index.php") )
302 $template = $stylesheet;
307 $template = trim( $template );
309 if ( !file_exists("$theme_root/$template/index.php") ) {
310 $parent_dir = dirname(dirname($theme_file));
311 if ( file_exists("$theme_root/$parent_dir/$template/index.php") ) {
312 $template = "$parent_dir/$template";
313 $template_directory = "$theme_root/$template";
316 * The parent theme doesn't exist in the current theme's folder or sub folder
317 * so lets use the theme root for the parent template.
319 if ( isset($theme_files[$template]) && file_exists( $theme_files[$template]['theme_root'] . "/$template/index.php" ) ) {
320 $template_directory = $theme_files[$template]['theme_root'] . "/$template";
322 $wp_broken_themes[$name] = array('Name' => $name, 'Title' => $title, 'Description' => __('Template is missing.'));
328 $template_directory = trim( $theme_root . '/' . $template );
331 $stylesheet_files = array();
332 $template_files = array();
334 $stylesheet_dir = @ dir("$theme_root/$stylesheet");
335 if ( $stylesheet_dir ) {
336 while ( ($file = $stylesheet_dir->read()) !== false ) {
337 if ( !preg_match('|^\.+$|', $file) ) {
338 if ( preg_match('|\.css$|', $file) )
339 $stylesheet_files[] = "$theme_root/$stylesheet/$file";
340 elseif ( preg_match('|\.php$|', $file) )
341 $template_files[] = "$theme_root/$stylesheet/$file";
344 @ $stylesheet_dir->close();
347 $template_dir = @ dir("$template_directory");
348 if ( $template_dir ) {
349 while ( ($file = $template_dir->read()) !== false ) {
350 if ( preg_match('|^\.+$|', $file) )
352 if ( preg_match('|\.php$|', $file) ) {
353 $template_files[] = "$template_directory/$file";
354 } elseif ( is_dir("$template_directory/$file") ) {
355 $template_subdir = @ dir("$template_directory/$file");
356 if ( !$template_subdir )
358 while ( ($subfile = $template_subdir->read()) !== false ) {
359 if ( preg_match('|^\.+$|', $subfile) )
361 if ( preg_match('|\.php$|', $subfile) )
362 $template_files[] = "$template_directory/$file/$subfile";
364 @ $template_subdir->close();
367 @ $template_dir->close();
370 //Make unique and remove duplicates when stylesheet and template are the same i.e. most themes
371 $template_files = array_unique($template_files);
372 $stylesheet_files = array_unique($stylesheet_files);
374 $template_dir = dirname($template_files[0]);
375 $stylesheet_dir = dirname($stylesheet_files[0]);
377 if ( empty($template_dir) )
379 if ( empty($stylesheet_dir) )
380 $stylesheet_dir = '/';
382 // Check for theme name collision. This occurs if a theme is copied to
383 // a new theme directory and the theme header is not updated. Whichever
384 // theme is first keeps the name. Subsequent themes get a suffix applied.
385 // The Default and Classic themes always trump their pretenders.
386 if ( isset($wp_themes[$name]) ) {
387 if ( ('WordPress Default' == $name || 'WordPress Classic' == $name) &&
388 ('default' == $stylesheet || 'classic' == $stylesheet) ) {
389 // If another theme has claimed to be one of our default themes, move
391 $suffix = $wp_themes[$name]['Stylesheet'];
392 $new_name = "$name/$suffix";
393 $wp_themes[$new_name] = $wp_themes[$name];
394 $wp_themes[$new_name]['Name'] = $new_name;
396 $name = "$name/$stylesheet";
400 $theme_roots[$stylesheet] = str_replace( WP_CONTENT_DIR, '', $theme_root );
401 $wp_themes[$name] = array( 'Name' => $name, 'Title' => $title, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => $template, 'Stylesheet' => $stylesheet, 'Template Files' => $template_files, 'Stylesheet Files' => $stylesheet_files, 'Template Dir' => $template_dir, 'Stylesheet Dir' => $stylesheet_dir, 'Status' => $theme_data['Status'], 'Screenshot' => $screenshot, 'Tags' => $theme_data['Tags'], 'Theme Root' => $theme_root, 'Theme Root URI' => str_replace( WP_CONTENT_DIR, content_url(), $theme_root ) );
406 /* Store theme roots in the DB */
407 if ( get_site_transient( 'theme_roots' ) != $theme_roots )
408 set_site_transient( 'theme_roots', $theme_roots, 7200 ); // cache for two hours
411 /* Resolve theme dependencies. */
412 $theme_names = array_keys( $wp_themes );
413 foreach ( (array) $theme_names as $theme_name ) {
414 $wp_themes[$theme_name]['Parent Theme'] = '';
415 if ( $wp_themes[$theme_name]['Stylesheet'] != $wp_themes[$theme_name]['Template'] ) {
416 foreach ( (array) $theme_names as $parent_theme_name ) {
417 if ( ($wp_themes[$parent_theme_name]['Stylesheet'] == $wp_themes[$parent_theme_name]['Template']) && ($wp_themes[$parent_theme_name]['Template'] == $wp_themes[$theme_name]['Template']) ) {
418 $wp_themes[$theme_name]['Parent Theme'] = $wp_themes[$parent_theme_name]['Name'];
429 * Retrieve theme roots.
433 * @return array Theme roots
435 function get_theme_roots() {
436 $theme_roots = get_site_transient( 'theme_roots' );
437 if ( false === $theme_roots ) {
439 $theme_roots = get_site_transient( 'theme_roots' ); // this is set in get_theme()
445 * Retrieve theme data.
449 * @param string $theme Theme name.
450 * @return array|null Null, if theme name does not exist. Theme data, if exists.
452 function get_theme($theme) {
453 $themes = get_themes();
455 if ( array_key_exists($theme, $themes) )
456 return $themes[$theme];
462 * Retrieve current theme display name.
464 * If the 'current_theme' option has already been set, then it will be returned
465 * instead. If it is not set, then each theme will be iterated over until both
466 * the current stylesheet and current template name.
472 function get_current_theme() {
473 if ( $theme = get_option('current_theme') )
476 $themes = get_themes();
477 $theme_names = array_keys($themes);
478 $current_template = get_option('template');
479 $current_stylesheet = get_option('stylesheet');
480 $current_theme = 'WordPress Default';
483 foreach ( (array) $theme_names as $theme_name ) {
484 if ( $themes[$theme_name]['Stylesheet'] == $current_stylesheet &&
485 $themes[$theme_name]['Template'] == $current_template ) {
486 $current_theme = $themes[$theme_name]['Name'];
492 update_option('current_theme', $current_theme);
494 return $current_theme;
498 * Register a directory that contains themes.
502 * @param string $directory Either the full filesystem path to a theme folder or a folder within WP_CONTENT_DIR
505 function register_theme_directory( $directory) {
506 global $wp_theme_directories;
508 /* If this folder does not exist, return and do not register */
509 if ( !file_exists( $directory ) )
510 /* Try prepending as the theme directory could be relative to the content directory */
511 $registered_directory = WP_CONTENT_DIR . '/' . $directory;
513 $registered_directory = $directory;
515 /* If this folder does not exist, return and do not register */
516 if ( !file_exists( $registered_directory ) )
519 $wp_theme_directories[] = $registered_directory;
525 * Search all registered theme directories for complete and valid themes.
529 * @return array Valid themes found
531 function search_theme_directories() {
532 global $wp_theme_directories, $wp_broken_themes;
533 if ( empty( $wp_theme_directories ) )
536 $theme_files = array();
537 $wp_broken_themes = array();
539 /* Loop the registered theme directories and extract all themes */
540 foreach ( (array) $wp_theme_directories as $theme_root ) {
541 $theme_loc = $theme_root;
543 /* We don't want to replace all forward slashes, see Trac #4541 */
544 if ( '/' != WP_CONTENT_DIR )
545 $theme_loc = str_replace(WP_CONTENT_DIR, '', $theme_root);
547 /* Files in the root of the current theme directory and one subdir down */
548 $themes_dir = @ opendir($theme_root);
553 while ( ($theme_dir = readdir($themes_dir)) !== false ) {
554 if ( is_dir($theme_root . '/' . $theme_dir) && is_readable($theme_root . '/' . $theme_dir) ) {
555 if ( $theme_dir{0} == '.' || $theme_dir == 'CVS' )
558 $stylish_dir = @opendir($theme_root . '/' . $theme_dir);
559 $found_stylesheet = false;
561 while ( ($theme_file = readdir($stylish_dir)) !== false ) {
562 if ( $theme_file == 'style.css' ) {
563 $theme_files[$theme_dir] = array( 'theme_file' => $theme_dir . '/' . $theme_file, 'theme_root' => $theme_root );
564 $found_stylesheet = true;
568 @closedir($stylish_dir);
570 if ( !$found_stylesheet ) { // look for themes in that dir
571 $subdir = "$theme_root/$theme_dir";
572 $subdir_name = $theme_dir;
573 $theme_subdirs = @opendir( $subdir );
575 $found_subdir_themes = false;
576 while ( ($theme_subdir = readdir($theme_subdirs)) !== false ) {
577 if ( is_dir( $subdir . '/' . $theme_subdir) && is_readable($subdir . '/' . $theme_subdir) ) {
578 if ( $theme_subdir{0} == '.' || $theme_subdir == 'CVS' )
581 $stylish_dir = @opendir($subdir . '/' . $theme_subdir);
582 $found_stylesheet = false;
584 while ( ($theme_file = readdir($stylish_dir)) !== false ) {
585 if ( $theme_file == 'style.css' ) {
586 $theme_files["$theme_dir/$theme_subdir"] = array( 'theme_file' => $subdir_name . '/' . $theme_subdir . '/' . $theme_file, 'theme_root' => $theme_root );
587 $found_stylesheet = true;
588 $found_subdir_themes = true;
592 @closedir($stylish_dir);
595 @closedir($theme_subdir);
596 if ( !$found_subdir_themes )
597 $wp_broken_themes[$theme_dir] = array('Name' => $theme_dir, 'Title' => $theme_dir, 'Description' => __('Stylesheet is missing.'));
601 if ( is_dir( $theme_dir ) )
602 @closedir( $theme_dir );
608 * Retrieve path to themes directory.
610 * Does not have trailing slash.
613 * @param $stylesheet_or_template The stylesheet or template name of the theme
614 * @uses apply_filters() Calls 'theme_root' filter on path.
616 * @return string Theme path.
618 function get_theme_root( $stylesheet_or_template = false ) {
619 if ($stylesheet_or_template) {
620 $theme_roots = get_theme_roots();
622 if ( $theme_roots[$stylesheet_or_template] )
623 $theme_root = WP_CONTENT_DIR . $theme_roots[$stylesheet_or_template];
625 $theme_root = WP_CONTENT_DIR . '/themes';
627 $theme_root = WP_CONTENT_DIR . '/themes';
630 return apply_filters( 'theme_root', $theme_root );
634 * Retrieve URI for themes directory.
636 * Does not have trailing slash.
639 * @param $stylesheet_or_template The stylesheet or template name of the theme
641 * @return string Themes URI.
643 function get_theme_root_uri( $stylesheet_or_template = false ) {
644 $theme_roots = get_theme_roots();
646 if ( $theme_roots[$stylesheet_or_template] )
647 $theme_root_uri = content_url( $theme_roots[$stylesheet_or_template] );
649 $theme_root_uri = content_url( 'themes' );
651 return apply_filters( 'theme_root_uri', $theme_root_uri, get_option('siteurl'), $stylesheet_or_template );
655 * Retrieve path to file without the use of extension.
657 * Used to quickly retrieve the path of file without including the file
658 * extension. It will also check the parent template, if the file exists, with
659 * the use of {@link locate_template()}. Allows for more generic file location
660 * without the use of the other get_*_template() functions.
662 * Can be used with include() or require() to retrieve path.
664 * if( '' != get_query_template( '404' ) )
665 * include( get_query_template( '404' ) );
667 * or the same can be accomplished with
669 * if( '' != get_404_template() )
670 * include( get_404_template() );
675 * @param string $type Filename without extension.
676 * @return string Full path to file.
678 function get_query_template($type) {
679 $type = preg_replace( '|[^a-z0-9-]+|', '', $type );
680 return apply_filters("{$type}_template", locate_template(array("{$type}.php")));
684 * Retrieve path of 404 template in current or parent template.
690 function get_404_template() {
691 return get_query_template('404');
695 * Retrieve path of archive template in current or parent template.
701 function get_archive_template() {
702 return get_query_template('archive');
706 * Retrieve path of author template in current or parent template.
712 function get_author_template() {
713 return get_query_template('author');
717 * Retrieve path of category template in current or parent template.
719 * Works by first retrieving the current slug for example 'category-default.php' and then
720 * trying category ID, for example 'category-1.php' and will finally fallback to category.php
721 * template, if those files don't exist.
724 * @uses apply_filters() Calls 'category_template' on file path of category template.
728 function get_category_template() {
729 $cat_ID = absint( get_query_var('cat') );
730 $category = get_category( $cat_ID );
732 $templates = array();
734 if ( !is_wp_error($category) )
735 $templates[] = "category-{$category->slug}.php";
737 $templates[] = "category-$cat_ID.php";
738 $templates[] = "category.php";
740 $template = locate_template($templates);
741 return apply_filters('category_template', $template);
745 * Retrieve path of tag template in current or parent template.
747 * Works by first retrieving the current tag name, for example 'tag-wordpress.php' and then
748 * trying tag ID, for example 'tag-1.php' and will finally fallback to tag.php
749 * template, if those files don't exist.
752 * @uses apply_filters() Calls 'tag_template' on file path of tag template.
756 function get_tag_template() {
757 $tag_id = absint( get_query_var('tag_id') );
758 $tag_name = get_query_var('tag');
760 $templates = array();
763 $templates[] = "tag-$tag_name.php";
765 $templates[] = "tag-$tag_id.php";
766 $templates[] = "tag.php";
768 $template = locate_template($templates);
769 return apply_filters('tag_template', $template);
773 * Retrieve path of taxonomy template in current or parent template.
775 * Retrieves the taxonomy and term, if term is available. The template is
776 * prepended with 'taxonomy-' and followed by both the taxonomy string and
777 * the taxonomy string followed by a dash and then followed by the term.
779 * The taxonomy and term template is checked and used first, if it exists.
780 * Second, just the taxonomy template is checked, and then finally, taxonomy.php
781 * template is used. If none of the files exist, then it will fall back on to
784 * @since unknown (2.6.0 most likely)
785 * @uses apply_filters() Calls 'taxonomy_template' filter on found path.
789 function get_taxonomy_template() {
790 $taxonomy = get_query_var('taxonomy');
791 $term = get_query_var('term');
793 $templates = array();
794 if ( $taxonomy && $term )
795 $templates[] = "taxonomy-$taxonomy-$term.php";
797 $templates[] = "taxonomy-$taxonomy.php";
799 $templates[] = "taxonomy.php";
801 $template = locate_template($templates);
802 return apply_filters('taxonomy_template', $template);
806 * Retrieve path of date template in current or parent template.
812 function get_date_template() {
813 return get_query_template('date');
817 * Retrieve path of home template in current or parent template.
819 * Attempts to locate 'home.php' first before falling back to 'index.php'.
822 * @uses apply_filters() Calls 'home_template' on file path of home template.
826 function get_home_template() {
827 $template = locate_template(array('home.php', 'index.php'));
828 return apply_filters('home_template', $template);
832 * Retrieve path of page template in current or parent template.
834 * Will first look for the specifically assigned page template
835 * The will search for 'page-{slug}.php' followed by 'page-id.php'
836 * and finally 'page.php'
842 function get_page_template() {
845 $id = (int) $wp_query->post->ID;
846 $template = get_post_meta($id, '_wp_page_template', true);
847 $pagename = get_query_var('pagename');
849 if ( 'default' == $template )
852 $templates = array();
853 if ( !empty($template) && !validate_file($template) )
854 $templates[] = $template;
856 $templates[] = "page-$pagename.php";
858 $templates[] = "page-$id.php";
859 $templates[] = "page.php";
861 return apply_filters('page_template', locate_template($templates));
865 * Retrieve path of paged template in current or parent template.
871 function get_paged_template() {
872 return get_query_template('paged');
876 * Retrieve path of search template in current or parent template.
882 function get_search_template() {
883 return get_query_template('search');
887 * Retrieve path of single template in current or parent template.
893 function get_single_template() {
894 return get_query_template('single');
898 * Retrieve path of attachment template in current or parent template.
900 * The attachment path first checks if the first part of the mime type exists.
901 * The second check is for the second part of the mime type. The last check is
902 * for both types separated by an underscore. If neither are found then the file
903 * 'attachment.php' is checked and returned.
905 * Some examples for the 'text/plain' mime type are 'text.php', 'plain.php', and
906 * finally 'text_plain.php'.
912 function get_attachment_template() {
914 $type = explode('/', $posts[0]->post_mime_type);
915 if ( $template = get_query_template($type[0]) )
917 elseif ( $template = get_query_template($type[1]) )
919 elseif ( $template = get_query_template("$type[0]_$type[1]") )
922 return get_query_template('attachment');
926 * Retrieve path of comment popup template in current or parent template.
928 * Checks for comment popup template in current template, if it exists or in the
929 * parent template. If it doesn't exist, then it retrieves the comment-popup.php
930 * file from the default theme. The default theme must then exist for it to
934 * @uses apply_filters() Calls 'comments_popup_template' filter on path.
938 function get_comments_popup_template() {
939 $template = locate_template(array("comments-popup.php"));
941 $template = get_theme_root() . '/default/comments-popup.php';
943 return apply_filters('comments_popup_template', $template);
947 * Retrieve the name of the highest priority template file that exists.
949 * Searches in the STYLESHEETPATH before TEMPLATEPATH so that themes which
950 * inherit from a parent theme can just overload one file.
954 * @param array $template_names Array of template files to search for in priority order.
955 * @param bool $load If true the template file will be loaded if it is found.
956 * @return string The template filename if one is located.
958 function locate_template($template_names, $load = false) {
959 if (!is_array($template_names))
963 foreach($template_names as $template_name) {
964 if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
965 $located = STYLESHEETPATH . '/' . $template_name;
967 } else if ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
968 $located = TEMPLATEPATH . '/' . $template_name;
973 if ($load && '' != $located)
974 load_template($located);
980 * Require once the template file with WordPress environment.
982 * The globals are set up for the template file to ensure that the WordPress
983 * environment is available from within the function. The query variables are
988 * @param string $_template_file Path to template file.
990 function load_template($_template_file) {
991 global $posts, $post, $wp_did_header, $wp_did_template_redirect, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
993 if ( is_array($wp_query->query_vars) )
994 extract($wp_query->query_vars, EXTR_SKIP);
996 require_once($_template_file);
1000 * Display localized stylesheet link element.
1004 function locale_stylesheet() {
1005 $stylesheet = get_locale_stylesheet_uri();
1006 if ( empty($stylesheet) )
1008 echo '<link rel="stylesheet" href="' . $stylesheet . '" type="text/css" media="screen" />';
1012 * Start preview theme output buffer.
1014 * Will only preform task if the user has permissions and template and preview
1015 * query variables exist.
1019 function preview_theme() {
1020 if ( ! (isset($_GET['template']) && isset($_GET['preview'])) )
1023 if ( !current_user_can( 'switch_themes' ) )
1026 $_GET['template'] = preg_replace('|[^a-z0-9_./-]|i', '', $_GET['template']);
1028 if ( validate_file($_GET['template']) )
1031 add_filter( 'template', '_preview_theme_template_filter' );
1033 if ( isset($_GET['stylesheet']) ) {
1034 $_GET['stylesheet'] = preg_replace('|[^a-z0-9_./-]|i', '', $_GET['stylesheet']);
1035 if ( validate_file($_GET['stylesheet']) )
1037 add_filter( 'stylesheet', '_preview_theme_stylesheet_filter' );
1040 // Prevent theme mods to current theme being used on theme being previewed
1041 add_filter( 'pre_option_mods_' . get_current_theme(), create_function( '', "return array();" ) );
1043 ob_start( 'preview_theme_ob_filter' );
1045 add_action('setup_theme', 'preview_theme');
1048 * Private function to modify the current template when previewing a theme
1055 function _preview_theme_template_filter() {
1056 return isset($_GET['template']) ? $_GET['template'] : '';
1060 * Private function to modify the current stylesheet when previewing a theme
1067 function _preview_theme_stylesheet_filter() {
1068 return isset($_GET['stylesheet']) ? $_GET['stylesheet'] : '';
1072 * Callback function for ob_start() to capture all links in the theme.
1077 * @param string $content
1080 function preview_theme_ob_filter( $content ) {
1081 return preg_replace_callback( "|(<a.*?href=([\"']))(.*?)([\"'].*?>)|", 'preview_theme_ob_filter_callback', $content );
1085 * Manipulates preview theme links in order to control and maintain location.
1087 * Callback function for preg_replace_callback() to accept and filter matches.
1092 * @param array $matches
1095 function preview_theme_ob_filter_callback( $matches ) {
1096 if ( strpos($matches[4], 'onclick') !== false )
1097 $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.
1099 ( false !== strpos($matches[3], '/wp-admin/') )
1101 ( false !== strpos($matches[3], '://') && 0 !== strpos($matches[3], get_option('home')) )
1103 ( false !== strpos($matches[3], '/feed/') )
1105 ( false !== strpos($matches[3], '/trackback/') )
1107 return $matches[1] . "#$matches[2] onclick=$matches[2]return false;" . $matches[4];
1109 $link = add_query_arg( array('preview' => 1, 'template' => $_GET['template'], 'stylesheet' => @$_GET['stylesheet'] ), $matches[3] );
1110 if ( 0 === strpos($link, 'preview=1') )
1112 return $matches[1] . esc_attr( $link ) . $matches[4];
1116 * Switches current theme to new template and stylesheet names.
1119 * @uses do_action() Calls 'switch_theme' action on updated theme display name.
1121 * @param string $template Template name
1122 * @param string $stylesheet Stylesheet name.
1124 function switch_theme($template, $stylesheet) {
1125 update_option('template', $template);
1126 update_option('stylesheet', $stylesheet);
1127 delete_option('current_theme');
1128 $theme = get_current_theme();
1129 do_action('switch_theme', $theme);
1133 * Checks that current theme files 'index.php' and 'style.css' exists.
1135 * Does not check the 'default' theme. The 'default' theme should always exist
1136 * or should have another theme renamed to that template name and directory
1137 * path. Will switch theme to default if current theme does not validate.
1138 * You can use the 'validate_current_theme' filter to return FALSE to
1139 * disable this functionality.
1145 function validate_current_theme() {
1146 // Don't validate during an install/upgrade.
1147 if ( defined('WP_INSTALLING') || !apply_filters( 'validate_current_theme', true ) )
1150 if ( get_template() != 'default' && !file_exists(get_template_directory() . '/index.php') ) {
1151 switch_theme('default', 'default');
1155 if ( get_stylesheet() != 'default' && !file_exists(get_template_directory() . '/style.css') ) {
1156 switch_theme('default', 'default');
1164 * Retrieve theme modification value for the current theme.
1166 * If the modification name does not exist, then the $default will be passed
1167 * through {@link http://php.net/sprintf sprintf()} PHP function with the first
1168 * string the template directory URI and the second string the stylesheet
1172 * @uses apply_filters() Calls 'theme_mod_$name' filter on the value.
1174 * @param string $name Theme modification name.
1175 * @param bool|string $default
1178 function get_theme_mod($name, $default = false) {
1179 $theme = get_current_theme();
1181 $mods = get_option("mods_$theme");
1183 if ( isset($mods[$name]) )
1184 return apply_filters( "theme_mod_$name", $mods[$name] );
1186 return apply_filters( "theme_mod_$name", sprintf($default, get_template_directory_uri(), get_stylesheet_directory_uri()) );
1190 * Update theme modification value for the current theme.
1194 * @param string $name Theme modification name.
1195 * @param string $value theme modification value.
1197 function set_theme_mod($name, $value) {
1198 $theme = get_current_theme();
1200 $mods = get_option("mods_$theme");
1202 $mods[$name] = $value;
1204 update_option("mods_$theme", $mods);
1205 wp_cache_delete("mods_$theme", 'options');
1209 * Remove theme modification name from current theme list.
1211 * If removing the name also removes all elements, then the entire option will
1216 * @param string $name Theme modification name.
1219 function remove_theme_mod( $name ) {
1220 $theme = get_current_theme();
1222 $mods = get_option("mods_$theme");
1224 if ( !isset($mods[$name]) )
1227 unset($mods[$name]);
1230 return remove_theme_mods();
1232 update_option("mods_$theme", $mods);
1233 wp_cache_delete("mods_$theme", 'options');
1237 * Remove theme modifications option for current theme.
1241 function remove_theme_mods() {
1242 $theme = get_current_theme();
1244 delete_option("mods_$theme");
1248 * Retrieve text color for custom header.
1251 * @uses HEADER_TEXTCOLOR
1255 function get_header_textcolor() {
1256 return get_theme_mod('header_textcolor', HEADER_TEXTCOLOR);
1260 * Display text color for custom header.
1264 function header_textcolor() {
1265 echo get_header_textcolor();
1269 * Retrieve header image for custom header.
1272 * @uses HEADER_IMAGE
1276 function get_header_image() {
1277 return get_theme_mod('header_image', HEADER_IMAGE);
1281 * Display header image path.
1285 function header_image() {
1286 echo get_header_image();
1290 * Add callbacks for image header display.
1292 * The parameter $header_callback callback will be required to display the
1293 * content for the 'wp_head' action. The parameter $admin_header_callback
1294 * callback will be added to Custom_Image_Header class and that will be added
1295 * to the 'admin_menu' action.
1298 * @uses Custom_Image_Header Sets up for $admin_header_callback for administration panel display.
1300 * @param callback $header_callback Call on 'wp_head' action.
1301 * @param callback $admin_header_callback Call on administration panels.
1303 function add_custom_image_header($header_callback, $admin_header_callback) {
1304 if ( ! empty($header_callback) )
1305 add_action('wp_head', $header_callback);
1309 require_once(ABSPATH . 'wp-admin/custom-header.php');
1310 $GLOBALS['custom_image_header'] =& new Custom_Image_Header($admin_header_callback);
1311 add_action('admin_menu', array(&$GLOBALS['custom_image_header'], 'init'));
1315 * Allows a theme to register its support of a certain feature
1317 * Must be called in the themes functions.php file to work.
1319 * @author Mark Jaquith
1321 * @param string $feature the feature being added
1323 function add_theme_support( $feature ) {
1324 global $_wp_theme_features;
1326 if ( func_num_args() == 1 )
1327 $_wp_theme_features[$feature] = true;
1329 $_wp_theme_features[$feature] = array_slice( func_get_args(), 1 );
1333 * Checks a theme's support for a given feature
1335 * @author Mark Jaquith
1337 * @param string $feature the feature being checked
1341 function current_theme_supports( $feature ) {
1342 global $_wp_theme_features;
1344 if ( !isset( $_wp_theme_features[$feature] ) )
1347 // If no args passed then no extra checks need be performed
1348 if ( func_num_args() <= 1 )
1351 $args = array_slice( func_get_args(), 1 );
1353 // @todo Allow pluggable arg checking
1354 switch ( $feature ) {
1355 case 'post-thumbnails':
1356 // post-thumbnails can be registered for only certain content/post types by passing
1357 // an array of types to add_theme_support(). If no array was passed, then
1358 // any type is accepted
1359 if ( true === $_wp_theme_features[$feature] ) // Registered for all types
1361 $content_type = $args[0];
1362 if ( in_array($content_type, $_wp_theme_features[$feature][0]) )
1373 * Checks a theme's support for a given feature before loading the functions which implement it.
1375 * @author Peter Westwood
1377 * @param string $feature the feature being checked
1378 * @param string $include the file containing the functions that implement the feature
1380 function require_if_theme_supports( $feature, $include) {
1381 if ( current_theme_supports( $feature ) )
1382 require ( $include );