Make WordPress Core

Ticket #42057: 42057.diff

File 42057.diff, 3.5 KB (added by williampatton, 7 years ago)

Patch to allow passing of $args array to add a label

  • wp-includes/general-template.php

    diff --git wp-includes/general-template.php wp-includes/general-template.php
    index 7b311b6..d5efff3 100644
    function get_template_part( $slug, $name = null ) { 
    174174 *
    175175 * @since 2.7.0
    176176 *
    177  * @param bool $echo Default to echo and not return the form.
    178  * @return string|void String when $echo is false.
     177 * @param array $args Default args are to echo and include no custom label.
     178 * @return string|void String when $args['echo'] is false.
    179179 */
    180 function get_search_form( $echo = true ) {
     180function get_search_form( $args = array() ) {
    181181        /**
    182182         * Fires before the search form is retrieved, at the start of get_search_form().
    183183         *
    function get_search_form( $echo = true ) { 
    200200         */
    201201        $format = apply_filters( 'search_form_format', $format );
    202202
     203        /*
     204         Back compat: to ensure previous uses of get_search_form continue to
     205         function as expected first check if we have an array. If not treat as
     206         booleen - and if it's not explicitly FALSE then assume we want to echo.
     207         */
     208        if( ! is_array( $args ) && ! false === $args ){
     209                // we can set an empty array and allow default args to take over.
     210                $args = array();
     211        }
     212
     213        // defaults are to echo and to output no custom label on the landmark.
     214        $defaults = array(
     215                'echo'  => true,
     216                'label' => false,
     217        );
     218
     219        // merge any args with the defaults.
     220        $args = wp_parse_args( $args, $defaults );
     221
     222        /**
     223         * Filters the array of args used when generating the search form.
     224         *
     225         * @since NEW
     226         *
     227         * @param array $args The array of args used when building the search form.
     228         */
     229        $result = apply_filters( 'search_form_args', $args );
     230
    203231        $search_form_template = locate_template( 'searchform.php' );
    204232        if ( '' != $search_form_template ) {
    205233                ob_start();
    206234                require( $search_form_template );
    207235                $form = ob_get_clean();
    208236        } else {
     237                // form a string containaing a label to use for the search role.
     238                if( $args['label'] ){
     239                        $label = 'aria-label="' . esc_attr( $args['label'] ) . '" ';
     240                } else {
     241                        // if there is no custom label we can set a default here. ATM it is
     242                        // empty as there's uncertainty about what the default should be.
     243                        $label = '';
     244                }
    209245                if ( 'html5' == $format ) {
    210                         $form = '<form role="search" method="get" class="search-form" action="' . esc_url( home_url( '/' ) ) . '">
     246                        $form = '<form role="search" ' . $label . 'method="get" class="search-form" action="' . esc_url( home_url( '/' ) ) . '">
    211247                                <label>
    212248                                        <span class="screen-reader-text">' . _x( 'Search for:', 'label' ) . '</span>
    213249                                        <input type="search" class="search-field" placeholder="' . esc_attr_x( 'Search &hellip;', 'placeholder' ) . '" value="' . get_search_query() . '" name="s" />
    function get_search_form( $echo = true ) { 
    215251                                <input type="submit" class="search-submit" value="'. esc_attr_x( 'Search', 'submit button' ) .'" />
    216252                        </form>';
    217253                } else {
    218                         $form = '<form role="search" method="get" id="searchform" class="searchform" action="' . esc_url( home_url( '/' ) ) . '">
     254                        $form = '<form role="search" ' . $label . 'method="get" id="searchform" class="searchform" action="' . esc_url( home_url( '/' ) ) . '">
    219255                                <div>
    220256                                        <label class="screen-reader-text" for="s">' . _x( 'Search for:', 'label' ) . '</label>
    221257                                        <input type="text" value="' . get_search_query() . '" name="s" id="s" />
    function get_search_form( $echo = true ) { 
    237273        if ( null === $result )
    238274                $result = $form;
    239275
    240         if ( $echo )
     276        if ( array_key_exists( 'echo', $args ) && $args['echo'] )
    241277                echo $result;
    242278        else
    243279                return $result;