| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * Template loading functions. |
|---|
| 4 | * |
|---|
| 5 | * @package WordPress |
|---|
| 6 | * @subpackage Template |
|---|
| 7 | */ |
|---|
| 8 | |
|---|
| 9 | /** |
|---|
| 10 | * Retrieve path to a template |
|---|
| 11 | * |
|---|
| 12 | * Used to quickly retrieve the path of a template without including the file |
|---|
| 13 | * extension. It will also check the parent theme, if the file exists, with |
|---|
| 14 | * the use of {@link locate_template()}. Allows for more generic template location |
|---|
| 15 | * without the use of the other get_*_template() functions. |
|---|
| 16 | * |
|---|
| 17 | * @since 1.5.0 |
|---|
| 18 | * |
|---|
| 19 | * @param string $type Filename without extension. |
|---|
| 20 | * @param array $templates An optional list of template candidates |
|---|
| 21 | * @return string Full path to file. |
|---|
| 22 | */ |
|---|
| 23 | function get_query_template( $type, $templates = array() ) { |
|---|
| 24 | $type = preg_replace( '|[^a-z0-9-]+|', '', $type ); |
|---|
| 25 | |
|---|
| 26 | if ( empty( $templates ) ) |
|---|
| 27 | $templates = array("{$type}.php"); |
|---|
| 28 | |
|---|
| 29 | $template = locate_template( $templates ); |
|---|
| 30 | /** |
|---|
| 31 | * Filter the path of the queried template by type. |
|---|
| 32 | * |
|---|
| 33 | * The dynamic portion of the hook name, $type, refers to the filename |
|---|
| 34 | * -- minus the extension -- of the file to load. This hook also applies |
|---|
| 35 | * to various types of files loaded as part of the Template Hierarchy. |
|---|
| 36 | * |
|---|
| 37 | * @since 1.5.0 |
|---|
| 38 | * |
|---|
| 39 | * @param string $template Path to the template. @see locate_template() |
|---|
| 40 | */ |
|---|
| 41 | return apply_filters( "{$type}_template", $template ); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | /** |
|---|
| 45 | * Retrieve path of index template in current or parent template. |
|---|
| 46 | * |
|---|
| 47 | * @since 3.0.0 |
|---|
| 48 | * |
|---|
| 49 | * @return string |
|---|
| 50 | */ |
|---|
| 51 | function get_index_template() { |
|---|
| 52 | return get_query_template('index'); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | /** |
|---|
| 56 | * Retrieve path of 404 template in current or parent template. |
|---|
| 57 | * |
|---|
| 58 | * @since 1.5.0 |
|---|
| 59 | * |
|---|
| 60 | * @return string |
|---|
| 61 | */ |
|---|
| 62 | function get_404_template() { |
|---|
| 63 | return get_query_template('404'); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | /** |
|---|
| 67 | * Retrieve path of archive template in current or parent template. |
|---|
| 68 | * |
|---|
| 69 | * @since 1.5.0 |
|---|
| 70 | * |
|---|
| 71 | * @return string |
|---|
| 72 | */ |
|---|
| 73 | function get_archive_template() { |
|---|
| 74 | $post_types = array_filter( (array) get_query_var( 'post_type' ) ); |
|---|
| 75 | |
|---|
| 76 | $templates = array(); |
|---|
| 77 | |
|---|
| 78 | if ( count( $post_types ) == 1 ) { |
|---|
| 79 | $post_type = reset( $post_types ); |
|---|
| 80 | $templates[] = "archive-{$post_type}.php"; |
|---|
| 81 | } |
|---|
| 82 | $templates[] = 'archive.php'; |
|---|
| 83 | |
|---|
| 84 | return get_query_template( 'archive', $templates ); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | /** |
|---|
| 88 | * Retrieve path of post type archive template in current or parent template. |
|---|
| 89 | * |
|---|
| 90 | * @since 3.7.0 |
|---|
| 91 | * |
|---|
| 92 | * @return string |
|---|
| 93 | */ |
|---|
| 94 | function get_post_type_archive_template() { |
|---|
| 95 | $post_type = get_query_var( 'post_type' ); |
|---|
| 96 | if ( is_array( $post_type ) ) |
|---|
| 97 | $post_type = reset( $post_type ); |
|---|
| 98 | |
|---|
| 99 | $obj = get_post_type_object( $post_type ); |
|---|
| 100 | if ( ! $obj->has_archive ) |
|---|
| 101 | return ''; |
|---|
| 102 | |
|---|
| 103 | return get_archive_template(); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | /** |
|---|
| 107 | * Retrieve path of author template in current or parent template. |
|---|
| 108 | * |
|---|
| 109 | * @since 1.5.0 |
|---|
| 110 | * |
|---|
| 111 | * @return string |
|---|
| 112 | */ |
|---|
| 113 | function get_author_template() { |
|---|
| 114 | $author = get_queried_object(); |
|---|
| 115 | |
|---|
| 116 | $templates = array(); |
|---|
| 117 | |
|---|
| 118 | if ( is_a( $author, 'WP_User' ) ) { |
|---|
| 119 | $templates[] = "author-{$author->user_nicename}.php"; |
|---|
| 120 | $templates[] = "author-{$author->ID}.php"; |
|---|
| 121 | } |
|---|
| 122 | $templates[] = 'author.php'; |
|---|
| 123 | |
|---|
| 124 | return get_query_template( 'author', $templates ); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | /** |
|---|
| 128 | * Retrieve path of category template in current or parent template. |
|---|
| 129 | * |
|---|
| 130 | * Works by first retrieving the current slug, for example 'category-default.php', and then |
|---|
| 131 | * trying category ID, for example 'category-1.php', and will finally fall back to category.php |
|---|
| 132 | * template, if those files don't exist. |
|---|
| 133 | * |
|---|
| 134 | * @since 1.5.0 |
|---|
| 135 | * @uses apply_filters() Calls 'category_template' on file path of category template. |
|---|
| 136 | * |
|---|
| 137 | * @return string |
|---|
| 138 | */ |
|---|
| 139 | function get_category_template() { |
|---|
| 140 | $category = get_queried_object(); |
|---|
| 141 | |
|---|
| 142 | $templates = array(); |
|---|
| 143 | |
|---|
| 144 | if ( ! empty( $category->slug ) ) { |
|---|
| 145 | $templates[] = "category-{$category->slug}.php"; |
|---|
| 146 | $templates[] = "category-{$category->term_id}.php"; |
|---|
| 147 | } |
|---|
| 148 | $templates[] = 'category.php'; |
|---|
| 149 | |
|---|
| 150 | return get_query_template( 'category', $templates ); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | /** |
|---|
| 154 | * Retrieve path of tag template in current or parent template. |
|---|
| 155 | * |
|---|
| 156 | * Works by first retrieving the current tag name, for example 'tag-wordpress.php', and then |
|---|
| 157 | * trying tag ID, for example 'tag-1.php', and will finally fall back to tag.php |
|---|
| 158 | * template, if those files don't exist. |
|---|
| 159 | * |
|---|
| 160 | * @since 2.3.0 |
|---|
| 161 | * @uses apply_filters() Calls 'tag_template' on file path of tag template. |
|---|
| 162 | * |
|---|
| 163 | * @return string |
|---|
| 164 | */ |
|---|
| 165 | function get_tag_template() { |
|---|
| 166 | $tag = get_queried_object(); |
|---|
| 167 | |
|---|
| 168 | $templates = array(); |
|---|
| 169 | |
|---|
| 170 | if ( ! empty( $tag->slug ) ) { |
|---|
| 171 | $templates[] = "tag-{$tag->slug}.php"; |
|---|
| 172 | $templates[] = "tag-{$tag->term_id}.php"; |
|---|
| 173 | } |
|---|
| 174 | $templates[] = 'tag.php'; |
|---|
| 175 | |
|---|
| 176 | return get_query_template( 'tag', $templates ); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | /** |
|---|
| 180 | * Retrieve path of taxonomy template in current or parent template. |
|---|
| 181 | * |
|---|
| 182 | * Retrieves the taxonomy and term, if term is available. The template is |
|---|
| 183 | * prepended with 'taxonomy-' and followed by both the taxonomy string and |
|---|
| 184 | * the taxonomy string followed by a dash and then followed by the term. |
|---|
| 185 | * |
|---|
| 186 | * The taxonomy and term template is checked and used first, if it exists. |
|---|
| 187 | * Second, just the taxonomy template is checked, and then finally, taxonomy.php |
|---|
| 188 | * template is used. If none of the files exist, then it will fall back on to |
|---|
| 189 | * index.php. |
|---|
| 190 | * |
|---|
| 191 | * @return string |
|---|
| 192 | */ |
|---|
| 193 | function get_taxonomy_template() { |
|---|
| 194 | $term = get_queried_object(); |
|---|
| 195 | |
|---|
| 196 | $templates = array(); |
|---|
| 197 | |
|---|
| 198 | if ( ! empty( $term->slug ) ) { |
|---|
| 199 | $taxonomy = $term->taxonomy; |
|---|
| 200 | $templates[] = "taxonomy-$taxonomy-{$term->slug}.php"; |
|---|
| 201 | $templates[] = "taxonomy-$taxonomy.php"; |
|---|
| 202 | } |
|---|
| 203 | $templates[] = 'taxonomy.php'; |
|---|
| 204 | |
|---|
| 205 | return get_query_template( 'taxonomy', $templates ); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | /** |
|---|
| 209 | * Retrieve path of date template in current or parent template. |
|---|
| 210 | * |
|---|
| 211 | * @since 1.5.0 |
|---|
| 212 | * |
|---|
| 213 | * @return string |
|---|
| 214 | */ |
|---|
| 215 | function get_date_template() { |
|---|
| 216 | return get_query_template('date'); |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | /** |
|---|
| 220 | * Retrieve path of home template in current or parent template. |
|---|
| 221 | * |
|---|
| 222 | * This is the template used for the page containing the blog posts. |
|---|
| 223 | * |
|---|
| 224 | * Attempts to locate 'home.php' first before falling back to 'index.php'. |
|---|
| 225 | * |
|---|
| 226 | * @since 1.5.0 |
|---|
| 227 | * @uses apply_filters() Calls 'home_template' on file path of home template. |
|---|
| 228 | * |
|---|
| 229 | * @return string |
|---|
| 230 | */ |
|---|
| 231 | function get_home_template() { |
|---|
| 232 | $templates = array( 'home.php', 'index.php' ); |
|---|
| 233 | |
|---|
| 234 | return get_query_template( 'home', $templates ); |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | /** |
|---|
| 238 | * Retrieve path of front-page template in current or parent template. |
|---|
| 239 | * |
|---|
| 240 | * Looks for 'front-page.php'. |
|---|
| 241 | * |
|---|
| 242 | * @since 3.0.0 |
|---|
| 243 | * @uses apply_filters() Calls 'front_page_template' on file path of template. |
|---|
| 244 | * |
|---|
| 245 | * @return string |
|---|
| 246 | */ |
|---|
| 247 | function get_front_page_template() { |
|---|
| 248 | $templates = array('front-page.php'); |
|---|
| 249 | |
|---|
| 250 | return get_query_template( 'front_page', $templates ); |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | /** |
|---|
| 254 | * Retrieve path of page template in current or parent template. |
|---|
| 255 | * |
|---|
| 256 | * Will first look for the specifically assigned page template. |
|---|
| 257 | * Then will search for 'page-{slug}.php', followed by 'page-{id}.php', |
|---|
| 258 | * and finally 'page.php'. |
|---|
| 259 | * |
|---|
| 260 | * @since 1.5.0 |
|---|
| 261 | * |
|---|
| 262 | * @return string |
|---|
| 263 | */ |
|---|
| 264 | function get_page_template() { |
|---|
| 265 | $id = get_queried_object_id(); |
|---|
| 266 | $template = get_page_template_slug(); |
|---|
| 267 | $pagename = get_query_var('pagename'); |
|---|
| 268 | |
|---|
| 269 | if ( ! $pagename && $id ) { |
|---|
| 270 | // If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object |
|---|
| 271 | $post = get_queried_object(); |
|---|
| 272 | if ( $post ) |
|---|
| 273 | $pagename = $post->post_name; |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | $templates = array(); |
|---|
| 277 | if ( $template && 0 === validate_file( $template ) ) |
|---|
| 278 | $templates[] = $template; |
|---|
| 279 | if ( $pagename ) |
|---|
| 280 | $templates[] = "page-$pagename.php"; |
|---|
| 281 | if ( $id ) |
|---|
| 282 | $templates[] = "page-$id.php"; |
|---|
| 283 | $templates[] = 'page.php'; |
|---|
| 284 | |
|---|
| 285 | return get_query_template( 'page', $templates ); |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | /** |
|---|
| 289 | * Retrieve path of paged template in current or parent template. |
|---|
| 290 | * |
|---|
| 291 | * @since 1.5.0 |
|---|
| 292 | * |
|---|
| 293 | * @return string |
|---|
| 294 | */ |
|---|
| 295 | function get_paged_template() { |
|---|
| 296 | return get_query_template('paged'); |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | /** |
|---|
| 300 | * Retrieve path of search template in current or parent template. |
|---|
| 301 | * |
|---|
| 302 | * @since 1.5.0 |
|---|
| 303 | * |
|---|
| 304 | * @return string |
|---|
| 305 | */ |
|---|
| 306 | function get_search_template() { |
|---|
| 307 | return get_query_template('search'); |
|---|
| 308 | } |
|---|
| 309 | |
|---|
| 310 | /** |
|---|
| 311 | * Retrieve path of single template in current or parent template. |
|---|
| 312 | * |
|---|
| 313 | * @since 1.5.0 |
|---|
| 314 | * |
|---|
| 315 | * @return string |
|---|
| 316 | */ |
|---|
| 317 | function get_single_template() { |
|---|
| 318 | $object = get_queried_object(); |
|---|
| 319 | |
|---|
| 320 | $templates = array(); |
|---|
| 321 | |
|---|
| 322 | if ( ! empty( $object->post_type ) ) |
|---|
| 323 | $templates[] = "single-{$object->post_type}.php"; |
|---|
| 324 | $templates[] = "single.php"; |
|---|
| 325 | |
|---|
| 326 | return get_query_template( 'single', $templates ); |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | /** |
|---|
| 330 | * Retrieve path of attachment template in current or parent template. |
|---|
| 331 | * |
|---|
| 332 | * The attachment path first checks if the first part of the mime type exists. |
|---|
| 333 | * The second check is for the second part of the mime type. The last check is |
|---|
| 334 | * for both types separated by an underscore. If neither are found then the file |
|---|
| 335 | * 'attachment.php' is checked and returned. |
|---|
| 336 | * |
|---|
| 337 | * Some examples for the 'text/plain' mime type are 'text.php', 'plain.php', and |
|---|
| 338 | * finally 'text_plain.php'. |
|---|
| 339 | * |
|---|
| 340 | * @since 2.0.0 |
|---|
| 341 | * |
|---|
| 342 | * @return string |
|---|
| 343 | */ |
|---|
| 344 | function get_attachment_template() { |
|---|
| 345 | global $posts; |
|---|
| 346 | |
|---|
| 347 | if ( ! empty( $posts ) && isset( $posts[0]->post_mime_type ) ) { |
|---|
| 348 | $type = explode( '/', $posts[0]->post_mime_type ); |
|---|
| 349 | |
|---|
| 350 | if ( ! empty( $type ) ) { |
|---|
| 351 | if ( $template = get_query_template( $type[0] ) ) |
|---|
| 352 | return $template; |
|---|
| 353 | elseif ( ! empty( $type[1] ) ) { |
|---|
| 354 | if ( $template = get_query_template( $type[1] ) ) |
|---|
| 355 | return $template; |
|---|
| 356 | elseif ( $template = get_query_template( "$type[0]_$type[1]" ) ) |
|---|
| 357 | return $template; |
|---|
| 358 | } |
|---|
| 359 | } |
|---|
| 360 | } |
|---|
| 361 | |
|---|
| 362 | return get_query_template( 'attachment' ); |
|---|
| 363 | } |
|---|
| 364 | |
|---|
| 365 | /** |
|---|
| 366 | * Retrieve path of comment popup template in current or parent template. |
|---|
| 367 | * |
|---|
| 368 | * Checks for comment popup template in current template, if it exists or in the |
|---|
| 369 | * parent template. |
|---|
| 370 | * |
|---|
| 371 | * @since 1.5.0 |
|---|
| 372 | * @uses apply_filters() Calls 'comments_popup_template' filter on path. |
|---|
| 373 | * |
|---|
| 374 | * @return string |
|---|
| 375 | */ |
|---|
| 376 | function get_comments_popup_template() { |
|---|
| 377 | $template = get_query_template( 'comments_popup', array( 'comments-popup.php' ) ); |
|---|
| 378 | |
|---|
| 379 | // Backward compat code will be removed in a future release |
|---|
| 380 | if ('' == $template) |
|---|
| 381 | $template = ABSPATH . WPINC . '/theme-compat/comments-popup.php'; |
|---|
| 382 | |
|---|
| 383 | return $template; |
|---|
| 384 | } |
|---|
| 385 | |
|---|
| 386 | /** |
|---|
| 387 | * Retrieve the name of the highest priority template file that exists. |
|---|
| 388 | * |
|---|
| 389 | * Searches in the STYLESHEETPATH before TEMPLATEPATH so that themes which |
|---|
| 390 | * inherit from a parent theme can just overload one file. |
|---|
| 391 | * |
|---|
| 392 | * @since 2.7.0 |
|---|
| 393 | * |
|---|
| 394 | * @param string|array $template_names Template file(s) to search for, in order. |
|---|
| 395 | * @param bool $load If true the template file will be loaded if it is found. |
|---|
| 396 | * @param bool $require_once Whether to require_once or require. Default true. Has no effect if $load is false. |
|---|
| 397 | * @return string The template filename if one is located. |
|---|
| 398 | */ |
|---|
| 399 | function locate_template($template_names, $load = false, $require_once = true ) { |
|---|
| 400 | $located = ''; |
|---|
| 401 | foreach ( (array) $template_names as $template_name ) { |
|---|
| 402 | if ( !$template_name ) |
|---|
| 403 | continue; |
|---|
| 404 | if ( file_exists(STYLESHEETPATH . '/' . $template_name)) { |
|---|
| 405 | $located = STYLESHEETPATH . '/' . $template_name; |
|---|
| 406 | break; |
|---|
| 407 | } else if ( file_exists(TEMPLATEPATH . '/' . $template_name) ) { |
|---|
| 408 | $located = TEMPLATEPATH . '/' . $template_name; |
|---|
| 409 | break; |
|---|
| 410 | } |
|---|
| 411 | } |
|---|
| 412 | |
|---|
| 413 | if ( $load && '' != $located ) |
|---|
| 414 | load_template( $located, $require_once ); |
|---|
| 415 | |
|---|
| 416 | return $located; |
|---|
| 417 | } |
|---|
| 418 | |
|---|
| 419 | /** |
|---|
| 420 | * Require the template file with WordPress environment. |
|---|
| 421 | * |
|---|
| 422 | * The globals are set up for the template file to ensure that the WordPress |
|---|
| 423 | * environment is available from within the function. The query variables are |
|---|
| 424 | * also available. |
|---|
| 425 | * |
|---|
| 426 | * @since 1.5.0 |
|---|
| 427 | * |
|---|
| 428 | * @param string $_template_file Path to template file. |
|---|
| 429 | * @param bool $require_once Whether to require_once or require. Default true. |
|---|
| 430 | */ |
|---|
| 431 | function load_template( $_template_file, $require_once = true ) { |
|---|
| 432 | global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID; |
|---|
| 433 | |
|---|
| 434 | if ( is_array( $wp_query->query_vars ) ) |
|---|
| 435 | extract( $wp_query->query_vars, EXTR_SKIP ); |
|---|
| 436 | |
|---|
| 437 | if ( $require_once ) |
|---|
| 438 | require_once( $_template_file ); |
|---|
| 439 | else |
|---|
| 440 | require( $_template_file ); |
|---|
| 441 | } |
|---|
| 442 | |
|---|