G

Untitled

public
Guest Mar 16, 2026 Never 223
Clone
Plaintext paste1.txt 86 lines (83 loc) | 2.72 KB
This code snippet is from a WordPress theme template file that determines which template to load based on the visitor's URL and request method. Here is an analysis of the code: 1. The code first checks if WordPress is using themes with `wp_using_themes()` function and fires the `template_redirect` action. 2. It then checks if the current request method is 'HEAD' and exits early if it is, based on the filter `exit_on_http_head`. 3. Next, it processes different types of requests such as robots.txt, feeds, and trackbacks, by calling specific functions or including necessary files. 4. If themes are being used, it goes through a series of conditional checks to determine the appropriate template based on the type of content being requested (e.g., 404 page, search results, front page, etc.). 5. It includes the selected template file after filtering it through `template_include` hook. 6. Finally, if no individual template is matched, it includes the index template file. Overall, this code snippet controls the template loading process in a WordPress theme based on the visitor's URL and request method, ensuring that the correct template is displayed for different types of content.
1
<?php
2
/**
3
* Loads the correct template based on the visitor's url
4
*
5
* @package WordPress
6
*/
7
if ( wp_using_themes() ) {
8
/**
9
* Fires before determining which template to load.
10
*
11
* @since 1.5.0
12
*/
13
do_action( 'template_redirect' );
14
}
15
16
/**
17
* Filters whether to allow 'HEAD' requests to generate content.
18
*
19
* Provides a significant performance bump by exiting before the page
20
* content loads for 'HEAD' requests. See #14348.
21
*
22
* @since 3.5.0
23
*
24
* @param bool $exit Whether to exit without generating any content for 'HEAD' requests. Default true.
25
*/
26
if ( 'HEAD' === $_SERVER['REQUEST_METHOD'] && apply_filters( 'exit_on_http_head', true ) ) {
27
exit();
28
}
29
30
// Process feeds and trackbacks even if not using themes.
31
if ( is_robots() ) :
32
/**
33
* Fired when the template loader determines a robots.txt request.
34
*
35
* @since 2.1.0
36
*/
37
do_action( 'do_robots' );
38
return;
39
elseif ( is_feed() ) :
40
do_feed();
41
return;
42
elseif ( is_trackback() ) :
43
include( ABSPATH . 'wp-trackback.php' );
44
return;
45
endif;
46
47
if ( wp_using_themes() ) :
48
$template = false;
49
if ( is_embed() && $template = get_embed_template() ) :
50
elseif ( is_404() && $template = get_404_template() ) :
51
elseif ( is_search() && $template = get_search_template() ) :
52
elseif ( is_front_page() && $template = get_front_page_template() ) :
53
elseif ( is_home() && $template = get_home_template() ) :
54
elseif ( is_privacy_policy() && $template = get_privacy_policy_template() ) :
55
elseif ( is_post_type_archive() && $template = get_post_type_archive_template() ) :
56
elseif ( is_tax() && $template = get_taxonomy_template() ) :
57
elseif ( is_attachment() && $template = get_attachment_template() ) :
58
remove_filter( 'the_content', 'prepend_attachment' );
59
elseif ( is_single() && $template = get_single_template() ) :
60
elseif ( is_page() && $template = get_page_template() ) :
61
elseif ( is_singular() && $template = get_singular_template() ) :
62
elseif ( is_category() && $template = get_category_template() ) :
63
elseif ( is_tag() && $template = get_tag_template() ) :
64
elseif ( is_author() && $template = get_author_template() ) :
65
elseif ( is_date() && $template = get_date_template() ) :
66
elseif ( is_archive() && $template = get_archive_template() ) :
67
else :
68
$template = get_index_template();
69
endif;
70
/**
71
* Filters the path of the current template before including it.
72
*
73
* @since 3.0.0
74
*
75
* @param string $template The path of the template to include.
76
*/
77
if ( $template = apply_filters( 'template_include', $template ) ) {
78
include( $template );
79
} elseif ( current_user_can( 'switch_themes' ) ) {
80
$theme = wp_get_theme();
81
if ( $theme->errors() ) {
82
wp_die( $theme->errors() );
83
}
84
}
85
return;
86
endif;