Make WordPress Core


Ignore:
Timestamp:
07/07/2020 11:00:21 AM (3 years ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/template.php

    r47808 r48370  
    645645 *
    646646 * @since 2.7.0
     647 * @since 5.5.0 The `$args` parameter was added.
    647648 *
    648649 * @param string|array $template_names Template file(s) to search for, in order.
    649650 * @param bool         $load           If true the template file will be loaded if it is found.
    650  * @param bool         $require_once   Whether to require_once or require. Default true. Has no effect if $load is false.
     651 * @param bool         $require_once   Whether to require_once or require. Has no effect if `$load` is false.
     652 *                                     Default true.
     653 * @param array        $args           Optional. Additional arguments passed to the template.
     654 *                                     Default empty array.
    651655 * @return string The template filename if one is located.
    652656 */
    653 function locate_template( $template_names, $load = false, $require_once = true ) {
     657function locate_template( $template_names, $load = false, $require_once = true, $args = array() ) {
    654658    $located = '';
    655659    foreach ( (array) $template_names as $template_name ) {
     
    670674
    671675    if ( $load && '' !== $located ) {
    672         load_template( $located, $require_once );
     676        load_template( $located, $require_once, $args );
    673677    }
    674678
     
    684688 *
    685689 * @since 1.5.0
     690 * @since 5.5.0 The `$args` parameter was added.
    686691 *
    687692 * @global array      $posts
     
    699704 * @param string $_template_file Path to template file.
    700705 * @param bool   $require_once   Whether to require_once or require. Default true.
    701  */
    702 function load_template( $_template_file, $require_once = true ) {
     706 * @param array  $args           Optional. Additional arguments passed to the template.
     707 *                               Default empty array.
     708 */
     709function load_template( $_template_file, $require_once = true, $args = array() ) {
    703710    global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
    704711
Note: See TracChangeset for help on using the changeset viewer.