Themes: Allow template loading functions to pass additional arguments to the template via the $args parameter.

This affects:
* `get_header()`
* `get_footer()`
* `get_sidebar()`
* `get_template_part()`
* `locate_template()`
* `load_template()`

Note: `get_search_form()` already passes additional arguments to the template as of [44956].

Props enrico.sorcinelli, sc0ttkclark, scribu, nacin, wonderboymusic, GeertDD, beatpanda, amaschas, mintindeed, ysalame, caiocrcosta, bigdawggi, julianm, eddiemoya, shawnz, sayedwp, shamai, mboynes, mihai2u, guidobras, Mte90, apedog, stuffradio, overclokk, johnbillion, joyously, afercia, audrasjb, justlevine, SergeyBiryukov.
See #21676.

git-svn-id: https://develop.svn.wordpress.org/trunk@48370 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2020-07-07 11:00:21 +00:00
parent 3eb2bd9f5b
commit 4e78b0acd9
4 changed files with 74 additions and 27 deletions

View File

@@ -644,13 +644,17 @@ function get_attachment_template() {
* so that themes which inherit from a parent theme can just overload one file.
*
* @since 2.7.0
* @since 5.5.0 The `$args` parameter was added.
*
* @param string|array $template_names Template file(s) to search for, in order.
* @param bool $load If true the template file will be loaded if it is found.
* @param bool $require_once Whether to require_once or require. Default true. Has no effect if $load is false.
* @param bool $require_once Whether to require_once or require. Has no effect if `$load` is false.
* Default true.
* @param array $args Optional. Additional arguments passed to the template.
* Default empty array.
* @return string The template filename if one is located.
*/
function locate_template( $template_names, $load = false, $require_once = true ) {
function locate_template( $template_names, $load = false, $require_once = true, $args = array() ) {
$located = '';
foreach ( (array) $template_names as $template_name ) {
if ( ! $template_name ) {
@@ -669,7 +673,7 @@ function locate_template( $template_names, $load = false, $require_once = true )
}
if ( $load && '' !== $located ) {
load_template( $located, $require_once );
load_template( $located, $require_once, $args );
}
return $located;
@@ -683,6 +687,7 @@ function locate_template( $template_names, $load = false, $require_once = true )
* also available.
*
* @since 1.5.0
* @since 5.5.0 The `$args` parameter was added.
*
* @global array $posts
* @global WP_Post $post Global post object.
@@ -698,8 +703,10 @@ function locate_template( $template_names, $load = false, $require_once = true )
*
* @param string $_template_file Path to template file.
* @param bool $require_once Whether to require_once or require. Default true.
* @param array $args Optional. Additional arguments passed to the template.
* Default empty array.
*/
function load_template( $_template_file, $require_once = true ) {
function load_template( $_template_file, $require_once = true, $args = array() ) {
global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
if ( is_array( $wp_query->query_vars ) ) {