1<?php
2/**
3 * Loads the correct template based on the visitor's URL
4 *
5 * @package WordPress
6 */
7if ( wp_using_themes() ) {
8 /**
9 * Fires before determining which template to load.
10 *
11 * This action hook executes just before WordPress determines which template page to load.
12 * It is a good hook to use if you need to do a redirect with full knowledge of the content
13 * that has been queried.
14 *
15 * Note: Loading a different template is not a good use of this hook. If you include another template
16 * and then use `exit()` or `die()`, no subsequent `template_redirect` hooks will be run, which could
17 * break the site’s functionality. Instead, use the {@see 'template_include'} filter hook to return
18 * the path to the new template you want to use. This will allow an alternative template to be used
19 * without interfering with the WordPress loading process.
20 *
21 * @since 1.5.0
22 */
23 do_action( 'template_redirect' );
24}
25
26/**
27 * Filters whether to allow 'HEAD' requests to generate content.
28 *
29 * Provides a significant performance bump by exiting before the page
30 * content loads for 'HEAD' requests. See #14348.
31 *
32 * @since 3.5.0
33 *
34 * @param bool $exit Whether to exit without generating any content for 'HEAD' requests. Default true.
35 */
36if ( 'HEAD' === $_SERVER['REQUEST_METHOD'] && apply_filters( 'exit_on_http_head', true ) ) {
37 exit;
38}
39
40// Process feeds and trackbacks even if not using themes.
41if ( is_robots() ) {
42 /**
43 * Fired when the template loader determines a robots.txt request.
44 *
45 * @since 2.1.0
46 */
47 do_action( 'do_robots' );
48 return;
49} elseif ( is_favicon() ) {
50 /**
51 * Fired when the template loader determines a favicon.ico request.
52 *
53 * @since 5.4.0
54 */
55 do_action( 'do_favicon' );
56 return;
57} elseif ( is_feed() ) {
58 do_feed();
59 return;
60} elseif ( is_trackback() ) {
61 require ABSPATH . 'wp-trackback.php';
62 return;
63}
64
65if ( wp_using_themes() ) {
66
67 $tag_templates = array(
68 'is_embed' => 'get_embed_template',
69 'is_404' => 'get_404_template',
70 'is_search' => 'get_search_template',
71 'is_front_page' => 'get_front_page_template',
72 'is_home' => 'get_home_template',
73 'is_privacy_policy' => 'get_privacy_policy_template',
74 'is_post_type_archive' => 'get_post_type_archive_template',
75 'is_tax' => 'get_taxonomy_template',
76 'is_attachment' => 'get_attachment_template',
77 'is_single' => 'get_single_template',
78 'is_page' => 'get_page_template',
79 'is_singular' => 'get_singular_template',
80 'is_category' => 'get_category_template',
81 'is_tag' => 'get_tag_template',
82 'is_author' => 'get_author_template',
83 'is_date' => 'get_date_template',
84 'is_archive' => 'get_archive_template',
85 );
86 $template = false;
87
88 // Loop through each of the template conditionals, and find the appropriate template file.
89 foreach ( $tag_templates as $tag => $template_getter ) {
90 if ( call_user_func( $tag ) ) {
91 $template = call_user_func( $template_getter );
92 }
93
94 if ( $template ) {
95 if ( 'is_attachment' === $tag ) {
96 remove_filter( 'the_content', 'prepend_attachment' );
97 }
98
99 break;
100 }
101 }
102
103 if ( ! $template ) {
104 $template = get_index_template();
105 }
106
107 /**
108 * Filters the path of the current template before including it.
109 *
110 * @since 3.0.0
111 *
112 * @param string $template The path of the template to include.
113 */
114 $template = apply_filters( 'template_include', $template );
115 $is_stringy = is_string( $template ) || ( is_object( $template ) && method_exists( $template, '__toString' ) );
116 $template = $is_stringy ? realpath( (string) $template ) : null;
117 if (
118 is_string( $template ) &&
119 ( str_ends_with( $template, '.php' ) || str_ends_with( $template, '.html' ) ) &&
120 is_file( $template ) &&
121 is_readable( $template )
122 ) {
123 /**
124 * Fires immediately before including the template.
125 *
126 * @since 6.9.0
127 *
128 * @param string $template The path of the template about to be included.
129 */
130 do_action( 'wp_before_include_template', $template );
131
132 include $template;
133 } elseif ( current_user_can( 'switch_themes' ) ) {
134 $theme = wp_get_theme();
135 if ( $theme->errors() ) {
136 wp_die( $theme->errors() );
137 }
138 }
139 return;
140}
141