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 $stylesheet_dir = get_theme_root() . "/$stylesheet";
38 return apply_filters('stylesheet_directory', $stylesheet_dir, $stylesheet);
42 * Retrieve stylesheet directory URI.
48 function get_stylesheet_directory_uri() {
49 $stylesheet = get_stylesheet();
50 $stylesheet_dir_uri = get_theme_root_uri() . "/$stylesheet";
51 return apply_filters('stylesheet_directory_uri', $stylesheet_dir_uri, $stylesheet);
55 * Retrieve URI of current theme stylesheet.
57 * The stylesheet file name is 'style.css' which is appended to {@link
58 * get_stylesheet_directory_uri() stylesheet directory URI} path.
61 * @uses apply_filters() Calls 'stylesheet_uri' filter on stylesheet URI path and stylesheet directory URI.
65 function get_stylesheet_uri() {
66 $stylesheet_dir_uri = get_stylesheet_directory_uri();
67 $stylesheet_uri = $stylesheet_dir_uri . "/style.css";
68 return apply_filters('stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri);
72 * Retrieve localized stylesheet URI.
74 * The stylesheet directory for the localized stylesheet files are located, by
75 * default, in the base theme directory. The name of the locale file will be the
76 * locale followed by '.css'. If that does not exist, then the text direction
77 * stylesheet will be checked for existence, for example 'ltr.css'.
79 * The theme may change the location of the stylesheet directory by either using
80 * the 'stylesheet_directory_uri' filter or the 'locale_stylesheet_uri' filter.
81 * If you want to change the location of the stylesheet files for the entire
82 * WordPress workflow, then change the former. If you just have the locale in a
83 * separate folder, then change the latter.
86 * @uses apply_filters() Calls 'locale_stylesheet_uri' filter on stylesheet URI path and stylesheet directory URI.
90 function get_locale_stylesheet_uri() {
92 $stylesheet_dir_uri = get_stylesheet_directory_uri();
93 $dir = get_stylesheet_directory();
94 $locale = get_locale();
95 if ( file_exists("$dir/$locale.css") )
96 $stylesheet_uri = "$stylesheet_dir_uri/$locale.css";
97 elseif ( !empty($wp_locale->text_direction) && file_exists("$dir/{$wp_locale->text_direction}.css") )
98 $stylesheet_uri = "$stylesheet_dir_uri/{$wp_locale->text_direction}.css";
100 $stylesheet_uri = '';
101 return apply_filters('locale_stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri);
105 * Retrieve name of the current theme.
108 * @uses apply_filters() Calls 'template' filter on template option.
110 * @return string Template name.
112 function get_template() {
113 return apply_filters('template', get_option('template'));
117 * Retrieve current theme directory.
120 * @uses apply_filters() Calls 'template_directory' filter on template directory path and template name.
122 * @return string Template directory path.
124 function get_template_directory() {
125 $template = get_template();
126 $template_dir = get_theme_root() . "/$template";
127 return apply_filters('template_directory', $template_dir, $template);
131 * Retrieve theme directory URI.
134 * @uses apply_filters() Calls 'template_directory_uri' filter on template directory URI path and template name.
136 * @return string Template directory URI.
138 function get_template_directory_uri() {
139 $template = get_template();
140 $template_dir_uri = get_theme_root_uri() . "/$template";
141 return apply_filters('template_directory_uri', $template_dir_uri, $template);
145 * Retrieve theme data from parsed theme file.
147 * The description will have the tags filtered with the following HTML elements
148 * whitelisted. The <b>'a'</b> element with the <em>href</em> and <em>title</em>
149 * attributes. The <b>abbr</b> element with the <em>title</em> attribute. The
150 * <b>acronym<b> element with the <em>title</em> attribute allowed. The
151 * <b>code</b>, <b>em</b>, and <b>strong</b> elements also allowed.
153 * The style.css file must contain theme name, theme URI, and description. The
154 * data can also contain author URI, author, template (parent template),
155 * version, status, and finally tags. Some of these are not used by WordPress
156 * administration panels, but are used by theme directory web sites which list
161 * @param string $theme_file Theme file path.
162 * @return array Theme data.
164 function get_theme_data( $theme_file ) {
165 $themes_allowed_tags = array(
167 'href' => array(),'title' => array()
180 $theme_data = implode( '', file( $theme_file ) );
181 $theme_data = str_replace ( '\r', '\n', $theme_data );
182 preg_match( '|Theme Name:(.*)$|mi', $theme_data, $theme_name );
183 preg_match( '|Theme URI:(.*)$|mi', $theme_data, $theme_uri );
184 preg_match( '|Description:(.*)$|mi', $theme_data, $description );
186 if ( preg_match( '|Author URI:(.*)$|mi', $theme_data, $author_uri ) )
187 $author_uri = clean_url( trim( $author_uri[1]) );
191 if ( preg_match( '|Template:(.*)$|mi', $theme_data, $template ) )
192 $template = wp_kses( trim( $template[1] ), $themes_allowed_tags );
196 if ( preg_match( '|Version:(.*)|i', $theme_data, $version ) )
197 $version = wp_kses( trim( $version[1] ), $themes_allowed_tags );
201 if ( preg_match('|Status:(.*)|i', $theme_data, $status) )
202 $status = wp_kses( trim( $status[1] ), $themes_allowed_tags );
206 if ( preg_match('|Tags:(.*)|i', $theme_data, $tags) )
207 $tags = array_map( 'trim', explode( ',', wp_kses( trim( $tags[1] ), array() ) ) );
211 $name = $theme = wp_kses( trim( $theme_name[1] ), $themes_allowed_tags );
212 $theme_uri = clean_url( trim( $theme_uri[1] ) );
213 $description = wptexturize( wp_kses( trim( $description[1] ), $themes_allowed_tags ) );
215 if ( preg_match( '|Author:(.*)$|mi', $theme_data, $author_name ) ) {
216 if ( empty( $author_uri ) ) {
217 $author = wp_kses( trim( $author_name[1] ), $themes_allowed_tags );
219 $author = sprintf( '<a href="%1$s" title="%2$s">%3$s</a>', $author_uri, __( 'Visit author homepage' ), wp_kses( trim( $author_name[1] ), $themes_allowed_tags ) );
222 $author = __('Anonymous');
225 return array( 'Name' => $name, 'Title' => $theme, 'URI' => $theme_uri, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => $template, 'Status' => $status, 'Tags' => $tags );
229 * Retrieve list of themes with theme data in theme directory.
231 * The theme is broken, if it doesn't have a parent theme and is missing either
232 * style.css and, or index.php. If the theme has a parent theme then it is
233 * broken, if it is missing style.css; index.php is optional. The broken theme
234 * list is saved in the {@link $wp_broken_themes} global, which is displayed on
235 * the theme list in the administration panels.
238 * @global array $wp_broken_themes Stores the broken themes.
239 * @global array $wp_themes Stores the working themes.
241 * @return array Theme list with theme data.
243 function get_themes() {
244 global $wp_themes, $wp_broken_themes;
246 if ( isset($wp_themes) )
250 $wp_broken_themes = array();
251 $theme_loc = $theme_root = get_theme_root();
252 if ( '/' != WP_CONTENT_DIR ) // don't want to replace all forward slashes, see Trac #4541
253 $theme_loc = str_replace(WP_CONTENT_DIR, '', $theme_root);
255 // Files in wp-content/themes directory and one subdir down
256 $themes_dir = @ opendir($theme_root);
260 while ( ($theme_dir = readdir($themes_dir)) !== false ) {
261 if ( is_dir($theme_root . '/' . $theme_dir) && is_readable($theme_root . '/' . $theme_dir) ) {
262 if ( $theme_dir{0} == '.' || $theme_dir == '..' || $theme_dir == 'CVS' )
264 $stylish_dir = @ opendir($theme_root . '/' . $theme_dir);
265 $found_stylesheet = false;
266 while ( ($theme_file = readdir($stylish_dir)) !== false ) {
267 if ( $theme_file == 'style.css' ) {
268 $theme_files[] = $theme_dir . '/' . $theme_file;
269 $found_stylesheet = true;
273 @closedir($stylish_dir);
274 if ( !$found_stylesheet ) { // look for themes in that dir
275 $subdir = "$theme_root/$theme_dir";
276 $subdir_name = $theme_dir;
277 $theme_subdir = @ opendir( $subdir );
278 while ( ($theme_dir = readdir($theme_subdir)) !== false ) {
279 if ( is_dir( $subdir . '/' . $theme_dir) && is_readable($subdir . '/' . $theme_dir) ) {
280 if ( $theme_dir{0} == '.' || $theme_dir == '..' || $theme_dir == 'CVS' )
282 $stylish_dir = @ opendir($subdir . '/' . $theme_dir);
283 $found_stylesheet = false;
284 while ( ($theme_file = readdir($stylish_dir)) !== false ) {
285 if ( $theme_file == 'style.css' ) {
286 $theme_files[] = $subdir_name . '/' . $theme_dir . '/' . $theme_file;
287 $found_stylesheet = true;
291 @closedir($stylish_dir);
294 @closedir($theme_subdir);
295 $wp_broken_themes[$theme_dir] = array('Name' => $theme_dir, 'Title' => $theme_dir, 'Description' => __('Stylesheet is missing.'));
299 if ( is_dir( $theme_dir ) )
300 @closedir( $theme_dir );
302 if ( !$themes_dir || !$theme_files )
307 foreach ( (array) $theme_files as $theme_file ) {
308 if ( !is_readable("$theme_root/$theme_file") ) {
309 $wp_broken_themes[$theme_file] = array('Name' => $theme_file, 'Title' => $theme_file, 'Description' => __('File not readable.'));
313 $theme_data = get_theme_data("$theme_root/$theme_file");
315 $name = $theme_data['Name'];
316 $title = $theme_data['Title'];
317 $description = wptexturize($theme_data['Description']);
318 $version = $theme_data['Version'];
319 $author = $theme_data['Author'];
320 $template = $theme_data['Template'];
321 $stylesheet = dirname($theme_file);
324 foreach ( array('png', 'gif', 'jpg', 'jpeg') as $ext ) {
325 if (file_exists("$theme_root/$stylesheet/screenshot.$ext")) {
326 $screenshot = "screenshot.$ext";
331 if ( empty($name) ) {
332 $name = dirname($theme_file);
336 if ( empty($template) ) {
337 if ( file_exists(dirname("$theme_root/$theme_file/index.php")) )
338 $template = dirname($theme_file);
343 $template = trim($template);
345 if ( !file_exists("$theme_root/$template/index.php") ) {
346 $parent_dir = dirname(dirname($theme_file));
347 if ( file_exists("$theme_root/$parent_dir/$template/index.php") ) {
348 $template = "$parent_dir/$template";
350 $wp_broken_themes[$name] = array('Name' => $name, 'Title' => $title, 'Description' => __('Template is missing.'));
355 $stylesheet_files = array();
356 $template_files = array();
358 $stylesheet_dir = @ dir("$theme_root/$stylesheet");
359 if ( $stylesheet_dir ) {
360 while ( ($file = $stylesheet_dir->read()) !== false ) {
361 if ( !preg_match('|^\.+$|', $file) ) {
362 if ( preg_match('|\.css$|', $file) )
363 $stylesheet_files[] = "$theme_loc/$stylesheet/$file";
364 elseif ( preg_match('|\.php$|', $file) )
365 $template_files[] = "$theme_loc/$stylesheet/$file";
370 $template_dir = @ dir("$theme_root/$template");
371 if ( $template_dir ) {
372 while(($file = $template_dir->read()) !== false) {
373 if ( !preg_match('|^\.+$|', $file) && preg_match('|\.php$|', $file) )
374 $template_files[] = "$theme_loc/$template/$file";
378 $template_dir = dirname($template_files[0]);
379 $stylesheet_dir = dirname($stylesheet_files[0]);
381 if ( empty($template_dir) )
383 if ( empty($stylesheet_dir) )
384 $stylesheet_dir = '/';
386 // Check for theme name collision. This occurs if a theme is copied to
387 // a new theme directory and the theme header is not updated. Whichever
388 // theme is first keeps the name. Subsequent themes get a suffix applied.
389 // The Default and Classic themes always trump their pretenders.
390 if ( isset($themes[$name]) ) {
391 if ( ('WordPress Default' == $name || 'WordPress Classic' == $name) &&
392 ('default' == $stylesheet || 'classic' == $stylesheet) ) {
393 // If another theme has claimed to be one of our default themes, move
395 $suffix = $themes[$name]['Stylesheet'];
396 $new_name = "$name/$suffix";
397 $themes[$new_name] = $themes[$name];
398 $themes[$new_name]['Name'] = $new_name;
400 $name = "$name/$stylesheet";
404 $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']);
407 // Resolve theme dependencies.
408 $theme_names = array_keys($themes);
410 foreach ( (array) $theme_names as $theme_name ) {
411 $themes[$theme_name]['Parent Theme'] = '';
412 if ( $themes[$theme_name]['Stylesheet'] != $themes[$theme_name]['Template'] ) {
413 foreach ( (array) $theme_names as $parent_theme_name ) {
414 if ( ($themes[$parent_theme_name]['Stylesheet'] == $themes[$parent_theme_name]['Template']) && ($themes[$parent_theme_name]['Template'] == $themes[$theme_name]['Template']) ) {
415 $themes[$theme_name]['Parent Theme'] = $themes[$parent_theme_name]['Name'];
422 $wp_themes = $themes;
428 * Retrieve theme data.
432 * @param string $theme Theme name.
433 * @return array|null Null, if theme name does not exist. Theme data, if exists.
435 function get_theme($theme) {
436 $themes = get_themes();
438 if ( array_key_exists($theme, $themes) )
439 return $themes[$theme];
445 * Retrieve current theme display name.
447 * If the 'current_theme' option has already been set, then it will be returned
448 * instead. If it is not set, then each theme will be iterated over until both
449 * the current stylesheet and current template name.
455 function get_current_theme() {
456 if ( $theme = get_option('current_theme') )
459 $themes = get_themes();
460 $theme_names = array_keys($themes);
461 $current_template = get_option('template');
462 $current_stylesheet = get_option('stylesheet');
463 $current_theme = 'WordPress Default';
466 foreach ( (array) $theme_names as $theme_name ) {
467 if ( $themes[$theme_name]['Stylesheet'] == $current_stylesheet &&
468 $themes[$theme_name]['Template'] == $current_template ) {
469 $current_theme = $themes[$theme_name]['Name'];
475 update_option('current_theme', $current_theme);
477 return $current_theme;
481 * Retrieve path to themes directory.
483 * Does not have trailing slash.
486 * @uses apply_filters() Calls 'theme_root' filter on path.
488 * @return string Theme path.
490 function get_theme_root() {
491 return apply_filters('theme_root', WP_CONTENT_DIR . "/themes");
495 * Retrieve URI for themes directory.
497 * Does not have trailing slash.
501 * @return string Themes URI.
503 function get_theme_root_uri() {
504 return apply_filters('theme_root_uri', content_url('themes'), get_option('siteurl'));
508 * Retrieve path to file without the use of extension.
510 * Used to quickly retrieve the path of file without including the file
511 * extension. It will also check the parent template, if the file exists, with
512 * the use of {@link locate_template()}. Allows for more generic file location
513 * without the use of the other get_*_template() functions.
515 * Can be used with include() or require() to retrieve path.
517 * if( '' != get_query_template( '404' ) )
518 * include( get_query_template( '404' ) );
520 * or the same can be accomplished with
522 * if( '' != get_404_template() )
523 * include( get_404_template() );
528 * @param string $type Filename without extension.
529 * @return string Full path to file.
531 function get_query_template($type) {
532 $type = preg_replace( '|[^a-z0-9-]+|', '', $type );
533 return apply_filters("{$type}_template", locate_template(array("{$type}.php")));
537 * Retrieve path of 404 template in current or parent template.
543 function get_404_template() {
544 return get_query_template('404');
548 * Retrieve path of archive template in current or parent template.
554 function get_archive_template() {
555 return get_query_template('archive');
559 * Retrieve path of author template in current or parent template.
565 function get_author_template() {
566 return get_query_template('author');
570 * Retrieve path of category template in current or parent template.
572 * Works by retrieving the current category ID, for example 'category-1.php' and
573 * will fallback to category.php template, if the ID category file doesn't
577 * @uses apply_filters() Calls 'category_template' on file path of category template.
581 function get_category_template() {
582 $template = locate_template(array("category-" . absint( get_query_var('cat') ) . '.php', 'category.php'));
583 return apply_filters('category_template', $template);
587 * Retrieve path of tag template in current or parent template.
589 * Works by retrieving the current tag name, for example 'tag-wordpress.php' and will
590 * fallback to tag.php template, if the name tag file doesn't exist.
593 * @uses apply_filters() Calls 'tag_template' on file path of tag template.
597 function get_tag_template() {
598 $template = locate_template(array("tag-" . get_query_var('tag') . '.php', 'tag.php'));
599 return apply_filters('tag_template', $template);
603 * Retrieve path of taxonomy template in current or parent template.
605 * Retrieves the taxonomy and term, if term is available. The template is
606 * prepended with 'taxonomy-' and followed by both the taxonomy string and
607 * the taxonomy string followed by a dash and then followed by the term.
609 * The taxonomy and term template is checked and used first, if it exists.
610 * Second, just the taxonomy template is checked, and then finally, taxonomy.php
611 * template is used. If none of the files exist, then it will fall back on to
614 * @since unknown (2.6.0 most likely)
615 * @uses apply_filters() Calls 'taxonomy_template' filter on found path.
619 function get_taxonomy_template() {
620 $taxonomy = get_query_var('taxonomy');
621 $term = get_query_var('term');
623 $templates = array();
624 if ( $taxonomy && $term )
625 $templates[] = "taxonomy-$taxonomy-$term.php";
627 $templates[] = "taxonomy-$taxonomy.php";
629 $templates[] = "taxonomy.php";
631 $template = locate_template($templates);
632 return apply_filters('taxonomy_template', $template);
636 * Retrieve path of date template in current or parent template.
642 function get_date_template() {
643 return get_query_template('date');
647 * Retrieve path of home template in current or parent template.
649 * Attempts to locate 'home.php' first before falling back to 'index.php'.
652 * @uses apply_filters() Calls 'home_template' on file path of home template.
656 function get_home_template() {
657 $template = locate_template(array('home.php', 'index.php'));
658 return apply_filters('home_template', $template);
662 * Retrieve path of page template in current or parent template.
664 * First attempt is to look for the file in the '_wp_page_template' page meta
665 * data. The second attempt, if the first has a file and is not empty, is to
666 * look for 'page.php'.
672 function get_page_template() {
675 $id = (int) $wp_query->post->ID;
676 $template = get_post_meta($id, '_wp_page_template', true);
678 if ( 'default' == $template )
681 $templates = array();
682 if ( !empty($template) && !validate_file($template) )
683 $templates[] = $template;
685 $templates[] = "page.php";
687 return apply_filters('page_template', locate_template($templates));
691 * Retrieve path of paged template in current or parent template.
697 function get_paged_template() {
698 return get_query_template('paged');
702 * Retrieve path of search template in current or parent template.
708 function get_search_template() {
709 return get_query_template('search');
713 * Retrieve path of single template in current or parent template.
719 function get_single_template() {
720 return get_query_template('single');
724 * Retrieve path of attachment template in current or parent template.
726 * The attachment path first checks if the first part of the mime type exists.
727 * The second check is for the second part of the mime type. The last check is
728 * for both types separated by an underscore. If neither are found then the file
729 * 'attachment.php' is checked and returned.
731 * Some examples for the 'text/plain' mime type are 'text.php', 'plain.php', and
732 * finally 'text_plain.php'.
738 function get_attachment_template() {
740 $type = explode('/', $posts[0]->post_mime_type);
741 if ( $template = get_query_template($type[0]) )
743 elseif ( $template = get_query_template($type[1]) )
745 elseif ( $template = get_query_template("$type[0]_$type[1]") )
748 return get_query_template('attachment');
752 * Retrieve path of comment popup template in current or parent template.
754 * Checks for comment popup template in current template, if it exists or in the
755 * parent template. If it doesn't exist, then it retrieves the comment-popup.php
756 * file from the default theme. The default theme must then exist for it to
760 * @uses apply_filters() Calls 'comments_popup_template' filter on path.
764 function get_comments_popup_template() {
765 $template = locate_template(array("comments-popup.php"));
767 $template = get_theme_root() . '/default/comments-popup.php';
769 return apply_filters('comments_popup_template', $template);
773 * Retrieve the name of the highest priority template file that exists.
775 * Searches in the STYLESHEETPATH before TEMPLATEPATH so that themes which
776 * inherit from a parent theme can just overload one file.
780 * @param array $template_names Array of template files to search for in priority order.
781 * @param bool $load If true the template file will be loaded if it is found.
782 * @return string The template filename if one is located.
784 function locate_template($template_names, $load = false) {
785 if (!is_array($template_names))
789 foreach($template_names as $template_name) {
790 if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
791 $located = STYLESHEETPATH . '/' . $template_name;
793 } else if ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
794 $located = TEMPLATEPATH . '/' . $template_name;
799 if ($load && '' != $located)
800 load_template($located);
806 * Require once the template file with WordPress environment.
808 * The globals are set up for the template file to ensure that the WordPress
809 * environment is available from within the function. The query variables are
814 * @param string $_template_file Path to template file.
816 function load_template($_template_file) {
817 global $posts, $post, $wp_did_header, $wp_did_template_redirect, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
819 if ( is_array($wp_query->query_vars) )
820 extract($wp_query->query_vars, EXTR_SKIP);
822 require_once($_template_file);
826 * Display localized stylesheet link element.
830 function locale_stylesheet() {
831 $stylesheet = get_locale_stylesheet_uri();
832 if ( empty($stylesheet) )
834 echo '<link rel="stylesheet" href="' . $stylesheet . '" type="text/css" media="screen" />';
838 * Start preview theme output buffer.
840 * Will only preform task if the user has permissions and template and preview
841 * query variables exist.
845 function preview_theme() {
846 if ( ! (isset($_GET['template']) && isset($_GET['preview'])) )
849 if ( !current_user_can( 'switch_themes' ) )
852 $_GET['template'] = preg_replace('|[^a-z0-9_.-/]|i', '', $_GET['template']);
854 if ( validate_file($_GET['template']) )
857 add_filter('template', create_function('', "return '{$_GET['template']}';") );
859 if ( isset($_GET['stylesheet']) ) {
860 $_GET['stylesheet'] = preg_replace('|[^a-z0-9_.-/]|i', '', $_GET['stylesheet']);
861 if ( validate_file($_GET['stylesheet']) )
863 add_filter('stylesheet', create_function('', "return '{$_GET['stylesheet']}';") );
866 ob_start( 'preview_theme_ob_filter' );
868 add_action('setup_theme', 'preview_theme');
871 * Callback function for ob_start() to capture all links in the theme.
876 * @param string $content
879 function preview_theme_ob_filter( $content ) {
880 return preg_replace_callback( "|(<a.*?href=([\"']))(.*?)([\"'].*?>)|", 'preview_theme_ob_filter_callback', $content );
884 * Manipulates preview theme links in order to control and maintain location.
886 * Callback function for preg_replace_callback() to accept and filter matches.
891 * @param array $matches
894 function preview_theme_ob_filter_callback( $matches ) {
895 if ( strpos($matches[4], 'onclick') !== false )
896 $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.
898 ( false !== strpos($matches[3], '/wp-admin/') )
900 ( false !== strpos($matches[3], '://') && 0 !== strpos($matches[3], get_option('home')) )
902 ( false !== strpos($matches[3], '/feed/') )
904 ( false !== strpos($matches[3], '/trackback/') )
906 return $matches[1] . "#$matches[2] onclick=$matches[2]return false;" . $matches[4];
908 $link = add_query_arg( array('preview' => 1, 'template' => $_GET['template'], 'stylesheet' => @$_GET['stylesheet'] ), $matches[3] );
909 if ( 0 === strpos($link, 'preview=1') )
911 return $matches[1] . attribute_escape( $link ) . $matches[4];
915 * Switches current theme to new template and stylesheet names.
918 * @uses do_action() Calls 'switch_theme' action on updated theme display name.
920 * @param string $template Template name
921 * @param string $stylesheet Stylesheet name.
923 function switch_theme($template, $stylesheet) {
924 update_option('template', $template);
925 update_option('stylesheet', $stylesheet);
926 delete_option('current_theme');
927 $theme = get_current_theme();
928 do_action('switch_theme', $theme);
932 * Checks that current theme files 'index.php' and 'style.css' exists.
934 * Does not check the 'default' theme. The 'default' theme should always exist
935 * or should have another theme renamed to that template name and directory
936 * path. Will switch theme to default if current theme does not validate.
937 * You can use the 'validate_current_theme' filter to return FALSE to
938 * disable this functionality.
944 function validate_current_theme() {
945 // Don't validate during an install/upgrade.
946 if ( defined('WP_INSTALLING') || !apply_filters( 'validate_current_theme', true ) )
949 if ( get_template() != 'default' && !file_exists(get_template_directory() . '/index.php') ) {
950 switch_theme('default', 'default');
954 if ( get_stylesheet() != 'default' && !file_exists(get_template_directory() . '/style.css') ) {
955 switch_theme('default', 'default');
963 * Retrieve theme modification value for the current theme.
965 * If the modification name does not exist, then the $default will be passed
966 * through {@link http://php.net/sprintf sprintf()} PHP function with the first
967 * string the template directory URI and the second string the stylesheet
971 * @uses apply_filters() Calls 'theme_mod_$name' filter on the value.
973 * @param string $name Theme modification name.
974 * @param bool|string $default
977 function get_theme_mod($name, $default = false) {
978 $theme = get_current_theme();
980 $mods = get_option("mods_$theme");
982 if ( isset($mods[$name]) )
983 return apply_filters( "theme_mod_$name", $mods[$name] );
985 return apply_filters( "theme_mod_$name", sprintf($default, get_template_directory_uri(), get_stylesheet_directory_uri()) );
989 * Update theme modification value for the current theme.
993 * @param string $name Theme modification name.
994 * @param string $value theme modification value.
996 function set_theme_mod($name, $value) {
997 $theme = get_current_theme();
999 $mods = get_option("mods_$theme");
1001 $mods[$name] = $value;
1003 update_option("mods_$theme", $mods);
1004 wp_cache_delete("mods_$theme", 'options');
1008 * Remove theme modification name from current theme list.
1010 * If removing the name also removes all elements, then the entire option will
1015 * @param string $name Theme modification name.
1018 function remove_theme_mod( $name ) {
1019 $theme = get_current_theme();
1021 $mods = get_option("mods_$theme");
1023 if ( !isset($mods[$name]) )
1026 unset($mods[$name]);
1029 return remove_theme_mods();
1031 update_option("mods_$theme", $mods);
1032 wp_cache_delete("mods_$theme", 'options');
1036 * Remove theme modifications option for current theme.
1040 function remove_theme_mods() {
1041 $theme = get_current_theme();
1043 delete_option("mods_$theme");
1047 * Retrieve text color for custom header.
1050 * @uses HEADER_TEXTCOLOR
1054 function get_header_textcolor() {
1055 return get_theme_mod('header_textcolor', HEADER_TEXTCOLOR);
1059 * Display text color for custom header.
1063 function header_textcolor() {
1064 echo get_header_textcolor();
1068 * Retrieve header image for custom header.
1071 * @uses HEADER_IMAGE
1075 function get_header_image() {
1076 return get_theme_mod('header_image', HEADER_IMAGE);
1080 * Display header image path.
1084 function header_image() {
1085 echo get_header_image();
1089 * Add callbacks for image header display.
1091 * The parameter $header_callback callback will be required to display the
1092 * content for the 'wp_head' action. The parameter $admin_header_callback
1093 * callback will be added to Custom_Image_Header class and that will be added
1094 * to the 'admin_menu' action.
1097 * @uses Custom_Image_Header Sets up for $admin_header_callback for administration panel display.
1099 * @param callback $header_callback Call on 'wp_head' action.
1100 * @param callback $admin_header_callback Call on administration panels.
1102 function add_custom_image_header($header_callback, $admin_header_callback) {
1103 if ( ! empty($header_callback) )
1104 add_action('wp_head', $header_callback);
1108 require_once(ABSPATH . 'wp-admin/custom-header.php');
1109 $GLOBALS['custom_image_header'] =& new Custom_Image_Header($admin_header_callback);
1110 add_action('admin_menu', array(&$GLOBALS['custom_image_header'], 'init'));