wp/wp-includes/template.php
changeset 21 48c4eec2b7e6
parent 18 be944660c56a
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
     5  * @package WordPress
     5  * @package WordPress
     6  * @subpackage Template
     6  * @subpackage Template
     7  */
     7  */
     8 
     8 
     9 /**
     9 /**
    10  * Retrieve path to a template
    10  * Retrieves path to a template.
    11  *
    11  *
    12  * Used to quickly retrieve the path of a template without including the file
    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
    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
    14  * the use of locate_template(). Allows for more generic template location
    15  * without the use of the other get_*_template() functions.
    15  * without the use of the other get_*_template() functions.
   102 	 */
   102 	 */
   103 	return apply_filters( "{$type}_template", $template, $type, $templates );
   103 	return apply_filters( "{$type}_template", $template, $type, $templates );
   104 }
   104 }
   105 
   105 
   106 /**
   106 /**
   107  * Retrieve path of index template in current or parent template.
   107  * Retrieves path of index template in current or parent template.
   108  *
   108  *
   109  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
   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'.
   110  * and {@see '$type_template'} dynamic hooks, where `$type` is 'index'.
   111  *
   111  *
   112  * @since 3.0.0
   112  * @since 3.0.0
   118 function get_index_template() {
   118 function get_index_template() {
   119 	return get_query_template( 'index' );
   119 	return get_query_template( 'index' );
   120 }
   120 }
   121 
   121 
   122 /**
   122 /**
   123  * Retrieve path of 404 template in current or parent template.
   123  * Retrieves path of 404 template in current or parent template.
   124  *
   124  *
   125  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
   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'.
   126  * and {@see '$type_template'} dynamic hooks, where `$type` is '404'.
   127  *
   127  *
   128  * @since 1.5.0
   128  * @since 1.5.0
   134 function get_404_template() {
   134 function get_404_template() {
   135 	return get_query_template( '404' );
   135 	return get_query_template( '404' );
   136 }
   136 }
   137 
   137 
   138 /**
   138 /**
   139  * Retrieve path of archive template in current or parent template.
   139  * Retrieves path of archive template in current or parent template.
   140  *
   140  *
   141  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
   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'.
   142  * and {@see '$type_template'} dynamic hooks, where `$type` is 'archive'.
   143  *
   143  *
   144  * @since 1.5.0
   144  * @since 1.5.0
   150 function get_archive_template() {
   150 function get_archive_template() {
   151 	$post_types = array_filter( (array) get_query_var( 'post_type' ) );
   151 	$post_types = array_filter( (array) get_query_var( 'post_type' ) );
   152 
   152 
   153 	$templates = array();
   153 	$templates = array();
   154 
   154 
   155 	if ( count( $post_types ) == 1 ) {
   155 	if ( count( $post_types ) === 1 ) {
   156 		$post_type   = reset( $post_types );
   156 		$post_type   = reset( $post_types );
   157 		$templates[] = "archive-{$post_type}.php";
   157 		$templates[] = "archive-{$post_type}.php";
   158 	}
   158 	}
   159 	$templates[] = 'archive.php';
   159 	$templates[] = 'archive.php';
   160 
   160 
   161 	return get_query_template( 'archive', $templates );
   161 	return get_query_template( 'archive', $templates );
   162 }
   162 }
   163 
   163 
   164 /**
   164 /**
   165  * Retrieve path of post type archive template in current or parent template.
   165  * Retrieves path of post type archive template in current or parent template.
   166  *
   166  *
   167  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
   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'.
   168  * and {@see '$type_template'} dynamic hooks, where `$type` is 'archive'.
   169  *
   169  *
   170  * @since 3.7.0
   170  * @since 3.7.0
   186 
   186 
   187 	return get_archive_template();
   187 	return get_archive_template();
   188 }
   188 }
   189 
   189 
   190 /**
   190 /**
   191  * Retrieve path of author template in current or parent template.
   191  * Retrieves path of author template in current or parent template.
   192  *
   192  *
   193  * The hierarchy for this template looks like:
   193  * The hierarchy for this template looks like:
   194  *
   194  *
   195  * 1. author-{nicename}.php
   195  * 1. author-{nicename}.php
   196  * 2. author-{id}.php
   196  * 2. author-{id}.php
   224 
   224 
   225 	return get_query_template( 'author', $templates );
   225 	return get_query_template( 'author', $templates );
   226 }
   226 }
   227 
   227 
   228 /**
   228 /**
   229  * Retrieve path of category template in current or parent template.
   229  * Retrieves path of category template in current or parent template.
   230  *
   230  *
   231  * The hierarchy for this template looks like:
   231  * The hierarchy for this template looks like:
   232  *
   232  *
   233  * 1. category-{slug}.php
   233  * 1. category-{slug}.php
   234  * 2. category-{id}.php
   234  * 2. category-{id}.php
   270 
   270 
   271 	return get_query_template( 'category', $templates );
   271 	return get_query_template( 'category', $templates );
   272 }
   272 }
   273 
   273 
   274 /**
   274 /**
   275  * Retrieve path of tag template in current or parent template.
   275  * Retrieves path of tag template in current or parent template.
   276  *
   276  *
   277  * The hierarchy for this template looks like:
   277  * The hierarchy for this template looks like:
   278  *
   278  *
   279  * 1. tag-{slug}.php
   279  * 1. tag-{slug}.php
   280  * 2. tag-{id}.php
   280  * 2. tag-{id}.php
   316 
   316 
   317 	return get_query_template( 'tag', $templates );
   317 	return get_query_template( 'tag', $templates );
   318 }
   318 }
   319 
   319 
   320 /**
   320 /**
   321  * Retrieve path of custom taxonomy term template in current or parent template.
   321  * Retrieves path of custom taxonomy term template in current or parent template.
   322  *
   322  *
   323  * The hierarchy for this template looks like:
   323  * The hierarchy for this template looks like:
   324  *
   324  *
   325  * 1. taxonomy-{taxonomy_slug}-{term_slug}.php
   325  * 1. taxonomy-{taxonomy_slug}-{term_slug}.php
   326  * 2. taxonomy-{taxonomy_slug}.php
   326  * 2. taxonomy-{taxonomy_slug}.php
   363 
   363 
   364 	return get_query_template( 'taxonomy', $templates );
   364 	return get_query_template( 'taxonomy', $templates );
   365 }
   365 }
   366 
   366 
   367 /**
   367 /**
   368  * Retrieve path of date template in current or parent template.
   368  * Retrieves path of date template in current or parent template.
   369  *
   369  *
   370  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
   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'.
   371  * and {@see '$type_template'} dynamic hooks, where `$type` is 'date'.
   372  *
   372  *
   373  * @since 1.5.0
   373  * @since 1.5.0
   379 function get_date_template() {
   379 function get_date_template() {
   380 	return get_query_template( 'date' );
   380 	return get_query_template( 'date' );
   381 }
   381 }
   382 
   382 
   383 /**
   383 /**
   384  * Retrieve path of home template in current or parent template.
   384  * Retrieves path of home template in current or parent template.
   385  *
   385  *
   386  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
   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'.
   387  * and {@see '$type_template'} dynamic hooks, where `$type` is 'home'.
   388  *
   388  *
   389  * @since 1.5.0
   389  * @since 1.5.0
   397 
   397 
   398 	return get_query_template( 'home', $templates );
   398 	return get_query_template( 'home', $templates );
   399 }
   399 }
   400 
   400 
   401 /**
   401 /**
   402  * Retrieve path of front page template in current or parent template.
   402  * Retrieves path of front page template in current or parent template.
   403  *
   403  *
   404  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
   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'.
   405  * and {@see '$type_template'} dynamic hooks, where `$type` is 'frontpage'.
   406  *
   406  *
   407  * @since 3.0.0
   407  * @since 3.0.0
   415 
   415 
   416 	return get_query_template( 'frontpage', $templates );
   416 	return get_query_template( 'frontpage', $templates );
   417 }
   417 }
   418 
   418 
   419 /**
   419 /**
   420  * Retrieve path of Privacy Policy page template in current or parent template.
   420  * Retrieves path of Privacy Policy page template in current or parent template.
   421  *
   421  *
   422  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
   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'.
   423  * and {@see '$type_template'} dynamic hooks, where `$type` is 'privacypolicy'.
   424  *
   424  *
   425  * @since 5.2.0
   425  * @since 5.2.0
   433 
   433 
   434 	return get_query_template( 'privacypolicy', $templates );
   434 	return get_query_template( 'privacypolicy', $templates );
   435 }
   435 }
   436 
   436 
   437 /**
   437 /**
   438  * Retrieve path of page template in current or parent template.
   438  * Retrieves path of page template in current or parent template.
       
   439  *
       
   440  * Note: For block themes, use locate_block_template() function instead.
   439  *
   441  *
   440  * The hierarchy for this template looks like:
   442  * The hierarchy for this template looks like:
   441  *
   443  *
   442  * 1. {Page Template}.php
   444  * 1. {Page Template}.php
   443  * 2. page-{page_name}.php
   445  * 2. page-{page_name}.php
   466 	$id       = get_queried_object_id();
   468 	$id       = get_queried_object_id();
   467 	$template = get_page_template_slug();
   469 	$template = get_page_template_slug();
   468 	$pagename = get_query_var( 'pagename' );
   470 	$pagename = get_query_var( 'pagename' );
   469 
   471 
   470 	if ( ! $pagename && $id ) {
   472 	if ( ! $pagename && $id ) {
   471 		// If a static page is set as the front page, $pagename will not be set.
   473 		/*
   472 		// Retrieve it from the queried object.
   474 		 * If a static page is set as the front page, $pagename will not be set.
       
   475 		 * Retrieve it from the queried object.
       
   476 		 */
   473 		$post = get_queried_object();
   477 		$post = get_queried_object();
   474 		if ( $post ) {
   478 		if ( $post ) {
   475 			$pagename = $post->post_name;
   479 			$pagename = $post->post_name;
   476 		}
   480 		}
   477 	}
   481 	}
   494 
   498 
   495 	return get_query_template( 'page', $templates );
   499 	return get_query_template( 'page', $templates );
   496 }
   500 }
   497 
   501 
   498 /**
   502 /**
   499  * Retrieve path of search template in current or parent template.
   503  * Retrieves path of search template in current or parent template.
   500  *
   504  *
   501  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
   505  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
   502  * and {@see '$type_template'} dynamic hooks, where `$type` is 'search'.
   506  * and {@see '$type_template'} dynamic hooks, where `$type` is 'search'.
   503  *
   507  *
   504  * @since 1.5.0
   508  * @since 1.5.0
   510 function get_search_template() {
   514 function get_search_template() {
   511 	return get_query_template( 'search' );
   515 	return get_query_template( 'search' );
   512 }
   516 }
   513 
   517 
   514 /**
   518 /**
   515  * Retrieve path of single template in current or parent template. Applies to single Posts,
   519  * Retrieves path of single template in current or parent template. Applies to single Posts,
   516  * single Attachments, and single custom post types.
   520  * single Attachments, and single custom post types.
   517  *
   521  *
   518  * The hierarchy for this template looks like:
   522  * The hierarchy for this template looks like:
   519  *
   523  *
   520  * 1. {Post Type Template}.php
   524  * 1. {Post Type Template}.php
   624 function get_singular_template() {
   628 function get_singular_template() {
   625 	return get_query_template( 'singular' );
   629 	return get_query_template( 'singular' );
   626 }
   630 }
   627 
   631 
   628 /**
   632 /**
   629  * Retrieve path of attachment template in current or parent template.
   633  * Retrieves path of attachment template in current or parent template.
   630  *
   634  *
   631  * The hierarchy for this template looks like:
   635  * The hierarchy for this template looks like:
   632  *
   636  *
   633  * 1. {mime_type}-{sub_type}.php
   637  * 1. {mime_type}-{sub_type}.php
   634  * 2. {sub_type}.php
   638  * 2. {sub_type}.php
   648  * @since 2.0.0
   652  * @since 2.0.0
   649  * @since 4.3.0 The order of the mime type logic was reversed so the hierarchy is more logical.
   653  * @since 4.3.0 The order of the mime type logic was reversed so the hierarchy is more logical.
   650  *
   654  *
   651  * @see get_query_template()
   655  * @see get_query_template()
   652  *
   656  *
   653  * @global array $posts
       
   654  *
       
   655  * @return string Full path to attachment template file.
   657  * @return string Full path to attachment template file.
   656  */
   658  */
   657 function get_attachment_template() {
   659 function get_attachment_template() {
   658 	$attachment = get_queried_object();
   660 	$attachment = get_queried_object();
   659 
   661 
   660 	$templates = array();
   662 	$templates = array();
   661 
   663 
   662 	if ( $attachment ) {
   664 	if ( $attachment ) {
   663 		if ( false !== strpos( $attachment->post_mime_type, '/' ) ) {
   665 		if ( str_contains( $attachment->post_mime_type, '/' ) ) {
   664 			list( $type, $subtype ) = explode( '/', $attachment->post_mime_type );
   666 			list( $type, $subtype ) = explode( '/', $attachment->post_mime_type );
   665 		} else {
   667 		} else {
   666 			list( $type, $subtype ) = array( $attachment->post_mime_type, '' );
   668 			list( $type, $subtype ) = array( $attachment->post_mime_type, '' );
   667 		}
   669 		}
   668 
   670 
   676 
   678 
   677 	return get_query_template( 'attachment', $templates );
   679 	return get_query_template( 'attachment', $templates );
   678 }
   680 }
   679 
   681 
   680 /**
   682 /**
   681  * Retrieve the name of the highest priority template file that exists.
   683  * Set up the globals used for template loading.
   682  *
   684  *
   683  * Searches in the STYLESHEETPATH before TEMPLATEPATH and wp-includes/theme-compat
   685  * @since 6.5.0
   684  * so that themes which inherit from a parent theme can just overload one file.
   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  */
       
   690 function 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.
   685  *
   703  *
   686  * @since 2.7.0
   704  * @since 2.7.0
   687  * @since 5.5.0 The `$args` parameter was added.
   705  * @since 5.5.0 The `$args` parameter was added.
   688  *
   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  *
   689  * @param string|array $template_names Template file(s) to search for, in order.
   710  * @param string|array $template_names Template file(s) to search for, in order.
   690  * @param bool         $load           If true the template file will be loaded if it is found.
   711  * @param bool         $load           If true the template file will be loaded if it is found.
   691  * @param bool         $require_once   Whether to require_once or require. Has no effect if `$load` is false.
   712  * @param bool         $load_once      Whether to require_once or require. Has no effect if `$load` is false.
   692  *                                     Default true.
   713  *                                     Default true.
   693  * @param array        $args           Optional. Additional arguments passed to the template.
   714  * @param array        $args           Optional. Additional arguments passed to the template.
   694  *                                     Default empty array.
   715  *                                     Default empty array.
   695  * @return string The template filename if one is located.
   716  * @return string The template filename if one is located.
   696  */
   717  */
   697 function locate_template( $template_names, $load = false, $require_once = true, $args = array() ) {
   718 function 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 
   698 	$located = '';
   727 	$located = '';
   699 	foreach ( (array) $template_names as $template_name ) {
   728 	foreach ( (array) $template_names as $template_name ) {
   700 		if ( ! $template_name ) {
   729 		if ( ! $template_name ) {
   701 			continue;
   730 			continue;
   702 		}
   731 		}
   703 		if ( file_exists( STYLESHEETPATH . '/' . $template_name ) ) {
   732 		if ( file_exists( $wp_stylesheet_path . '/' . $template_name ) ) {
   704 			$located = STYLESHEETPATH . '/' . $template_name;
   733 			$located = $wp_stylesheet_path . '/' . $template_name;
   705 			break;
   734 			break;
   706 		} elseif ( file_exists( TEMPLATEPATH . '/' . $template_name ) ) {
   735 		} elseif ( $is_child_theme && file_exists( $wp_template_path . '/' . $template_name ) ) {
   707 			$located = TEMPLATEPATH . '/' . $template_name;
   736 			$located = $wp_template_path . '/' . $template_name;
   708 			break;
   737 			break;
   709 		} elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
   738 		} elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
   710 			$located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
   739 			$located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
   711 			break;
   740 			break;
   712 		}
   741 		}
   713 	}
   742 	}
   714 
   743 
   715 	if ( $load && '' !== $located ) {
   744 	if ( $load && '' !== $located ) {
   716 		load_template( $located, $require_once, $args );
   745 		load_template( $located, $load_once, $args );
   717 	}
   746 	}
   718 
   747 
   719 	return $located;
   748 	return $located;
   720 }
   749 }
   721 
   750 
   722 /**
   751 /**
   723  * Require the template file with WordPress environment.
   752  * Requires the template file with WordPress environment.
   724  *
   753  *
   725  * The globals are set up for the template file to ensure that the WordPress
   754  * The globals are set up for the template file to ensure that the WordPress
   726  * environment is available from within the function. The query variables are
   755  * environment is available from within the function. The query variables are
   727  * also available.
   756  * also available.
   728  *
   757  *
   740  * @global int        $id
   769  * @global int        $id
   741  * @global WP_Comment $comment       Global comment object.
   770  * @global WP_Comment $comment       Global comment object.
   742  * @global int        $user_ID
   771  * @global int        $user_ID
   743  *
   772  *
   744  * @param string $_template_file Path to template file.
   773  * @param string $_template_file Path to template file.
   745  * @param bool   $require_once   Whether to require_once or require. Default true.
   774  * @param bool   $load_once      Whether to require_once or require. Default true.
   746  * @param array  $args           Optional. Additional arguments passed to the template.
   775  * @param array  $args           Optional. Additional arguments passed to the template.
   747  *                               Default empty array.
   776  *                               Default empty array.
   748  */
   777  */
   749 function load_template( $_template_file, $require_once = true, $args = array() ) {
   778 function load_template( $_template_file, $load_once = true, $args = array() ) {
   750 	global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
   779 	global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
   751 
   780 
   752 	if ( is_array( $wp_query->query_vars ) ) {
   781 	if ( is_array( $wp_query->query_vars ) ) {
   753 		/*
   782 		/*
   754 		 * This use of extract() cannot be removed. There are many possible ways that
   783 		 * This use of extract() cannot be removed. There are many possible ways that
   764 
   793 
   765 	if ( isset( $s ) ) {
   794 	if ( isset( $s ) ) {
   766 		$s = esc_attr( $s );
   795 		$s = esc_attr( $s );
   767 	}
   796 	}
   768 
   797 
   769 	if ( $require_once ) {
   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 ) {
   770 		require_once $_template_file;
   810 		require_once $_template_file;
   771 	} else {
   811 	} else {
   772 		require $_template_file;
   812 		require $_template_file;
   773 	}
   813 	}
   774 }
   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 }