Make WordPress Core

source: tags/6.5/src/wp-includes/template.php

Last change on this file was 57685, checked in by joemcgill, 5 months ago

Themes: Use original template paths when switching blogs.

This fixes a bug introduced by [57129] and [56635] in which deprecating the previous TEMPLATEPATH and STYLESHEETPATH constants in favor of get_template_directory() and get_stylesheet_directory() functions caused the active theme template path to change when using switch_to_blog().

This introduces a new function, wp_set_template_globals(), which is called during the bootstrap process to store the template paths to new globals values $wp_template_path and $wp_stylesheet_path. This restores behavior to how things worked prior to [56635] but retains the ability for template values to be reset for better testability.

Related #18298, #60025.

Props joemcgill, flixos90, mukesh27, swissspidy, manfcarlo, metropolis_john, jeremyfelt.
Fixes #60290.

  • Property svn:eol-style set to native
File size: 23.6 KB
Line 
1<?php
2/**
3 * Template loading functions.
4 *
5 * @package WordPress
6 * @subpackage Template
7 */
8
9/**
10 * Retrieves 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 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 string[] $templates An optional list of template candidates.
21 * @return string Full path to template file.
22 */
23function 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
30        /**
31         * Filters the list of template filenames that are searched for when retrieving a template to use.
32         *
33         * The dynamic portion of the hook name, `$type`, refers to the filename -- minus the file
34         * extension and any non-alphanumeric characters delimiting words -- of the file to load.
35         * The last element in the array should always be the fallback template for this query type.
36         *
37         * Possible hook names include:
38         *
39         *  - `404_template_hierarchy`
40         *  - `archive_template_hierarchy`
41         *  - `attachment_template_hierarchy`
42         *  - `author_template_hierarchy`
43         *  - `category_template_hierarchy`
44         *  - `date_template_hierarchy`
45         *  - `embed_template_hierarchy`
46         *  - `frontpage_template_hierarchy`
47         *  - `home_template_hierarchy`
48         *  - `index_template_hierarchy`
49         *  - `page_template_hierarchy`
50         *  - `paged_template_hierarchy`
51         *  - `privacypolicy_template_hierarchy`
52         *  - `search_template_hierarchy`
53         *  - `single_template_hierarchy`
54         *  - `singular_template_hierarchy`
55         *  - `tag_template_hierarchy`
56         *  - `taxonomy_template_hierarchy`
57         *
58         * @since 4.7.0
59         *
60         * @param string[] $templates A list of template candidates, in descending order of priority.
61         */
62        $templates = apply_filters( "{$type}_template_hierarchy", $templates );
63
64        $template = locate_template( $templates );
65
66        $template = locate_block_template( $template, $type, $templates );
67
68        /**
69         * Filters the path of the queried template by type.
70         *
71         * The dynamic portion of the hook name, `$type`, refers to the filename -- minus the file
72         * extension and any non-alphanumeric characters delimiting words -- of the file to load.
73         * This hook also applies to various types of files loaded as part of the Template Hierarchy.
74         *
75         * Possible hook names include:
76         *
77         *  - `404_template`
78         *  - `archive_template`
79         *  - `attachment_template`
80         *  - `author_template`
81         *  - `category_template`
82         *  - `date_template`
83         *  - `embed_template`
84         *  - `frontpage_template`
85         *  - `home_template`
86         *  - `index_template`
87         *  - `page_template`
88         *  - `paged_template`
89         *  - `privacypolicy_template`
90         *  - `search_template`
91         *  - `single_template`
92         *  - `singular_template`
93         *  - `tag_template`
94         *  - `taxonomy_template`
95         *
96         * @since 1.5.0
97         * @since 4.8.0 The `$type` and `$templates` parameters were added.
98         *
99         * @param string   $template  Path to the template. See locate_template().
100         * @param string   $type      Sanitized filename without extension.
101         * @param string[] $templates A list of template candidates, in descending order of priority.
102         */
103        return apply_filters( "{$type}_template", $template, $type, $templates );
104}
105
106/**
107 * Retrieves path of index template in current or parent template.
108 *
109 * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
110 * and {@see '$type_template'} dynamic hooks, where `$type` is 'index'.
111 *
112 * @since 3.0.0
113 *
114 * @see get_query_template()
115 *
116 * @return string Full path to index template file.
117 */
118function get_index_template() {
119        return get_query_template( 'index' );
120}
121
122/**
123 * Retrieves path of 404 template in current or parent template.
124 *
125 * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
126 * and {@see '$type_template'} dynamic hooks, where `$type` is '404'.
127 *
128 * @since 1.5.0
129 *
130 * @see get_query_template()
131 *
132 * @return string Full path to 404 template file.
133 */
134function get_404_template() {
135        return get_query_template( '404' );
136}
137
138/**
139 * Retrieves path of archive template in current or parent template.
140 *
141 * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
142 * and {@see '$type_template'} dynamic hooks, where `$type` is 'archive'.
143 *
144 * @since 1.5.0
145 *
146 * @see get_query_template()
147 *
148 * @return string Full path to archive template file.
149 */
150function get_archive_template() {
151        $post_types = array_filter( (array) get_query_var( 'post_type' ) );
152
153        $templates = array();
154
155        if ( count( $post_types ) === 1 ) {
156                $post_type   = reset( $post_types );
157                $templates[] = "archive-{$post_type}.php";
158        }
159        $templates[] = 'archive.php';
160
161        return get_query_template( 'archive', $templates );
162}
163
164/**
165 * Retrieves path of post type archive template in current or parent template.
166 *
167 * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
168 * and {@see '$type_template'} dynamic hooks, where `$type` is 'archive'.
169 *
170 * @since 3.7.0
171 *
172 * @see get_archive_template()
173 *
174 * @return string Full path to archive template file.
175 */
176function get_post_type_archive_template() {
177        $post_type = get_query_var( 'post_type' );
178        if ( is_array( $post_type ) ) {
179                $post_type = reset( $post_type );
180        }
181
182        $obj = get_post_type_object( $post_type );
183        if ( ! ( $obj instanceof WP_Post_Type ) || ! $obj->has_archive ) {
184                return '';
185        }
186
187        return get_archive_template();
188}
189
190/**
191 * Retrieves path of author template in current or parent template.
192 *
193 * The hierarchy for this template looks like:
194��*
195 * 1. author-{nicename}.php
196 * 2. author-{id}.php
197 * 3. author.php
198 *
199 * An example of this is:
200 *
201 * 1. author-john.php
202 * 2. author-1.php
203 * 3. author.php
204 *
205 * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
206 * and {@see '$type_template'} dynamic hooks, where `$type` is 'author'.
207 *
208 * @since 1.5.0
209 *
210 * @see get_query_template()
211 *
212 * @return string Full path to author template file.
213 */
214function get_author_template() {
215        $author = get_queried_object();
216
217        $templates = array();
218
219        if ( $author instanceof WP_User ) {
220                $templates[] = "author-{$author->user_nicename}.php";
221                $templates[] = "author-{$author->ID}.php";
222        }
223        $templates[] = 'author.php';
224
225        return get_query_template( 'author', $templates );
226}
227
228/**
229 * Retrieves path of category template in current or parent template.
230 *
231 * The hierarchy for this template looks like:
232 *
233 * 1. category-{slug}.php
234 * 2. category-{id}.php
235 * 3. category.php
236 *
237 * An example of this is:
238 *
239 * 1. category-news.php
240 * 2. category-2.php
241 * 3. category.php
242 *
243 * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
244 * and {@see '$type_template'} dynamic hooks, where `$type` is 'category'.
245 *
246 * @since 1.5.0
247 * @since 4.7.0 The decoded form of `category-{slug}.php` was added to the top of the
248 *              template hierarchy when the category slug contains multibyte characters.
249 *
250 * @see get_query_template()
251 *
252 * @return string Full path to category template file.
253 */
254function get_category_template() {
255        $category = get_queried_object();
256
257        $templates = array();
258
259        if ( ! empty( $category->slug ) ) {
260
261                $slug_decoded = urldecode( $category->slug );
262                if ( $slug_decoded !== $category->slug ) {
263                        $templates[] = "category-{$slug_decoded}.php";
264                }
265
266                $templates[] = "category-{$category->slug}.php";
267                $templates[] = "category-{$category->term_id}.php";
268        }
269        $templates[] = 'category.php';
270
271        return get_query_template( 'category', $templates );
272}
273
274/**
275 * Retrieves path of tag template in current or parent template.
276 *
277 * The hierarchy for this template looks like:
278 *
279 * 1. tag-{slug}.php
280 * 2. tag-{id}.php
281 * 3. tag.php
282 *
283 * An example of this is:
284 *
285 * 1. tag-wordpress.php
286 * 2. tag-3.php
287 * 3. tag.php
288 *
289 * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
290 * and {@see '$type_template'} dynamic hooks, where `$type` is 'tag'.
291 *
292 * @since 2.3.0
293 * @since 4.7.0 The decoded form of `tag-{slug}.php` was added to the top of the
294 *              template hierarchy when the tag slug contains multibyte characters.
295 *
296 * @see get_query_template()
297 *
298 * @return string Full path to tag template file.
299 */
300function get_tag_template() {
301        $tag = get_queried_object();
302
303        $templates = array();
304
305        if ( ! empty( $tag->slug ) ) {
306
307                $slug_decoded = urldecode( $tag->slug );
308                if ( $slug_decoded !== $tag->slug ) {
309                        $templates[] = "tag-{$slug_decoded}.php";
310                }
311
312                $templates[] = "tag-{$tag->slug}.php";
313                $templates[] = "tag-{$tag->term_id}.php";
314        }
315        $templates[] = 'tag.php';
316
317        return get_query_template( 'tag', $templates );
318}
319
320/**
321 * Retrieves path of custom taxonomy term template in current or parent template.
322 *
323 * The hierarchy for this template looks like:
324 *
325 * 1. taxonomy-{taxonomy_slug}-{term_slug}.php
326 * 2. taxonomy-{taxonomy_slug}.php
327 * 3. taxonomy.php
328 *
329 * An example of this is:
330 *
331 * 1. taxonomy-location-texas.php
332 * 2. taxonomy-location.php
333 * 3. taxonomy.php
334 *
335 * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
336 * and {@see '$type_template'} dynamic hooks, where `$type` is 'taxonomy'.
337 *
338 * @since 2.5.0
339 * @since 4.7.0 The decoded form of `taxonomy-{taxonomy_slug}-{term_slug}.php` was added to the top of the
340 *              template hierarchy when the term slug contains multibyte characters.
341 *
342 * @see get_query_template()
343 *
344 * @return string Full path to custom taxonomy term template file.
345 */
346function get_taxonomy_template() {
347        $term = get_queried_object();
348
349        $templates = array();
350
351        if ( ! empty( $term->slug ) ) {
352                $taxonomy = $term->taxonomy;
353
354                $slug_decoded = urldecode( $term->slug );
355                if ( $slug_decoded !== $term->slug ) {
356                        $templates[] = "taxonomy-$taxonomy-{$slug_decoded}.php";
357                }
358
359                $templates[] = "taxonomy-$taxonomy-{$term->slug}.php";
360                $templates[] = "taxonomy-$taxonomy.php";
361        }
362        $templates[] = 'taxonomy.php';
363
364        return get_query_template( 'taxonomy', $templates );
365}
366
367/**
368 * Retrieves path of date template in current or parent template.
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 'date'.
372 *
373 * @since 1.5.0
374 *
375 * @see get_query_template()
376 *
377 * @return string Full path to date template file.
378 */
379function get_date_template() {
380        return get_query_template( 'date' );
381}
382
383/**
384 * Retrieves path of home template in current or parent template.
385 *
386 * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
387 * and {@see '$type_template'} dynamic hooks, where `$type` is 'home'.
388 *
389 * @since 1.5.0
390 *
391 * @see get_query_template()
392 *
393 * @return string Full path to home template file.
394 */
395function get_home_template() {
396        $templates = array( 'home.php', 'index.php' );
397
398        return get_query_template( 'home', $templates );
399}
400
401/**
402 * Retrieves path of front page template in current or parent template.
403 *
404 * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
405 * and {@see '$type_template'} dynamic hooks, where `$type` is 'frontpage'.
406 *
407 * @since 3.0.0
408 *
409 * @see get_query_template()
410 *
411 * @return string Full path to front page template file.
412 */
413function get_front_page_template() {
414        $templates = array( 'front-page.php' );
415
416        return get_query_template( 'frontpage', $templates );
417}
418
419/**
420 * Retrieves path of Privacy Policy page template in current or parent template.
421 *
422 * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
423 * and {@see '$type_template'} dynamic hooks, where `$type` is 'privacypolicy'.
424 *
425 * @since 5.2.0
426 *
427 * @see get_query_template()
428 *
429 * @return string Full path to privacy policy template file.
430 */
431function get_privacy_policy_template() {
432        $templates = array( 'privacy-policy.php' );
433
434        return get_query_template( 'privacypolicy', $templates );
435}
436
437/**
438 * Retrieves path of page template in current or parent template.
439 *
440 * Note: For block themes, use locate_block_template() function instead.
441 *
442 * The hierarchy for this template looks like:
443 *
444 * 1. {Page Template}.php
445 * 2. page-{page_name}.php
446 * 3. page-{id}.php
447 * 4. page.php
448 *
449 * An example of this is:
450 *
451 * 1. page-templates/full-width.php
452 * 2. page-about.php
453 * 3. page-4.php
454 * 4. page.php
455 *
456 * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
457 * and {@see '$type_template'} dynamic hooks, where `$type` is 'page'.
458 *
459 * @since 1.5.0
460 * @since 4.7.0 The decoded form of `page-{page_name}.php` was added to the top of the
461 *              template hierarchy when the page name contains multibyte characters.
462 *
463 * @see get_query_template()
464 *
465 * @return string Full path to page template file.
466 */
467function get_page_template() {
468        $id       = get_queried_object_id();
469        $template = get_page_template_slug();
470        $pagename = get_query_var( 'pagename' );
471
472        if ( ! $pagename && $id ) {
473                /*
474                 * If a static page is set as the front page, $pagename will not be set.
475                 * Retrieve it from the queried object.
476                 */
477                $post = get_queried_object();
478                if ( $post ) {
479                        $pagename = $post->post_name;
480                }
481        }
482
483        $templates = array();
484        if ( $template && 0 === validate_file( $template ) ) {
485                $templates[] = $template;
486        }
487        if ( $pagename ) {
488                $pagename_decoded = urldecode( $pagename );
489                if ( $pagename_decoded !== $pagename ) {
490                        $templates[] = "page-{$pagename_decoded}.php";
491                }
492                $templates[] = "page-{$pagename}.php";
493        }
494        if ( $id ) {
495                $templates[] = "page-{$id}.php";
496        }
497        $templates[] = 'page.php';
498
499        return get_query_template( 'page', $templates );
500}
501
502/**
503 * Retrieves path of search template in current or parent template.
504 *
505 * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
506 * and {@see '$type_template'} dynamic hooks, where `$type` is 'search'.
507 *
508 * @since 1.5.0
509 *
510 * @see get_query_template()
511 *
512 * @return string Full path to search template file.
513 */
514function get_search_template() {
515        return get_query_template( 'search' );
516}
517
518/**
519 * Retrieves path of single template in current or parent template. Applies to single Posts,
520 * single Attachments, and single custom post types.
521 *
522 * The hierarchy for this template looks like:
523 *
524 * 1. {Post Type Template}.php
525 * 2. single-{post_type}-{post_name}.php
526 * 3. single-{post_type}.php
527 * 4. single.php
528 *
529 * An example of this is:
530 *
531 * 1. templates/full-width.php
532 * 2. single-post-hello-world.php
533 * 3. single-post.php
534 * 4. single.php
535 *
536 * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
537 * and {@see '$type_template'} dynamic hooks, where `$type` is 'single'.
538 *
539 * @since 1.5.0
540 * @since 4.4.0 `single-{post_type}-{post_name}.php` was added to the top of the template hierarchy.
541 * @since 4.7.0 The decoded form of `single-{post_type}-{post_name}.php` was added to the top of the
542 *              template hierarchy when the post name contains multibyte characters.
543 * @since 4.7.0 `{Post Type Template}.php` was added to the top of the template hierarchy.
544 *
545 * @see get_query_template()
546 *
547 * @return string Full path to single template file.
548 */
549function get_single_template() {
550        $object = get_queried_object();
551
552        $templates = array();
553
554        if ( ! empty( $object->post_type ) ) {
555                $template = get_page_template_slug( $object );
556                if ( $template && 0 === validate_file( $template ) ) {
557                        $templates[] = $template;
558                }
559
560                $name_decoded = urldecode( $object->post_name );
561                if ( $name_decoded !== $object->post_name ) {
562                        $templates[] = "single-{$object->post_type}-{$name_decoded}.php";
563                }
564
565                $templates[] = "single-{$object->post_type}-{$object->post_name}.php";
566                $templates[] = "single-{$object->post_type}.php";
567        }
568
569        $templates[] = 'single.php';
570
571        return get_query_template( 'single', $templates );
572}
573
574/**
575 * Retrieves an embed template path in the current or parent template.
576 *
577 * The hierarchy for this template looks like:
578 *
579 * 1. embed-{post_type}-{post_format}.php
580 * 2. embed-{post_type}.php
581 * 3. embed.php
582 *
583 * An example of this is:
584 *
585 * 1. embed-post-audio.php
586 * 2. embed-post.php
587 * 3. embed.php
588 *
589 * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
590 * and {@see '$type_template'} dynamic hooks, where `$type` is 'embed'.
591 *
592 * @since 4.5.0
593 *
594 * @see get_query_template()
595 *
596 * @return string Full path to embed template file.
597 */
598function get_embed_template() {
599        $object = get_queried_object();
600
601        $templates = array();
602
603        if ( ! empty( $object->post_type ) ) {
604                $post_format = get_post_format( $object );
605                if ( $post_format ) {
606                        $templates[] = "embed-{$object->post_type}-{$post_format}.php";
607                }
608                $templates[] = "embed-{$object->post_type}.php";
609        }
610
611        $templates[] = 'embed.php';
612
613        return get_query_template( 'embed', $templates );
614}
615
616/**
617 * Retrieves the path of the singular template in current or parent template.
618 *
619 * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
620 * and {@see '$type_template'} dynamic hooks, where `$type` is 'singular'.
621 *
622 * @since 4.3.0
623 *
624 * @see get_query_template()
625 *
626 * @return string Full path to singular template file
627 */
628function get_singular_template() {
629        return get_query_template( 'singular' );
630}
631
632/**
633 * Retrieves path of attachment template in current or parent template.
634 *
635 * The hierarchy for this template looks like:
636 *
637 * 1. {mime_type}-{sub_type}.php
638 * 2. {sub_type}.php
639 * 3. {mime_type}.php
640 * 4. attachment.php
641 *
642 * An example of this is:
643 *
644 * 1. image-jpeg.php
645 * 2. jpeg.php
646 * 3. image.php
647 * 4. attachment.php
648 *
649 * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
650 * and {@see '$type_template'} dynamic hooks, where `$type` is 'attachment'.
651 *
652 * @since 2.0.0
653 * @since 4.3.0 The order of the mime type logic was reversed so the hierarchy is more logical.
654 *
655 * @see get_query_template()
656 *
657 * @return string Full path to attachment template file.
658 */
659function get_attachment_template() {
660        $attachment = get_queried_object();
661
662        $templates = array();
663
664        if ( $attachment ) {
665                if ( str_contains( $attachment->post_mime_type, '/' ) ) {
666                        list( $type, $subtype ) = explode( '/', $attachment->post_mime_type );
667                } else {
668                        list( $type, $subtype ) = array( $attachment->post_mime_type, '' );
669                }
670
671                if ( ! empty( $subtype ) ) {
672                        $templates[] = "{$type}-{$subtype}.php";
673                        $templates[] = "{$subtype}.php";
674                }
675                $templates[] = "{$type}.php";
676        }
677        $templates[] = 'attachment.php';
678
679        return get_query_template( 'attachment', $templates );
680}
681
682/**
683 * Set up the globals used for template loading.
684 *
685 * @since 6.5.0
686 *
687 * @global string $wp_stylesheet_path Path to current theme's stylesheet directory.
688 * @global string $wp_template_path   Path to current theme's template directory.
689 */
690function wp_set_template_globals() {
691        global $wp_stylesheet_path, $wp_template_path;
692
693        $wp_stylesheet_path = get_stylesheet_directory();
694        $wp_template_path   = get_template_directory();
695}
696
697/**
698 * Retrieves the name of the highest priority template file that exists.
699 *
700 * Searches in the stylesheet directory before the template directory and
701 * wp-includes/theme-compat so that themes which inherit from a parent theme
702 * can just overload one file.
703 *
704 * @since 2.7.0
705 * @since 5.5.0 The `$args` parameter was added.
706 *
707 * @global string $wp_stylesheet_path Path to current theme's stylesheet directory.
708 * @global string $wp_template_path   Path to current theme's template directory.
709 *
710 * @param string|array $template_names Template file(s) to search for, in order.
711 * @param bool         $load           If true the template file will be loaded if it is found.
712 * @param bool         $load_once      Whether to require_once or require. Has no effect if `$load` is false.
713 *                                     Default true.
714 * @param array        $args           Optional. Additional arguments passed to the template.
715 *                                     Default empty array.
716 * @return string The template filename if one is located.
717 */
718function locate_template( $template_names, $load = false, $load_once = true, $args = array() ) {
719        global $wp_stylesheet_path, $wp_template_path;
720
721        if ( ! isset( $wp_stylesheet_path ) || ! isset( $wp_template_path ) ) {
722                wp_set_template_globals();
723        }
724
725        $is_child_theme = is_child_theme();
726
727        $located = '';
728        foreach ( (array) $template_names as $template_name ) {
729                if ( ! $template_name ) {
730                        continue;
731                }
732                if ( file_exists( $wp_stylesheet_path . '/' . $template_name ) ) {
733                        $located = $wp_stylesheet_path . '/' . $template_name;
734                        break;
735                } elseif ( $is_child_theme && file_exists( $wp_template_path . '/' . $template_name ) ) {
736                        $located = $wp_template_path . '/' . $template_name;
737                        break;
738                } elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
739                        $located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
740                        break;
741                }
742        }
743
744        if ( $load && '' !== $located ) {
745                load_template( $located, $load_once, $args );
746        }
747
748        return $located;
749}
750
751/**
752 * Requires the template file with WordPress environment.
753 *
754 * The globals are set up for the template file to ensure that the WordPress
755 * environment is available from within the function. The query variables are
756 * also available.
757 *
758 * @since 1.5.0
759 * @since 5.5.0 The `$args` parameter was added.
760 *
761 * @global array      $posts
762 * @global WP_Post    $post          Global post object.
763 * @global bool       $wp_did_header
764 * @global WP_Query   $wp_query      WordPress Query object.
765 * @global WP_Rewrite $wp_rewrite    WordPress rewrite component.
766 * @global wpdb       $wpdb          WordPress database abstraction object.
767 * @global string     $wp_version
768 * @global WP         $wp            Current WordPress environment instance.
769 * @global int        $id
770 * @global WP_Comment $comment       Global comment object.
771 * @global int        $user_ID
772 *
773 * @param string $_template_file Path to template file.
774 * @param bool   $load_once      Whether to require_once or require. Default true.
775 * @param array  $args           Optional. Additional arguments passed to the template.
776 *                               Default empty array.
777 */
778function load_template( $_template_file, $load_once = true, $args = array() ) {
779        global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
780
781        if ( is_array( $wp_query->query_vars ) ) {
782                /*
783                 * This use of extract() cannot be removed. There are many possible ways that
784                 * templates could depend on variables that it creates existing, and no way to
785                 * detect and deprecate it.
786                 *
787                 * Passing the EXTR_SKIP flag is the safest option, ensuring globals and
788                 * function variables cannot be overwritten.
789                 */
790                // phpcs:ignore WordPress.PHP.DontExtract.extract_extract
791                extract( $wp_query->query_vars, EXTR_SKIP );
792        }
793
794        if ( isset( $s ) ) {
795                $s = esc_attr( $s );
796        }
797
798        /**
799         * Fires before a template file is loaded.
800         *
801         * @since 6.1.0
802         *
803         * @param string $_template_file The full path to the template file.
804         * @param bool   $load_once      Whether to require_once or require.
805         * @param array  $args           Additional arguments passed to the template.
806         */
807        do_action( 'wp_before_load_template', $_template_file, $load_once, $args );
808
809        if ( $load_once ) {
810                require_once $_template_file;
811        } else {
812                require $_template_file;
813        }
814
815        /**
816         * Fires after a template file is loaded.
817         *
818         * @since 6.1.0
819         *
820         * @param string $_template_file The full path to the template file.
821         * @param bool   $load_once      Whether to require_once or require.
822         * @param array  $args           Additional arguments passed to the template.
823         */
824        do_action( 'wp_after_load_template', $_template_file, $load_once, $args );
825}
Note: See TracBrowser for help on using the repository browser.