3 * Theme/template/stylesheet functions.
6 function get_stylesheet() {
7 return apply_filters('stylesheet', get_option('stylesheet'));
10 function get_stylesheet_directory() {
11 $stylesheet = get_stylesheet();
12 $stylesheet_dir = get_theme_root() . "/$stylesheet";
13 return apply_filters('stylesheet_directory', $stylesheet_dir, $stylesheet);
16 function get_stylesheet_directory_uri() {
17 $stylesheet = get_stylesheet();
18 $stylesheet_dir_uri = get_theme_root_uri() . "/$stylesheet";
19 return apply_filters('stylesheet_directory_uri', $stylesheet_dir_uri, $stylesheet);
22 function get_stylesheet_uri() {
23 $stylesheet_dir_uri = get_stylesheet_directory_uri();
24 $stylesheet_uri = $stylesheet_dir_uri . "/style.css";
25 return apply_filters('stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri);
28 function get_locale_stylesheet_uri() {
30 $stylesheet_dir_uri = get_stylesheet_directory_uri();
31 $dir = get_stylesheet_directory();
32 $locale = get_locale();
33 if ( file_exists("$dir/$locale.css") )
34 $stylesheet_uri = "$stylesheet_dir_uri/$locale.css";
35 elseif ( !empty($wp_locale->text_direction) && file_exists("$dir/{$wp_locale->text_direction}.css") )
36 $stylesheet_uri = "$stylesheet_dir_uri/{$wp_locale->text_direction}.css";
39 return apply_filters('locale_stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri);
42 function get_template() {
43 return apply_filters('template', get_option('template'));
46 function get_template_directory() {
47 $template = get_template();
48 $template_dir = get_theme_root() . "/$template";
49 return apply_filters('template_directory', $template_dir, $template);
52 function get_template_directory_uri() {
53 $template = get_template();
54 $template_dir_uri = get_theme_root_uri() . "/$template";
55 return apply_filters('template_directory_uri', $template_dir_uri, $template);
58 function get_theme_data( $theme_file ) {
59 $themes_allowed_tags = array(
61 'href' => array(),'title' => array()
74 $theme_data = implode( '', file( $theme_file ) );
75 $theme_data = str_replace ( '\r', '\n', $theme_data );
76 preg_match( '|Theme Name:(.*)$|mi', $theme_data, $theme_name );
77 preg_match( '|Theme URI:(.*)$|mi', $theme_data, $theme_uri );
78 preg_match( '|Description:(.*)$|mi', $theme_data, $description );
79 preg_match( '|Author:(.*)$|mi', $theme_data, $author_name );
80 preg_match( '|Author URI:(.*)$|mi', $theme_data, $author_uri );
81 preg_match( '|Template:(.*)$|mi', $theme_data, $template );
83 if ( preg_match( '|Version:(.*)|i', $theme_data, $version ) )
84 $version = wp_kses( trim( $version[1] ), $themes_allowed_tags );
88 if ( preg_match('|Status:(.*)|i', $theme_data, $status) )
89 $status = wp_kses( trim( $status[1] ), $themes_allowed_tags );
93 $name = $theme = wp_kses( trim( $theme_name[1] ), $themes_allowed_tags );
94 $theme_uri = clean_url( trim( $theme_uri[1] ) );
95 $description = wptexturize( wp_kses( trim( $description[1] ), $themes_allowed_tags ) );
96 $template = wp_kses( trim( $template[1] ), $themes_allowed_tags );
98 $author_uri = clean_url( trim( $author_uri[1] ) );
100 if ( empty( $author_uri[1] ) ) {
101 $author = wp_kses( trim( $author_name[1] ), $themes_allowed_tags );
103 $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 ) );
106 return array( 'Name' => $name, 'Title' => $theme, 'URI' => $theme_uri, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => $template, 'Status' => $status );
109 function get_themes() {
110 global $wp_themes, $wp_broken_themes;
112 if ( isset($wp_themes) )
116 $wp_broken_themes = array();
117 $theme_loc = $theme_root = get_theme_root();
118 if ( '/' != ABSPATH ) // don't want to replace all forward slashes, see Trac #4541
119 $theme_loc = str_replace(ABSPATH, '', $theme_root);
121 // Files in wp-content/themes directory and one subdir down
122 $themes_dir = @ opendir($theme_root);
126 while ( ($theme_dir = readdir($themes_dir)) !== false ) {
127 if ( is_dir($theme_root . '/' . $theme_dir) && is_readable($theme_root . '/' . $theme_dir) ) {
128 if ( $theme_dir{0} == '.' || $theme_dir == '..' || $theme_dir == 'CVS' )
130 $stylish_dir = @ opendir($theme_root . '/' . $theme_dir);
131 $found_stylesheet = false;
132 while ( ($theme_file = readdir($stylish_dir)) !== false ) {
133 if ( $theme_file == 'style.css' ) {
134 $theme_files[] = $theme_dir . '/' . $theme_file;
135 $found_stylesheet = true;
139 @closedir($stylish_dir);
140 if ( !$found_stylesheet ) { // look for themes in that dir
141 $subdir = "$theme_root/$theme_dir";
142 $subdir_name = $theme_dir;
143 $theme_subdir = @ opendir( $subdir );
144 while ( ($theme_dir = readdir($theme_subdir)) !== false ) {
145 if ( is_dir( $subdir . '/' . $theme_dir) && is_readable($subdir . '/' . $theme_dir) ) {
146 if ( $theme_dir{0} == '.' || $theme_dir == '..' || $theme_dir == 'CVS' )
148 $stylish_dir = @ opendir($subdir . '/' . $theme_dir);
149 $found_stylesheet = false;
150 while ( ($theme_file = readdir($stylish_dir)) !== false ) {
151 if ( $theme_file == 'style.css' ) {
152 $theme_files[] = $subdir_name . '/' . $theme_dir . '/' . $theme_file;
153 $found_stylesheet = true;
157 @closedir($stylish_dir);
160 @closedir($theme_subdir);
161 $wp_broken_themes[$theme_dir] = array('Name' => $theme_dir, 'Title' => $theme_dir, 'Description' => __('Stylesheet is missing.'));
165 @closedir($theme_dir);
167 if ( !$themes_dir || !$theme_files )
172 foreach ( (array) $theme_files as $theme_file ) {
173 if ( !is_readable("$theme_root/$theme_file") ) {
174 $wp_broken_themes[$theme_file] = array('Name' => $theme_file, 'Title' => $theme_file, 'Description' => __('File not readable.'));
178 $theme_data = get_theme_data("$theme_root/$theme_file");
180 $name = $theme_data['Name'];
181 $title = $theme_data['Title'];
182 $description = wptexturize($theme_data['Description']);
183 $version = $theme_data['Version'];
184 $author = $theme_data['Author'];
185 $template = $theme_data['Template'];
186 $stylesheet = dirname($theme_file);
189 foreach ( array('png', 'gif', 'jpg', 'jpeg') as $ext ) {
190 if (file_exists("$theme_root/$stylesheet/screenshot.$ext")) {
191 $screenshot = "screenshot.$ext";
196 if ( empty($name) ) {
197 $name = dirname($theme_file);
201 if ( empty($template) ) {
202 if ( file_exists(dirname("$theme_root/$theme_file/index.php")) )
203 $template = dirname($theme_file);
208 $template = trim($template);
210 if ( !file_exists("$theme_root/$template/index.php") ) {
211 $parent_dir = dirname(dirname($theme_file));
212 if ( file_exists("$theme_root/$parent_dir/$template/index.php") ) {
213 $template = "$parent_dir/$template";
215 $wp_broken_themes[$name] = array('Name' => $name, 'Title' => $title, 'Description' => __('Template is missing.'));
220 $stylesheet_files = array();
221 $stylesheet_dir = @ dir("$theme_root/$stylesheet");
222 if ( $stylesheet_dir ) {
223 while ( ($file = $stylesheet_dir->read()) !== false ) {
224 if ( !preg_match('|^\.+$|', $file) && preg_match('|\.css$|', $file) )
225 $stylesheet_files[] = "$theme_loc/$stylesheet/$file";
229 $template_files = array();
230 $template_dir = @ dir("$theme_root/$template");
231 if ( $template_dir ) {
232 while(($file = $template_dir->read()) !== false) {
233 if ( !preg_match('|^\.+$|', $file) && preg_match('|\.php$|', $file) )
234 $template_files[] = "$theme_loc/$template/$file";
238 $template_dir = dirname($template_files[0]);
239 $stylesheet_dir = dirname($stylesheet_files[0]);
241 if ( empty($template_dir) )
243 if ( empty($stylesheet_dir) )
244 $stylesheet_dir = '/';
246 // Check for theme name collision. This occurs if a theme is copied to
247 // a new theme directory and the theme header is not updated. Whichever
248 // theme is first keeps the name. Subsequent themes get a suffix applied.
249 // The Default and Classic themes always trump their pretenders.
250 if ( isset($themes[$name]) ) {
251 if ( ('WordPress Default' == $name || 'WordPress Classic' == $name) &&
252 ('default' == $stylesheet || 'classic' == $stylesheet) ) {
253 // If another theme has claimed to be one of our default themes, move
255 $suffix = $themes[$name]['Stylesheet'];
256 $new_name = "$name/$suffix";
257 $themes[$new_name] = $themes[$name];
258 $themes[$new_name]['Name'] = $new_name;
260 $name = "$name/$stylesheet";
264 $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);
267 // Resolve theme dependencies.
268 $theme_names = array_keys($themes);
270 foreach ( (array) $theme_names as $theme_name ) {
271 $themes[$theme_name]['Parent Theme'] = '';
272 if ( $themes[$theme_name]['Stylesheet'] != $themes[$theme_name]['Template'] ) {
273 foreach ( (array) $theme_names as $parent_theme_name ) {
274 if ( ($themes[$parent_theme_name]['Stylesheet'] == $themes[$parent_theme_name]['Template']) && ($themes[$parent_theme_name]['Template'] == $themes[$theme_name]['Template']) ) {
275 $themes[$theme_name]['Parent Theme'] = $themes[$parent_theme_name]['Name'];
282 $wp_themes = $themes;
287 function get_theme($theme) {
288 $themes = get_themes();
290 if ( array_key_exists($theme, $themes) )
291 return $themes[$theme];
296 function get_current_theme() {
297 $themes = get_themes();
298 $theme_names = array_keys($themes);
299 $current_template = get_option('template');
300 $current_stylesheet = get_option('stylesheet');
301 $current_theme = 'WordPress Default';
304 foreach ( (array) $theme_names as $theme_name ) {
305 if ( $themes[$theme_name]['Stylesheet'] == $current_stylesheet &&
306 $themes[$theme_name]['Template'] == $current_template ) {
307 $current_theme = $themes[$theme_name]['Name'];
313 return $current_theme;
316 function get_theme_root() {
317 return apply_filters('theme_root', ABSPATH . "wp-content/themes");
320 function get_theme_root_uri() {
321 return apply_filters('theme_root_uri', get_option('siteurl') . "/wp-content/themes", get_option('siteurl'));
324 function get_query_template($type) {
326 if ( file_exists(TEMPLATEPATH . "/{$type}.php") )
327 $template = TEMPLATEPATH . "/{$type}.php";
329 return apply_filters("{$type}_template", $template);
332 function get_404_template() {
333 return get_query_template('404');
336 function get_archive_template() {
337 return get_query_template('archive');
340 function get_author_template() {
341 return get_query_template('author');
344 function get_category_template() {
346 if ( file_exists(TEMPLATEPATH . "/category-" . get_query_var('cat') . '.php') )
347 $template = TEMPLATEPATH . "/category-" . get_query_var('cat') . '.php';
348 elseif ( file_exists(TEMPLATEPATH . "/category.php") )
349 $template = TEMPLATEPATH . "/category.php";
351 return apply_filters('category_template', $template);
354 function get_tag_template() {
356 if ( file_exists(TEMPLATEPATH . "/tag-" . get_query_var('tag') . '.php') )
357 $template = TEMPLATEPATH . "/tag-" . get_query_var('tag') . '.php';
358 elseif ( file_exists(TEMPLATEPATH . "/tag.php") )
359 $template = TEMPLATEPATH . "/tag.php";
361 return apply_filters('tag_template', $template);
365 function get_date_template() {
366 return get_query_template('date');
369 function get_home_template() {
372 if ( file_exists(TEMPLATEPATH . "/home.php") )
373 $template = TEMPLATEPATH . "/home.php";
374 elseif ( file_exists(TEMPLATEPATH . "/index.php") )
375 $template = TEMPLATEPATH . "/index.php";
377 return apply_filters('home_template', $template);
380 function get_page_template() {
383 $id = (int) $wp_query->post->ID;
384 $template = get_post_meta($id, '_wp_page_template', true);
386 if ( 'default' == $template )
389 if ( !empty($template) && file_exists(TEMPLATEPATH . "/$template") )
390 $template = TEMPLATEPATH . "/$template";
391 elseif ( file_exists(TEMPLATEPATH . "/page.php") )
392 $template = TEMPLATEPATH . "/page.php";
396 return apply_filters('page_template', $template);
399 function get_paged_template() {
400 return get_query_template('paged');
403 function get_search_template() {
404 return get_query_template('search');
407 function get_single_template() {
408 return get_query_template('single');
411 function get_attachment_template() {
413 $type = explode('/', $posts[0]->post_mime_type);
414 if ( $template = get_query_template($type[0]) )
416 elseif ( $template = get_query_template($type[1]) )
418 elseif ( $template = get_query_template("$type[0]_$type[1]") )
421 return get_query_template('attachment');
424 function get_comments_popup_template() {
425 if ( file_exists( TEMPLATEPATH . '/comments-popup.php') )
426 $template = TEMPLATEPATH . '/comments-popup.php';
428 $template = get_theme_root() . '/default/comments-popup.php';
430 return apply_filters('comments_popup_template', $template);
433 function load_template($_template_file) {
434 global $posts, $post, $wp_did_header, $wp_did_template_redirect, $wp_query,
435 $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
437 if ( is_array($wp_query->query_vars) )
438 extract($wp_query->query_vars, EXTR_SKIP);
440 require_once($_template_file);
443 function locale_stylesheet() {
444 $stylesheet = get_locale_stylesheet_uri();
445 if ( empty($stylesheet) )
447 echo '<link rel="stylesheet" href="' . $stylesheet . '" type="text/css" media="screen" />';
450 function validate_current_theme() {
451 // Don't validate during an install/upgrade.
452 if ( defined('WP_INSTALLING') )
455 if ( get_template() != 'default' && !file_exists(get_template_directory() . '/index.php') ) {
456 update_option('template', 'default');
457 update_option('stylesheet', 'default');
458 do_action('switch_theme', 'Default');
462 if ( get_stylesheet() != 'default' && !file_exists(get_template_directory() . '/style.css') ) {
463 update_option('template', 'default');
464 update_option('stylesheet', 'default');
465 do_action('switch_theme', 'Default');
472 function get_theme_mod($name, $default = false) {
473 $theme = get_current_theme();
475 $mods = get_option("mods_$theme");
477 if ( isset($mods[$name]) )
478 return apply_filters( "theme_mod_$name", $mods[$name] );
480 return apply_filters( "theme_mod_$name", sprintf($default, get_template_directory_uri()) );
483 function set_theme_mod($name, $value) {
484 $theme = get_current_theme();
486 $mods = get_option("mods_$theme");
488 $mods[$name] = $value;
490 update_option("mods_$theme", $mods);
491 wp_cache_delete("mods_$theme", 'options');
494 function remove_theme_mod( $name ) {
495 $theme = get_current_theme();
497 $mods = get_option("mods_$theme");
499 if ( !isset($mods[$name]) )
505 return remove_theme_mods();
507 update_option("mods_$theme", $mods);
508 wp_cache_delete("mods_$theme", 'options');
511 function remove_theme_mods() {
512 $theme = get_current_theme();
514 delete_option("mods_$theme");
517 function get_header_textcolor() {
518 return get_theme_mod('header_textcolor', HEADER_TEXTCOLOR);
521 function header_textcolor() {
522 echo get_header_textcolor();
525 function get_header_image() {
526 return get_theme_mod('header_image', HEADER_IMAGE);
529 function header_image() {
530 echo get_header_image();
533 function add_custom_image_header($header_callback, $admin_header_callback) {
534 if ( ! empty($header_callback) )
535 add_action('wp_head', $header_callback);
539 require_once(ABSPATH . 'wp-admin/custom-header.php');
540 $GLOBALS['custom_image_header'] =& new Custom_Image_Header($admin_header_callback);
541 add_action('admin_menu', array(&$GLOBALS['custom_image_header'], 'init'));