diff --git src/wp-includes/general-template.php src/wp-includes/general-template.php
index d2eccbb617..009c5c9f99 100644
|
|
|
17 | 17 | * |
18 | 18 | * @since 1.5.0 |
19 | 19 | * @since 5.5.0 A return value was added. |
| 20 | * @since 5.5.0 $wp_tmpl_args parameter added. |
20 | 21 | * |
21 | | * @param string $name The name of the specialised header. |
| 22 | * @param string $name The name of the specialised header. |
| 23 | * @param array $wp_tmpl_args Additional arguments passed to the header template. |
22 | 24 | * @return void|false Void on success, false if the template does not exist. |
23 | 25 | */ |
24 | | function get_header( $name = null ) { |
| 26 | function get_header( $name = null, $wp_tmpl_args = array() ) { |
25 | 27 | /** |
26 | 28 | * Fires before the header template file is loaded. |
27 | 29 | * |
28 | 30 | * @since 2.1.0 |
29 | 31 | * @since 2.8.0 $name parameter added. |
| 32 | * @since 5.5.0 $wp_tmpl_args parameter added. |
30 | 33 | * |
31 | | * @param string|null $name Name of the specific header file to use. null for the default header. |
| 34 | * @param string|null $name Name of the specific header file to use. null for the default header. |
| 35 | * @param array $wp_tmpl_args Additional arguments passed to the header template. |
32 | 36 | */ |
33 | | do_action( 'get_header', $name ); |
| 37 | do_action( 'get_header', $name, $wp_tmpl_args ); |
34 | 38 | |
35 | 39 | $templates = array(); |
36 | 40 | $name = (string) $name; |
… |
… |
function get_header( $name = null ) { |
40 | 44 | |
41 | 45 | $templates[] = 'header.php'; |
42 | 46 | |
43 | | if ( ! locate_template( $templates, true ) ) { |
| 47 | if ( ! locate_template( $templates, true, true, $wp_tmpl_args ) ) { |
44 | 48 | return false; |
45 | 49 | } |
46 | 50 | } |
… |
… |
function get_header( $name = null ) { |
56 | 60 | * |
57 | 61 | * @since 1.5.0 |
58 | 62 | * @since 5.5.0 A return value was added. |
| 63 | * @since 5.5.0 $wp_tmpl_args parameter added. |
59 | 64 | * |
60 | | * @param string $name The name of the specialised footer. |
| 65 | * @param string $name The name of the specialised footer. |
| 66 | * @param array $wp_tmpl_args Additional arguments passed to the footer template. |
61 | 67 | * @return void|false Void on success, false if the template does not exist. |
62 | 68 | */ |
63 | | function get_footer( $name = null ) { |
| 69 | function get_footer( $name = null, $wp_tmpl_args = array() ) { |
64 | 70 | /** |
65 | 71 | * Fires before the footer template file is loaded. |
66 | 72 | * |
67 | 73 | * @since 2.1.0 |
68 | 74 | * @since 2.8.0 $name parameter added. |
| 75 | * @since 5.5.0 $wp_tmpl_args parameter added. |
69 | 76 | * |
70 | | * @param string|null $name Name of the specific footer file to use. null for the default footer. |
| 77 | * @param string|null $name Name of the specific footer file to use. null for the default footer. |
| 78 | * @param array $wp_tmpl_args Additional arguments passed to the footer template. |
71 | 79 | */ |
72 | | do_action( 'get_footer', $name ); |
| 80 | do_action( 'get_footer', $name, $wp_tmpl_args ); |
73 | 81 | |
74 | 82 | $templates = array(); |
75 | 83 | $name = (string) $name; |
… |
… |
function get_footer( $name = null ) { |
79 | 87 | |
80 | 88 | $templates[] = 'footer.php'; |
81 | 89 | |
82 | | if ( ! locate_template( $templates, true ) ) { |
| 90 | if ( ! locate_template( $templates, true, true, $wp_tmpl_args ) ) { |
83 | 91 | return false; |
84 | 92 | } |
85 | 93 | } |
… |
… |
function get_footer( $name = null ) { |
95 | 103 | * |
96 | 104 | * @since 1.5.0 |
97 | 105 | * @since 5.5.0 A return value was added. |
| 106 | * @since 5.5.0 $wp_tmpl_args parameter added. |
98 | 107 | * |
99 | | * @param string $name The name of the specialised sidebar. |
| 108 | * @param string $name The name of the specialised sidebar. |
| 109 | * @param array $wp_tmpl_args Additional arguments passed to the sidebar template. |
100 | 110 | * @return void|false Void on success, false if the template does not exist. |
101 | 111 | */ |
102 | | function get_sidebar( $name = null ) { |
| 112 | function get_sidebar( $name = null, $wp_tmpl_args = array() ) { |
103 | 113 | /** |
104 | 114 | * Fires before the sidebar template file is loaded. |
105 | 115 | * |
106 | 116 | * @since 2.2.0 |
107 | 117 | * @since 2.8.0 $name parameter added. |
| 118 | * @since 5.5.0 $wp_tmpl_args parameter added. |
108 | 119 | * |
109 | | * @param string|null $name Name of the specific sidebar file to use. null for the default sidebar. |
| 120 | * @param string|null $name Name of the specific sidebar file to use. null for the default sidebar. |
| 121 | * @param array $wp_tmpl_args Additional arguments passed to the sidebar template. |
110 | 122 | */ |
111 | | do_action( 'get_sidebar', $name ); |
| 123 | do_action( 'get_sidebar', $name, $wp_tmpl_args ); |
112 | 124 | |
113 | 125 | $templates = array(); |
114 | 126 | $name = (string) $name; |
… |
… |
function get_sidebar( $name = null ) { |
118 | 130 | |
119 | 131 | $templates[] = 'sidebar.php'; |
120 | 132 | |
121 | | if ( ! locate_template( $templates, true ) ) { |
| 133 | if ( ! locate_template( $templates, true, true, $wp_tmpl_args ) ) { |
122 | 134 | return false; |
123 | 135 | } |
124 | 136 | } |
… |
… |
function get_sidebar( $name = null ) { |
141 | 153 | * |
142 | 154 | * @since 3.0.0 |
143 | 155 | * @since 5.5.0 A return value was added. |
| 156 | * @since 5.5.0 $wp_tmpl_args parameter added. |
144 | 157 | * |
145 | | * @param string $slug The slug name for the generic template. |
146 | | * @param string $name The name of the specialised template. |
| 158 | * @param string $slug The slug name for the generic template. |
| 159 | * @param string $name The name of the specialised template. |
| 160 | * @param array $wp_tmpl_args Additional arguments passed to the template. |
147 | 161 | * @return void|false Void on success, false if the template does not exist. |
148 | 162 | */ |
149 | | function get_template_part( $slug, $name = null ) { |
| 163 | function get_template_part( $slug, $name = null, $wp_tmpl_args = array() ) { |
150 | 164 | /** |
151 | 165 | * Fires before the specified template part file is loaded. |
152 | 166 | * |
… |
… |
function get_template_part( $slug, $name = null ) { |
154 | 168 | * for the generic template part. |
155 | 169 | * |
156 | 170 | * @since 3.0.0 |
| 171 | * @since 5.5.0 $wp_tmpl_args parameter added. |
157 | 172 | * |
158 | | * @param string $slug The slug name for the generic template. |
159 | | * @param string|null $name The name of the specialized template. |
| 173 | * @param string $slug The slug name for the generic template. |
| 174 | * @param string|null $name The name of the specialized template. |
| 175 | * @param array $wp_tmpl_args Additional arguments passed to the template. |
160 | 176 | */ |
161 | | do_action( "get_template_part_{$slug}", $slug, $name ); |
| 177 | do_action( "get_template_part_{$slug}", $slug, $name, $wp_tmpl_args ); |
162 | 178 | |
163 | 179 | $templates = array(); |
164 | 180 | $name = (string) $name; |
… |
… |
function get_template_part( $slug, $name = null ) { |
172 | 188 | * Fires before a template part is loaded. |
173 | 189 | * |
174 | 190 | * @since 5.2.0 |
| 191 | * @since 5.5.0 $wp_tmpl_args parameter added. |
175 | 192 | * |
176 | | * @param string $slug The slug name for the generic template. |
177 | | * @param string $name The name of the specialized template. |
178 | | * @param string[] $templates Array of template files to search for, in order. |
| 193 | * @param string $slug The slug name for the generic template. |
| 194 | * @param string $name The name of the specialized template. |
| 195 | * @param string[] $templates Array of template files to search for, in order. |
| 196 | * @param array $wp_tmpl_args Additional arguments passed to the template. |
179 | 197 | */ |
180 | | do_action( 'get_template_part', $slug, $name, $templates ); |
| 198 | do_action( 'get_template_part', $slug, $name, $templates, $wp_tmpl_args ); |
181 | 199 | |
182 | | if ( ! locate_template( $templates, true, false ) ) { |
| 200 | if ( ! locate_template( $templates, true, false, $wp_tmpl_args ) ) { |
183 | 201 | return false; |
184 | 202 | } |
185 | 203 | } |
… |
… |
function get_search_form( $args = array() ) { |
220 | 238 | * |
221 | 239 | * @since 2.7.0 as 'get_search_form' action. |
222 | 240 | * @since 3.6.0 |
| 241 | * @since 5.5.0 $args parameter added. |
| 242 | * |
| 243 | * @param array $args The array of arguments for building the search form. |
223 | 244 | * |
224 | 245 | * @link https://core.trac.wordpress.org/ticket/19321 |
225 | 246 | */ |
226 | | do_action( 'pre_get_search_form' ); |
| 247 | do_action( 'pre_get_search_form', $args ); |
227 | 248 | |
228 | 249 | $echo = true; |
229 | 250 | |
… |
… |
function get_search_form( $args = array() ) { |
262 | 283 | * Filters the HTML format of the search form. |
263 | 284 | * |
264 | 285 | * @since 3.6.0 |
| 286 | * @since 5.5.0 $args parameter added. |
265 | 287 | * |
266 | 288 | * @param string $format The type of markup to use in the search form. |
267 | 289 | * Accepts 'html5', 'xhtml'. |
| 290 | * @param array $args The array of arguments for building the search form. |
268 | 291 | */ |
269 | | $format = apply_filters( 'search_form_format', $format ); |
| 292 | $format = apply_filters( 'search_form_format', $format, $args ); |
270 | 293 | |
271 | 294 | $search_form_template = locate_template( 'searchform.php' ); |
272 | 295 | |
… |
… |
function get_search_form( $args = array() ) { |
308 | 331 | * Filters the HTML output of the search form. |
309 | 332 | * |
310 | 333 | * @since 2.7.0 |
| 334 | * @since 5.5.0 $args parameter added. |
311 | 335 | * |
312 | 336 | * @param string $form The search form HTML output. |
| 337 | * @param array $args The array of arguments for building the search form. |
313 | 338 | */ |
314 | | $result = apply_filters( 'get_search_form', $form ); |
| 339 | $result = apply_filters( 'get_search_form', $form, $args ); |
315 | 340 | |
316 | 341 | if ( null === $result ) { |
317 | 342 | $result = $form; |
diff --git src/wp-includes/template.php src/wp-includes/template.php
index 51a4c86358..04a5027862 100644
|
|
function get_attachment_template() { |
644 | 644 | * so that themes which inherit from a parent theme can just overload one file. |
645 | 645 | * |
646 | 646 | * @since 2.7.0 |
| 647 | * @since 5.5.0 $wp_tmpl_args parameter added. |
647 | 648 | * |
648 | 649 | * @param string|array $template_names Template file(s) to search for, in order. |
649 | 650 | * @param bool $load If true the template file will be loaded if it is found. |
650 | 651 | * @param bool $require_once Whether to require_once or require. Default true. Has no effect if $load is false. |
| 652 | * @param array $wp_tmpl_args Additional arguments passed to the template. |
651 | 653 | * @return string The template filename if one is located. |
652 | 654 | */ |
653 | | function locate_template( $template_names, $load = false, $require_once = true ) { |
| 655 | function locate_template( $template_names, $load = false, $require_once = true, $wp_tmpl_args = array() ) { |
654 | 656 | $located = ''; |
655 | 657 | foreach ( (array) $template_names as $template_name ) { |
656 | 658 | if ( ! $template_name ) { |
… |
… |
function locate_template( $template_names, $load = false, $require_once = true ) |
669 | 671 | } |
670 | 672 | |
671 | 673 | if ( $load && '' !== $located ) { |
672 | | load_template( $located, $require_once ); |
| 674 | load_template( $located, $require_once, $wp_tmpl_args ); |
673 | 675 | } |
674 | 676 | |
675 | 677 | return $located; |
… |
… |
function locate_template( $template_names, $load = false, $require_once = true ) |
683 | 685 | * also available. |
684 | 686 | * |
685 | 687 | * @since 1.5.0 |
| 688 | * @since 5.5.0 $wp_tmpl_args parameter added. |
686 | 689 | * |
687 | 690 | * @global array $posts |
688 | 691 | * @global WP_Post $post Global post object. |
… |
… |
function locate_template( $template_names, $load = false, $require_once = true ) |
698 | 701 | * |
699 | 702 | * @param string $_template_file Path to template file. |
700 | 703 | * @param bool $require_once Whether to require_once or require. Default true. |
| 704 | * @param array $wp_tmpl_args Additional arguments passed to the template. |
701 | 705 | */ |
702 | | function load_template( $_template_file, $require_once = true ) { |
| 706 | function load_template( $_template_file, $require_once = true, $wp_tmpl_args = array() ) { |
703 | 707 | global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID; |
704 | 708 | |
705 | 709 | if ( is_array( $wp_query->query_vars ) ) { |
diff --git tests/phpunit/data/themedir1/theme1/header.php tests/phpunit/data/themedir1/theme1/header.php
new file mode 100644
index 0000000000..9da09ebcb3
-
|
+
|
|
| 1 | <?php |
| 2 | echo json_encode( $wp_tmpl_args ); |
diff --git tests/phpunit/tests/general/template.php tests/phpunit/tests/general/template.php
index e387a49944..0871cbd290 100644
|
|
class Tests_General_Template extends WP_UnitTestCase { |
678 | 678 | function test_get_template_part_returns_false_on_failure() { |
679 | 679 | $this->assertFalse( get_template_part( 'non-existing-template' ) ); |
680 | 680 | } |
| 681 | |
| 682 | /** |
| 683 | * @ticket 21676 |
| 684 | */ |
| 685 | function test_load_template_with_params() { |
| 686 | load_template( DIR_TESTDATA . '/themedir1/theme1/header.php', false, array( 'foo' => 'baz' ) ); |
| 687 | $this->expectOutputString( '{"foo":"baz"}' ); |
| 688 | } |
681 | 689 | } |