template-loader.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. * Filters whether to allow 'HEAD' requests to generate content.
  17. *
  18. * Provides a significant performance bump by exiting before the page
  19. * content loads for 'HEAD' requests. See #14348.
  20. *
  21. * @since 3.5.0
  22. *
  23. * @param bool $exit Whether to exit without generating any content for 'HEAD' requests. Default true.
  24. */
  25. if ( 'HEAD' === $_SERVER['REQUEST_METHOD'] && apply_filters( 'exit_on_http_head', true ) ) {
  26. exit;
  27. }
  28. // Process feeds and trackbacks even if not using themes.
  29. if ( is_robots() ) {
  30. /**
  31. * Fired when the template loader determines a robots.txt request.
  32. *
  33. * @since 2.1.0
  34. */
  35. do_action( 'do_robots' );
  36. return;
  37. } elseif ( is_favicon() ) {
  38. /**
  39. * Fired when the template loader determines a favicon.ico request.
  40. *
  41. * @since 5.4.0
  42. */
  43. do_action( 'do_favicon' );
  44. return;
  45. } elseif ( is_feed() ) {
  46. do_feed();
  47. return;
  48. } elseif ( is_trackback() ) {
  49. require ABSPATH . 'wp-trackback.php';
  50. return;
  51. }
  52. if ( wp_using_themes() ) {
  53. $tag_templates = array(
  54. 'is_embed' => 'get_embed_template',
  55. 'is_404' => 'get_404_template',
  56. 'is_search' => 'get_search_template',
  57. 'is_front_page' => 'get_front_page_template',
  58. 'is_home' => 'get_home_template',
  59. 'is_privacy_policy' => 'get_privacy_policy_template',
  60. 'is_post_type_archive' => 'get_post_type_archive_template',
  61. 'is_tax' => 'get_taxonomy_template',
  62. 'is_attachment' => 'get_attachment_template',
  63. 'is_single' => 'get_single_template',
  64. 'is_page' => 'get_page_template',
  65. 'is_singular' => 'get_singular_template',
  66. 'is_category' => 'get_category_template',
  67. 'is_tag' => 'get_tag_template',
  68. 'is_author' => 'get_author_template',
  69. 'is_date' => 'get_date_template',
  70. 'is_archive' => 'get_archive_template',
  71. );
  72. $template = false;
  73. // Loop through each of the template conditionals, and find the appropriate template file.
  74. foreach ( $tag_templates as $tag => $template_getter ) {
  75. if ( call_user_func( $tag ) ) {
  76. $template = call_user_func( $template_getter );
  77. }
  78. if ( $template ) {
  79. if ( 'is_attachment' === $tag ) {
  80. remove_filter( 'the_content', 'prepend_attachment' );
  81. }
  82. break;
  83. }
  84. }
  85. if ( ! $template ) {
  86. $template = get_index_template();
  87. }
  88. /**
  89. * Filters the path of the current template before including it.
  90. *
  91. * @since 3.0.0
  92. *
  93. * @param string $template The path of the template to include.
  94. */
  95. $template = apply_filters( 'template_include', $template );
  96. if ( $template ) {
  97. include $template;
  98. } elseif ( current_user_can( 'switch_themes' ) ) {
  99. $theme = wp_get_theme();
  100. if ( $theme->errors() ) {
  101. wp_die( $theme->errors() );
  102. }
  103. }
  104. return;
  105. }