WordPress.org

Make WordPress Core

Ticket #19579: wp-includes.general-template.get_search_form.diff

File wp-includes.general-template.get_search_form.diff, 5.1 KB (added by chipbennett, 6 years ago)

Replacement get_search_form(), with updated phpDocs

  • general-template.php

     
    131131/**
    132132 * Display search form.
    133133 *
     134 * This function is primarily used by themes that want to hardcode the search
     135 * form into the sidebar and also by the search widget in WordPress.
     136 *
    134137 * Will first attempt to locate the searchform.php file in either the child or
    135138 * the parent, then load it. If it doesn't exist, then the default search form
    136139 * will be displayed. The default search form is HTML, which will be displayed.
    137  * There is a filter applied to the search form HTML in order to edit or replace
    138  * it. The filter is 'get_search_form'.
     140 *
     141 * The 'get_search_form' action is fired when the function is called, which can
     142 * be useful for outputting search-form dependent JavaScript or various formatting
     143 * that applies to the beginning of the search form.
     144 *
     145 * The 'search_form_defaults' filter is applied to the parsed argument array,
     146 * which can be useful for filtering the defaults in lieu of passing an argument
     147 * array directly to the function.
     148 *
     149 * The 'get_search_form' filter applied to the search form HTML in order to edit
     150 * or replace it.
     151 *
     152 * For backward compatibility, the function will also accept a boolean $echo
     153 * argument, to determine if the search form is echoed (true) or returned (false).
    139154 *
    140  * This function is primarily used by themes which want to hardcode the search
    141  * form into the sidebar and also by the search widget in WordPress.
    142  *
    143  * There is also an action that is called whenever the function is run called,
    144  * 'get_search_form'. This can be useful for outputting JavaScript that the
    145  * search relies on or various formatting that applies to the beginning of the
    146  * search. To give a few examples of what it can be used for.
    147  *
    148  * @since 2.7.0
    149  * @param boolean $echo Default to echo and not return the form.
     155 * @since       2.7.0
     156 * @param       array   $args   Array of arguments passed to the search form
    150157 */
    151 function get_search_form($echo = true) {
     158function get_search_form( $args = array() ) {
    152159        do_action( 'get_search_form' );
    153160
    154         $search_form_template = locate_template('searchform.php');
     161        $search_form_template = locate_template( 'searchform.php' );
    155162        if ( '' != $search_form_template ) {
    156                 require($search_form_template);
     163                require( $search_form_template );
    157164                return;
    158165        }
    159166
    160         $form = '<form role="search" method="get" id="searchform" action="' . esc_url( home_url( '/' ) ) . '" >
    161         <div><label class="screen-reader-text" for="s">' . __('Search for:') . '</label>
    162         <input type="text" value="' . get_search_query() . '" name="s" id="s" />
    163         <input type="submit" id="searchsubmit" value="'. esc_attr__('Search') .'" />
    164         </div>
    165         </form>';
     167        // Define default arguments
     168        $defaults = array(
     169                'id'                    => 's'
     170                'container'             => 'div',
     171                'form_id'               => 'searchform',
     172                'label_atts'            => array(
     173                        'class'                 => 'screen-reader-text'
     174                ),
     175                'label_text'    => __( 'Search for:' ),
     176                'input_atts'    => array(
     177                        'type'                  => 'text',
     178                        'value'                 => get_search_query(),
     179                        'placeholder'   => 'Search'
     180                ),
     181                'submit_atts'   => array(
     182                        'type'                  => 'submit',
     183                        'value'                 => __( 'Search' ),
     184                ),
     185                'echo'                  => true
     186        );
     187       
     188        // Maintain backward compatibility with $echo = true
     189        $echo = ( is_bool( $args ) ? $args : $args['echo'] );
     190        $args = ( is_array( $args ) ? wp_parse_args( $args, $defaults ) : $defaults );
    166191
    167         if ( $echo )
    168                 echo apply_filters('get_search_form', $form);
    169         else
    170                 return apply_filters('get_search_form', $form);
     192        // Allow search form arguments to be filtered
     193        $args = apply_filters( 'search_form_defaults', $args );
     194
     195        // Label attributes
     196        $label_atts = '';
     197        foreach ( $args['label_atts'] as $att => $value ) {
     198                $label_atts .= $att . '="' . esc_attr( $value ) . '"';
     199        }
     200        $label_atts = implode( ' ', $label_atts );
     201       
     202        // Input attributes
     203        $input_atts = '';
     204        foreach ( $args['input_atts'] as $att => $value ) {
     205                $input_atts .= $att . '="' . esc_attr( $value ) . '"';
     206        }
     207        $input_atts = implode( ' ', $input_atts );
     208       
     209        // Submit attributes
     210        $submit_atts = '';
     211        foreach ( $args['submit_atts'] as $att => $value ) {
     212                $submit_atts .= $att . '="' . esc_attr( $value ) . '"';
     213        }
     214        $submit_atts = implode( ' ', $submit_atts );
     215
     216        // Construct the form markup   
     217        $form = '<form role="search" method="get" id="' . esc_attr( $args['form_id'] ) . '" action="' . esc_url( home_url( '/' ) ) . '" >';
     218        if ( false != $args['container'] ) {
     219                $form .= '<' . esc_attr( $args['container'] ) . '>';
     220        }
     221        $form .= '<label for="' . esc_attr( $args['id'] ) . '" ' . $label_atts . '>' . esc_attr( $args['label_text'] ) . '</label>';
     222        $form .= '<input name="' . esc_attr( $args['id'] ) . '" id="' . esc_attr( $args['id'] ) . '" ' . $input_atts . ' />';
     223        $form .= '<input id="' . esc_attr( $args['form_id'] ) . '" ' . $submit_atts . ' />';
     224        if ( false != $args['container'] ) {
     225                $form .= '</' . esc_attr( $args['container'] ) . '>';
     226        }
     227        $form .= '</form>';
     228
     229        // Echo or return       
     230        if ( $echo ) {
     231                echo apply_filters( 'get_search_form', $form );
     232        } else {
     233                return apply_filters( 'get_search_form', $form );
     234        }
    171235}
    172236
    173237/**