template.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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. Retrieve it from the queried object
  387. $post = get_queried_object();
  388. if ( $post ) {
  389. $pagename = $post->post_name;
  390. }
  391. }
  392. $templates = array();
  393. if ( $template && 0 === validate_file( $template ) ) {
  394. $templates[] = $template;
  395. }
  396. if ( $pagename ) {
  397. $pagename_decoded = urldecode( $pagename );
  398. if ( $pagename_decoded !== $pagename ) {
  399. $templates[] = "page-{$pagename_decoded}.php";
  400. }
  401. $templates[] = "page-{$pagename}.php";
  402. }
  403. if ( $id ) {
  404. $templates[] = "page-{$id}.php";
  405. }
  406. $templates[] = 'page.php';
  407. return get_query_template( 'page', $templates );
  408. }
  409. /**
  410. * Retrieve path of search template in current or parent template.
  411. *
  412. * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  413. * and {@see '$type_template'} dynamic hooks, where `$type` is 'search'.
  414. *
  415. * @since 1.5.0
  416. *
  417. * @see get_query_template()
  418. *
  419. * @return string Full path to search template file.
  420. */
  421. function get_search_template() {
  422. return get_query_template( 'search' );
  423. }
  424. /**
  425. * Retrieve path of single template in current or parent template. Applies to single Posts,
  426. * single Attachments, and single custom post types.
  427. *
  428. * The hierarchy for this template looks like:
  429. *
  430. * 1. {Post Type Template}.php
  431. * 2. single-{post_type}-{post_name}.php
  432. * 3. single-{post_type}.php
  433. * 4. single.php
  434. *
  435. * An example of this is:
  436. *
  437. * 1. templates/full-width.php
  438. * 2. single-post-hello-world.php
  439. * 3. single-post.php
  440. * 4. single.php
  441. *
  442. * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  443. * and {@see '$type_template'} dynamic hooks, where `$type` is 'single'.
  444. *
  445. * @since 1.5.0
  446. * @since 4.4.0 `single-{post_type}-{post_name}.php` was added to the top of the template hierarchy.
  447. * @since 4.7.0 The decoded form of `single-{post_type}-{post_name}.php` was added to the top of the
  448. * template hierarchy when the post name contains multibyte characters.
  449. * @since 4.7.0 `{Post Type Template}.php` was added to the top of the template hierarchy.
  450. *
  451. * @see get_query_template()
  452. *
  453. * @return string Full path to single template file.
  454. */
  455. function get_single_template() {
  456. $object = get_queried_object();
  457. $templates = array();
  458. if ( ! empty( $object->post_type ) ) {
  459. $template = get_page_template_slug( $object );
  460. if ( $template && 0 === validate_file( $template ) ) {
  461. $templates[] = $template;
  462. }
  463. $name_decoded = urldecode( $object->post_name );
  464. if ( $name_decoded !== $object->post_name ) {
  465. $templates[] = "single-{$object->post_type}-{$name_decoded}.php";
  466. }
  467. $templates[] = "single-{$object->post_type}-{$object->post_name}.php";
  468. $templates[] = "single-{$object->post_type}.php";
  469. }
  470. $templates[] = 'single.php';
  471. return get_query_template( 'single', $templates );
  472. }
  473. /**
  474. * Retrieves an embed template path in the current or parent template.
  475. *
  476. * The hierarchy for this template looks like:
  477. *
  478. * 1. embed-{post_type}-{post_format}.php
  479. * 2. embed-{post_type}.php
  480. * 3. embed.php
  481. *
  482. * An example of this is:
  483. *
  484. * 1. embed-post-audio.php
  485. * 2. embed-post.php
  486. * 3. embed.php
  487. *
  488. * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  489. * and {@see '$type_template'} dynamic hooks, where `$type` is 'embed'.
  490. *
  491. * @since 4.5.0
  492. *
  493. * @see get_query_template()
  494. *
  495. * @return string Full path to embed template file.
  496. */
  497. function get_embed_template() {
  498. $object = get_queried_object();
  499. $templates = array();
  500. if ( ! empty( $object->post_type ) ) {
  501. $post_format = get_post_format( $object );
  502. if ( $post_format ) {
  503. $templates[] = "embed-{$object->post_type}-{$post_format}.php";
  504. }
  505. $templates[] = "embed-{$object->post_type}.php";
  506. }
  507. $templates[] = 'embed.php';
  508. return get_query_template( 'embed', $templates );
  509. }
  510. /**
  511. * Retrieves the path of the singular template in current or parent template.
  512. *
  513. * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  514. * and {@see '$type_template'} dynamic hooks, where `$type` is 'singular'.
  515. *
  516. * @since 4.3.0
  517. *
  518. * @see get_query_template()
  519. *
  520. * @return string Full path to singular template file
  521. */
  522. function get_singular_template() {
  523. return get_query_template( 'singular' );
  524. }
  525. /**
  526. * Retrieve path of attachment template in current or parent template.
  527. *
  528. * The hierarchy for this template looks like:
  529. *
  530. * 1. {mime_type}-{sub_type}.php
  531. * 2. {sub_type}.php
  532. * 3. {mime_type}.php
  533. * 4. attachment.php
  534. *
  535. * An example of this is:
  536. *
  537. * 1. image-jpeg.php
  538. * 2. jpeg.php
  539. * 3. image.php
  540. * 4. attachment.php
  541. *
  542. * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
  543. * and {@see '$type_template'} dynamic hooks, where `$type` is 'attachment'.
  544. *
  545. * @since 2.0.0
  546. * @since 4.3.0 The order of the mime type logic was reversed so the hierarchy is more logical.
  547. *
  548. * @see get_query_template()
  549. *
  550. * @global array $posts
  551. *
  552. * @return string Full path to attachment template file.
  553. */
  554. function get_attachment_template() {
  555. $attachment = get_queried_object();
  556. $templates = array();
  557. if ( $attachment ) {
  558. if ( false !== strpos( $attachment->post_mime_type, '/' ) ) {
  559. list( $type, $subtype ) = explode( '/', $attachment->post_mime_type );
  560. } else {
  561. list( $type, $subtype ) = array( $attachment->post_mime_type, '' );
  562. }
  563. if ( ! empty( $subtype ) ) {
  564. $templates[] = "{$type}-{$subtype}.php";
  565. $templates[] = "{$subtype}.php";
  566. }
  567. $templates[] = "{$type}.php";
  568. }
  569. $templates[] = 'attachment.php';
  570. return get_query_template( 'attachment', $templates );
  571. }
  572. /**
  573. * Retrieve the name of the highest priority template file that exists.
  574. *
  575. * Searches in the STYLESHEETPATH before TEMPLATEPATH and wp-includes/theme-compat
  576. * so that themes which inherit from a parent theme can just overload one file.
  577. *
  578. * @since 2.7.0
  579. *
  580. * @param string|array $template_names Template file(s) to search for, in order.
  581. * @param bool $load If true the template file will be loaded if it is found.
  582. * @param bool $require_once Whether to require_once or require. Default true. Has no effect if $load is false.
  583. * @return string The template filename if one is located.
  584. */
  585. function locate_template( $template_names, $load = false, $require_once = true ) {
  586. $located = '';
  587. foreach ( (array) $template_names as $template_name ) {
  588. if ( ! $template_name ) {
  589. continue;
  590. }
  591. if ( file_exists( STYLESHEETPATH . '/' . $template_name ) ) {
  592. $located = STYLESHEETPATH . '/' . $template_name;
  593. break;
  594. } elseif ( file_exists( TEMPLATEPATH . '/' . $template_name ) ) {
  595. $located = TEMPLATEPATH . '/' . $template_name;
  596. break;
  597. } elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
  598. $located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
  599. break;
  600. }
  601. }
  602. if ( $load && '' != $located ) {
  603. load_template( $located, $require_once );
  604. }
  605. return $located;
  606. }
  607. /**
  608. * Require the template file with WordPress environment.
  609. *
  610. * The globals are set up for the template file to ensure that the WordPress
  611. * environment is available from within the function. The query variables are
  612. * also available.
  613. *
  614. * @since 1.5.0
  615. *
  616. * @global array $posts
  617. * @global WP_Post $post Global post object.
  618. * @global bool $wp_did_header
  619. * @global WP_Query $wp_query WordPress Query object.
  620. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  621. * @global wpdb $wpdb WordPress database abstraction object.
  622. * @global string $wp_version
  623. * @global WP $wp Current WordPress environment instance.
  624. * @global int $id
  625. * @global WP_Comment $comment Global comment object.
  626. * @global int $user_ID
  627. *
  628. * @param string $_template_file Path to template file.
  629. * @param bool $require_once Whether to require_once or require. Default true.
  630. */
  631. function load_template( $_template_file, $require_once = true ) {
  632. global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
  633. if ( is_array( $wp_query->query_vars ) ) {
  634. /*
  635. * This use of extract() cannot be removed. There are many possible ways that
  636. * templates could depend on variables that it creates existing, and no way to
  637. * detect and deprecate it.
  638. *
  639. * Passing the EXTR_SKIP flag is the safest option, ensuring globals and
  640. * function variables cannot be overwritten.
  641. */
  642. // phpcs:ignore WordPress.PHP.DontExtract.extract_extract
  643. extract( $wp_query->query_vars, EXTR_SKIP );
  644. }
  645. if ( isset( $s ) ) {
  646. $s = esc_attr( $s );
  647. }
  648. if ( $require_once ) {
  649. require_once( $_template_file );
  650. } else {
  651. require( $_template_file );
  652. }
  653. }