PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/template.php

http://github.com/wordpress/wordpress
PHP | 727 lines | 223 code | 73 blank | 431 comment | 40 complexity | 67f3070b192ebf1a17713e7a861ca5d7 MD5 | raw file
Possible License(s): 0BSD
  1. <?php
  2. /**
  3. * Template loading functions.
  4. *
  5. * @package WordPress
  6. * @subpackage Template
  7. */
  8. /**
  9. * Retrieve path to a template
  10. *
  11. * Used to quickly retrieve the path of a template without including the file
  12. * extension. It will also check the parent theme, if the file exists, with
  13. * the use of locate_template(). Allows for more generic template location
  14. * without the use of the other get_*_template() functions.
  15. *
  16. * @since 1.5.0
  17. *
  18. * @param string $type Filename without extension.
  19. * @param array $templates An optional list of template candidates
  20. * @return string Full path to template file.
  21. */
  22. function get_query_template( $type, $templates = array() ) {
  23. $type = preg_replace( '|[^a-z0-9-]+|', '', $type );
  24. if ( empty( $templates ) ) {
  25. $templates = array( "{$type}.php" );
  26. }
  27. /**
  28. * Filters the list of template filenames that are searched for when retrieving a template to use.
  29. *
  30. * The last element in the array should always be the fallback template for this query type.
  31. *
  32. * Possible values for `$type` include: 'index', '404', 'archive', 'author', 'category', 'tag', 'taxonomy', 'date',
  33. * 'embed', 'home', 'frontpage', 'privacypolicy', 'page', 'paged', 'search', 'single', 'singular', and 'attachment'.
  34. *
  35. * @since 4.7.0
  36. *
  37. * @param array $templates A list of template candidates, in descending order of priority.
  38. */
  39. $templates = apply_filters( "{$type}_template_hierarchy", $templates );
  40. $template = locate_template( $templates );
  41. /**
  42. * Filters the path of the queried template by type.
  43. *
  44. * The dynamic portion of the hook name, `$type`, refers to the filename -- minus the file
  45. * extension and any non-alphanumeric characters delimiting words -- of the file to load.
  46. * This hook also applies to various types of files loaded as part of the Template Hierarchy.
  47. *
  48. * Possible values for `$type` include: 'index', '404', 'archive', 'author', 'category', 'tag', 'taxonomy', 'date',
  49. * 'embed', 'home', 'frontpage', 'privacypolicy', 'page', 'paged', 'search', 'single', 'singular', and 'attachment'.
  50. *
  51. * @since 1.5.0
  52. * @since 4.8.0 The `$type` and `$templates` parameters were added.
  53. *
  54. * @param string $template Path to the template. See locate_template().
  55. * @param string $type Sanitized filename without extension.
  56. * @param array $templates A list of template candidates, in descending order of priority.
  57. */
  58. return apply_filters( "{$type}_template", $template, $type, $templates );
  59. }
  60. /**
  61. * Retrieve path of index template in current or parent template.
  62. *
  63. * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  64. * and {@see '$type_template'} dynamic hooks, where `$type` is 'index'.
  65. *
  66. * @since 3.0.0
  67. *
  68. * @see get_query_template()
  69. *
  70. * @return string Full path to index template file.
  71. */
  72. function get_index_template() {
  73. return get_query_template( 'index' );
  74. }
  75. /**
  76. * Retrieve path of 404 template in current or parent template.
  77. *
  78. * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  79. * and {@see '$type_template'} dynamic hooks, where `$type` is '404'.
  80. *
  81. * @since 1.5.0
  82. *
  83. * @see get_query_template()
  84. *
  85. * @return string Full path to 404 template file.
  86. */
  87. function get_404_template() {
  88. return get_query_template( '404' );
  89. }
  90. /**
  91. * Retrieve path of archive template in current or parent template.
  92. *
  93. * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  94. * and {@see '$type_template'} dynamic hooks, where `$type` is 'archive'.
  95. *
  96. * @since 1.5.0
  97. *
  98. * @see get_query_template()
  99. *
  100. * @return string Full path to archive template file.
  101. */
  102. function get_archive_template() {
  103. $post_types = array_filter( (array) get_query_var( 'post_type' ) );
  104. $templates = array();
  105. if ( count( $post_types ) == 1 ) {
  106. $post_type = reset( $post_types );
  107. $templates[] = "archive-{$post_type}.php";
  108. }
  109. $templates[] = 'archive.php';
  110. return get_query_template( 'archive', $templates );
  111. }
  112. /**
  113. * Retrieve path of post type archive template in current or parent template.
  114. *
  115. * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  116. * and {@see '$type_template'} dynamic hooks, where `$type` is 'archive'.
  117. *
  118. * @since 3.7.0
  119. *
  120. * @see get_archive_template()
  121. *
  122. * @return string Full path to archive template file.
  123. */
  124. function get_post_type_archive_template() {
  125. $post_type = get_query_var( 'post_type' );
  126. if ( is_array( $post_type ) ) {
  127. $post_type = reset( $post_type );
  128. }
  129. $obj = get_post_type_object( $post_type );
  130. if ( ! ( $obj instanceof WP_Post_Type ) || ! $obj->has_archive ) {
  131. return '';
  132. }
  133. return get_archive_template();
  134. }
  135. /**
  136. * Retrieve path of author template in current or parent template.
  137. *
  138. * The hierarchy for this template looks like:
  139. *
  140. * 1. author-{nicename}.php
  141. * 2. author-{id}.php
  142. * 3. author.php
  143. *
  144. * An example of this is:
  145. *
  146. * 1. author-john.php
  147. * 2. author-1.php
  148. * 3. author.php
  149. *
  150. * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  151. * and {@see '$type_template'} dynamic hooks, where `$type` is 'author'.
  152. *
  153. * @since 1.5.0
  154. *
  155. * @see get_query_template()
  156. *
  157. * @return string Full path to author template file.
  158. */
  159. function get_author_template() {
  160. $author = get_queried_object();
  161. $templates = array();
  162. if ( $author instanceof WP_User ) {
  163. $templates[] = "author-{$author->user_nicename}.php";
  164. $templates[] = "author-{$author->ID}.php";
  165. }
  166. $templates[] = 'author.php';
  167. return get_query_template( 'author', $templates );
  168. }
  169. /**
  170. * Retrieve path of category template in current or parent template.
  171. *
  172. * The hierarchy for this template looks like:
  173. *
  174. * 1. category-{slug}.php
  175. * 2. category-{id}.php
  176. * 3. category.php
  177. *
  178. * An example of this is:
  179. *
  180. * 1. category-news.php
  181. * 2. category-2.php
  182. * 3. category.php
  183. *
  184. * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  185. * and {@see '$type_template'} dynamic hooks, where `$type` is 'category'.
  186. *
  187. * @since 1.5.0
  188. * @since 4.7.0 The decoded form of `category-{slug}.php` was added to the top of the
  189. * template hierarchy when the category slug contains multibyte characters.
  190. *
  191. * @see get_query_template()
  192. *
  193. * @return string Full path to category template file.
  194. */
  195. function get_category_template() {
  196. $category = get_queried_object();
  197. $templates = array();
  198. if ( ! empty( $category->slug ) ) {
  199. $slug_decoded = urldecode( $category->slug );
  200. if ( $slug_decoded !== $category->slug ) {
  201. $templates[] = "category-{$slug_decoded}.php";
  202. }
  203. $templates[] = "category-{$category->slug}.php";
  204. $templates[] = "category-{$category->term_id}.php";
  205. }
  206. $templates[] = 'category.php';
  207. return get_query_template( 'category', $templates );
  208. }
  209. /**
  210. * Retrieve path of tag template in current or parent template.
  211. *
  212. * The hierarchy for this template looks like:
  213. *
  214. * 1. tag-{slug}.php
  215. * 2. tag-{id}.php
  216. * 3. tag.php
  217. *
  218. * An example of this is:
  219. *
  220. * 1. tag-wordpress.php
  221. * 2. tag-3.php
  222. * 3. tag.php
  223. *
  224. * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  225. * and {@see '$type_template'} dynamic hooks, where `$type` is 'tag'.
  226. *
  227. * @since 2.3.0
  228. * @since 4.7.0 The decoded form of `tag-{slug}.php` was added to the top of the
  229. * template hierarchy when the tag slug contains multibyte characters.
  230. *
  231. * @see get_query_template()
  232. *
  233. * @return string Full path to tag template file.
  234. */
  235. function get_tag_template() {
  236. $tag = get_queried_object();
  237. $templates = array();
  238. if ( ! empty( $tag->slug ) ) {
  239. $slug_decoded = urldecode( $tag->slug );
  240. if ( $slug_decoded !== $tag->slug ) {
  241. $templates[] = "tag-{$slug_decoded}.php";
  242. }
  243. $templates[] = "tag-{$tag->slug}.php";
  244. $templates[] = "tag-{$tag->term_id}.php";
  245. }
  246. $templates[] = 'tag.php';
  247. return get_query_template( 'tag', $templates );
  248. }
  249. /**
  250. * Retrieve path of custom taxonomy term template in current or parent template.
  251. *
  252. * The hierarchy for this template looks like:
  253. *
  254. * 1. taxonomy-{taxonomy_slug}-{term_slug}.php
  255. * 2. taxonomy-{taxonomy_slug}.php
  256. * 3. taxonomy.php
  257. *
  258. * An example of this is:
  259. *
  260. * 1. taxonomy-location-texas.php
  261. * 2. taxonomy-location.php
  262. * 3. taxonomy.php
  263. *
  264. * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  265. * and {@see '$type_template'} dynamic hooks, where `$type` is 'taxonomy'.
  266. *
  267. * @since 2.5.0
  268. * @since 4.7.0 The decoded form of `taxonomy-{taxonomy_slug}-{term_slug}.php` was added to the top of the
  269. * template hierarchy when the term slug contains multibyte characters.
  270. *
  271. * @see get_query_template()
  272. *
  273. * @return string Full path to custom taxonomy term template file.
  274. */
  275. function get_taxonomy_template() {
  276. $term = get_queried_object();
  277. $templates = array();
  278. if ( ! empty( $term->slug ) ) {
  279. $taxonomy = $term->taxonomy;
  280. $slug_decoded = urldecode( $term->slug );
  281. if ( $slug_decoded !== $term->slug ) {
  282. $templates[] = "taxonomy-$taxonomy-{$slug_decoded}.php";
  283. }
  284. $templates[] = "taxonomy-$taxonomy-{$term->slug}.php";
  285. $templates[] = "taxonomy-$taxonomy.php";
  286. }
  287. $templates[] = 'taxonomy.php';
  288. return get_query_template( 'taxonomy', $templates );
  289. }
  290. /**
  291. * Retrieve path of date template in current or parent template.
  292. *
  293. * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  294. * and {@see '$type_template'} dynamic hooks, where `$type` is 'date'.
  295. *
  296. * @since 1.5.0
  297. *
  298. * @see get_query_template()
  299. *
  300. * @return string Full path to date template file.
  301. */
  302. function get_date_template() {
  303. return get_query_template( 'date' );
  304. }
  305. /**
  306. * Retrieve path of home template in current or parent template.
  307. *
  308. * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  309. * and {@see '$type_template'} dynamic hooks, where `$type` is 'home'.
  310. *
  311. * @since 1.5.0
  312. *
  313. * @see get_query_template()
  314. *
  315. * @return string Full path to home template file.
  316. */
  317. function get_home_template() {
  318. $templates = array( 'home.php', 'index.php' );
  319. return get_query_template( 'home', $templates );
  320. }
  321. /**
  322. * Retrieve path of front page template in current or parent template.
  323. *
  324. * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  325. * and {@see '$type_template'} dynamic hooks, where `$type` is 'frontpage'.
  326. *
  327. * @since 3.0.0
  328. *
  329. * @see get_query_template()
  330. *
  331. * @return string Full path to front page template file.
  332. */
  333. function get_front_page_template() {
  334. $templates = array( 'front-page.php' );
  335. return get_query_template( 'frontpage', $templates );
  336. }
  337. /**
  338. * Retrieve path of Privacy Policy page template in current or parent template.
  339. *
  340. * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  341. * and {@see '$type_template'} dynamic hooks, where `$type` is 'privacypolicy'.
  342. *
  343. * @since 5.2.0
  344. *
  345. * @see get_query_template()
  346. *
  347. * @return string Full path to privacy policy template file.
  348. */
  349. function get_privacy_policy_template() {
  350. $templates = array( 'privacy-policy.php' );
  351. return get_query_template( 'privacypolicy', $templates );
  352. }
  353. /**
  354. * Retrieve path of page template in current or parent template.
  355. *
  356. * The hierarchy for this template looks like:
  357. *
  358. * 1. {Page Template}.php
  359. * 2. page-{page_name}.php
  360. * 3. page-{id}.php
  361. * 4. page.php
  362. *
  363. * An example of this is:
  364. *
  365. * 1. page-templates/full-width.php
  366. * 2. page-about.php
  367. * 3. page-4.php
  368. * 4. page.php
  369. *
  370. * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  371. * and {@see '$type_template'} dynamic hooks, where `$type` is 'page'.
  372. *
  373. * @since 1.5.0
  374. * @since 4.7.0 The decoded form of `page-{page_name}.php` was added to the top of the
  375. * template hierarchy when the page name contains multibyte characters.
  376. *
  377. * @see get_query_template()
  378. *
  379. * @return string Full path to page template file.
  380. */
  381. function get_page_template() {
  382. $id = get_queried_object_id();
  383. $template = get_page_template_slug();
  384. $pagename = get_query_var( 'pagename' );
  385. if ( ! $pagename && $id ) {
  386. // If a static page is set as the front page, $pagename will not be set.
  387. // Retrieve it from the queried object.
  388. $post = get_queried_object();
  389. if ( $post ) {
  390. $pagename = $post->post_name;
  391. }
  392. }
  393. $templates = array();
  394. if ( $template && 0 === validate_file( $template ) ) {
  395. $templates[] = $template;
  396. }
  397. if ( $pagename ) {
  398. $pagename_decoded = urldecode( $pagename );
  399. if ( $pagename_decoded !== $pagename ) {
  400. $templates[] = "page-{$pagename_decoded}.php";
  401. }
  402. $templates[] = "page-{$pagename}.php";
  403. }
  404. if ( $id ) {
  405. $templates[] = "page-{$id}.php";
  406. }
  407. $templates[] = 'page.php';
  408. return get_query_template( 'page', $templates );
  409. }
  410. /**
  411. * Retrieve path of search template in current or parent template.
  412. *
  413. * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  414. * and {@see '$type_template'} dynamic hooks, where `$type` is 'search'.
  415. *
  416. * @since 1.5.0
  417. *
  418. * @see get_query_template()
  419. *
  420. * @return string Full path to search template file.
  421. */
  422. function get_search_template() {
  423. return get_query_template( 'search' );
  424. }
  425. /**
  426. * Retrieve path of single template in current or parent template. Applies to single Posts,
  427. * single Attachments, and single custom post types.
  428. *
  429. * The hierarchy for this template looks like:
  430. *
  431. * 1. {Post Type Template}.php
  432. * 2. single-{post_type}-{post_name}.php
  433. * 3. single-{post_type}.php
  434. * 4. single.php
  435. *
  436. * An example of this is:
  437. *
  438. * 1. templates/full-width.php
  439. * 2. single-post-hello-world.php
  440. * 3. single-post.php
  441. * 4. single.php
  442. *
  443. * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  444. * and {@see '$type_template'} dynamic hooks, where `$type` is 'single'.
  445. *
  446. * @since 1.5.0
  447. * @since 4.4.0 `single-{post_type}-{post_name}.php` was added to the top of the template hierarchy.
  448. * @since 4.7.0 The decoded form of `single-{post_type}-{post_name}.php` was added to the top of the
  449. * template hierarchy when the post name contains multibyte characters.
  450. * @since 4.7.0 `{Post Type Template}.php` was added to the top of the template hierarchy.
  451. *
  452. * @see get_query_template()
  453. *
  454. * @return string Full path to single template file.
  455. */
  456. function get_single_template() {
  457. $object = get_queried_object();
  458. $templates = array();
  459. if ( ! empty( $object->post_type ) ) {
  460. $template = get_page_template_slug( $object );
  461. if ( $template && 0 === validate_file( $template ) ) {
  462. $templates[] = $template;
  463. }
  464. $name_decoded = urldecode( $object->post_name );
  465. if ( $name_decoded !== $object->post_name ) {
  466. $templates[] = "single-{$object->post_type}-{$name_decoded}.php";
  467. }
  468. $templates[] = "single-{$object->post_type}-{$object->post_name}.php";
  469. $templates[] = "single-{$object->post_type}.php";
  470. }
  471. $templates[] = 'single.php';
  472. return get_query_template( 'single', $templates );
  473. }
  474. /**
  475. * Retrieves an embed template path in the current or parent template.
  476. *
  477. * The hierarchy for this template looks like:
  478. *
  479. * 1. embed-{post_type}-{post_format}.php
  480. * 2. embed-{post_type}.php
  481. * 3. embed.php
  482. *
  483. * An example of this is:
  484. *
  485. * 1. embed-post-audio.php
  486. * 2. embed-post.php
  487. * 3. embed.php
  488. *
  489. * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  490. * and {@see '$type_template'} dynamic hooks, where `$type` is 'embed'.
  491. *
  492. * @since 4.5.0
  493. *
  494. * @see get_query_template()
  495. *
  496. * @return string Full path to embed template file.
  497. */
  498. function get_embed_template() {
  499. $object = get_queried_object();
  500. $templates = array();
  501. if ( ! empty( $object->post_type ) ) {
  502. $post_format = get_post_format( $object );
  503. if ( $post_format ) {
  504. $templates[] = "embed-{$object->post_type}-{$post_format}.php";
  505. }
  506. $templates[] = "embed-{$object->post_type}.php";
  507. }
  508. $templates[] = 'embed.php';
  509. return get_query_template( 'embed', $templates );
  510. }
  511. /**
  512. * Retrieves the path of the singular template in current or parent template.
  513. *
  514. * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  515. * and {@see '$type_template'} dynamic hooks, where `$type` is 'singular'.
  516. *
  517. * @since 4.3.0
  518. *
  519. * @see get_query_template()
  520. *
  521. * @return string Full path to singular template file
  522. */
  523. function get_singular_template() {
  524. return get_query_template( 'singular' );
  525. }
  526. /**
  527. * Retrieve path of attachment template in current or parent template.
  528. *
  529. * The hierarchy for this template looks like:
  530. *
  531. * 1. {mime_type}-{sub_type}.php
  532. * 2. {sub_type}.php
  533. * 3. {mime_type}.php
  534. * 4. attachment.php
  535. *
  536. * An example of this is:
  537. *
  538. * 1. image-jpeg.php
  539. * 2. jpeg.php
  540. * 3. image.php
  541. * 4. attachment.php
  542. *
  543. * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  544. * and {@see '$type_template'} dynamic hooks, where `$type` is 'attachment'.
  545. *
  546. * @since 2.0.0
  547. * @since 4.3.0 The order of the mime type logic was reversed so the hierarchy is more logical.
  548. *
  549. * @see get_query_template()
  550. *
  551. * @global array $posts
  552. *
  553. * @return string Full path to attachment template file.
  554. */
  555. function get_attachment_template() {
  556. $attachment = get_queried_object();
  557. $templates = array();
  558. if ( $attachment ) {
  559. if ( false !== strpos( $attachment->post_mime_type, '/' ) ) {
  560. list( $type, $subtype ) = explode( '/', $attachment->post_mime_type );
  561. } else {
  562. list( $type, $subtype ) = array( $attachment->post_mime_type, '' );
  563. }
  564. if ( ! empty( $subtype ) ) {
  565. $templates[] = "{$type}-{$subtype}.php";
  566. $templates[] = "{$subtype}.php";
  567. }
  568. $templates[] = "{$type}.php";
  569. }
  570. $templates[] = 'attachment.php';
  571. return get_query_template( 'attachment', $templates );
  572. }
  573. /**
  574. * Retrieve the name of the highest priority template file that exists.
  575. *
  576. * Searches in the STYLESHEETPATH before TEMPLATEPATH and wp-includes/theme-compat
  577. * so that themes which inherit from a parent theme can just overload one file.
  578. *
  579. * @since 2.7.0
  580. *
  581. * @param string|array $template_names Template file(s) to search for, in order.
  582. * @param bool $load If true the template file will be loaded if it is found.
  583. * @param bool $require_once Whether to require_once or require. Default true. Has no effect if $load is false.
  584. * @return string The template filename if one is located.
  585. */
  586. function locate_template( $template_names, $load = false, $require_once = true ) {
  587. $located = '';
  588. foreach ( (array) $template_names as $template_name ) {
  589. if ( ! $template_name ) {
  590. continue;
  591. }
  592. if ( file_exists( STYLESHEETPATH . '/' . $template_name ) ) {
  593. $located = STYLESHEETPATH . '/' . $template_name;
  594. break;
  595. } elseif ( file_exists( TEMPLATEPATH . '/' . $template_name ) ) {
  596. $located = TEMPLATEPATH . '/' . $template_name;
  597. break;
  598. } elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
  599. $located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
  600. break;
  601. }
  602. }
  603. if ( $load && '' != $located ) {
  604. load_template( $located, $require_once );
  605. }
  606. return $located;
  607. }
  608. /**
  609. * Require the template file with WordPress environment.
  610. *
  611. * The globals are set up for the template file to ensure that the WordPress
  612. * environment is available from within the function. The query variables are
  613. * also available.
  614. *
  615. * @since 1.5.0
  616. *
  617. * @global array $posts
  618. * @global WP_Post $post Global post object.
  619. * @global bool $wp_did_header
  620. * @global WP_Query $wp_query WordPress Query object.
  621. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  622. * @global wpdb $wpdb WordPress database abstraction object.
  623. * @global string $wp_version
  624. * @global WP $wp Current WordPress environment instance.
  625. * @global int $id
  626. * @global WP_Comment $comment Global comment object.
  627. * @global int $user_ID
  628. *
  629. * @param string $_template_file Path to template file.
  630. * @param bool $require_once Whether to require_once or require. Default true.
  631. */
  632. function load_template( $_template_file, $require_once = true ) {
  633. global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
  634. if ( is_array( $wp_query->query_vars ) ) {
  635. /*
  636. * This use of extract() cannot be removed. There are many possible ways that
  637. * templates could depend on variables that it creates existing, and no way to
  638. * detect and deprecate it.
  639. *
  640. * Passing the EXTR_SKIP flag is the safest option, ensuring globals and
  641. * function variables cannot be overwritten.
  642. */
  643. // phpcs:ignore WordPress.PHP.DontExtract.extract_extract
  644. extract( $wp_query->query_vars, EXTR_SKIP );
  645. }
  646. if ( isset( $s ) ) {
  647. $s = esc_attr( $s );
  648. }
  649. if ( $require_once ) {
  650. require_once $_template_file;
  651. } else {
  652. require $_template_file;
  653. }
  654. }