template-loader.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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_feed() ) {
  38. do_feed();
  39. return;
  40. } elseif ( is_trackback() ) {
  41. include( ABSPATH . 'wp-trackback.php' );
  42. return;
  43. }
  44. if ( wp_using_themes() ) {
  45. $tag_templates = array(
  46. 'is_embed' => 'get_embed_template',
  47. 'is_404' => 'get_404_template',
  48. 'is_search' => 'get_search_template',
  49. 'is_front_page' => 'get_front_page_template',
  50. 'is_home' => 'get_home_template',
  51. 'is_privacy_policy' => 'get_privacy_policy_template',
  52. 'is_post_type_archive' => 'get_post_type_archive_template',
  53. 'is_tax' => 'get_taxonomy_template',
  54. 'is_attachment' => 'get_attachment_template',
  55. 'is_single' => 'get_single_template',
  56. 'is_page' => 'get_page_template',
  57. 'is_singular' => 'get_singular_template',
  58. 'is_category' => 'get_category_template',
  59. 'is_tag' => 'get_tag_template',
  60. 'is_author' => 'get_author_template',
  61. 'is_date' => 'get_date_template',
  62. 'is_archive' => 'get_archive_template',
  63. );
  64. $template = false;
  65. // Loop through each of the template conditionals, and find the appropriate template file.
  66. foreach ( $tag_templates as $tag => $template_getter ) {
  67. if ( call_user_func( $tag ) ) {
  68. $template = call_user_func( $template_getter );
  69. }
  70. if ( $template ) {
  71. if ( 'is_attachment' === $tag ) {
  72. remove_filter( 'the_content', 'prepend_attachment' );
  73. }
  74. break;
  75. }
  76. }
  77. if ( ! $template ) {
  78. $template = get_index_template();
  79. }
  80. /**
  81. * Filters the path of the current template before including it.
  82. *
  83. * @since 3.0.0
  84. *
  85. * @param string $template The path of the template to include.
  86. */
  87. $template = apply_filters( 'template_include', $template );
  88. if ( $template ) {
  89. include( $template );
  90. } elseif ( current_user_can( 'switch_themes' ) ) {
  91. $theme = wp_get_theme();
  92. if ( $theme->errors() ) {
  93. wp_die( $theme->errors() );
  94. }
  95. }
  96. return;
  97. }