[wp-trac] [WordPress Trac] #32528: Create a new search_form_content hook in get_search_form()

WordPress Trac noreply at wordpress.org
Fri May 29 16:22:30 UTC 2015


#32528: Create a new search_form_content hook in get_search_form()
--------------------------------+-----------------------------
 Reporter:  charlestonsw        |      Owner:
     Type:  feature request     |     Status:  new
 Priority:  normal              |  Milestone:  Awaiting Review
Component:  General             |    Version:  trunk
 Severity:  normal              |   Keywords:
  Focuses:  ui, administration  |
--------------------------------+-----------------------------
 The only viable method for creating a custom search form in a plugin or
 theme is to replace the entire default search form.

 This runs the risk of introducing possible security issues should a patch
 be issued in the default search form.   More importantly it means more
 potential deviation from "the WordPress standard".

 Instead of requiring developers to recreate a new feature that mimics the
 HTML that get_search_form() provides, consider adding a new filter that
 allows developers to easily add HTML elements to the search form.

 The current general-template.php get_search_form() snippet:

 {{{
                 if ( 'html5' == $format ) {
                         $form = '<form role="search" method="get" class
 ="search-form" action="' . esc_url( home_url( '/' ) ) . '">
                                 <label>
                                         <span class="screen-reader-text">'
 . _x( 'Search for:', 'label' ) . '</span>
                                         <input type="search" class
 ="search-field" placeholder="' . esc_attr_x( 'Search …',
 'placeholder' ) . '" value="' . get_search_query() . '" name="s" title="'
 . esc_attr_x( 'Search for:', 'label' ) . '" />
                                 </label>
                                 <input type="submit" class="search-submit"
 value="'. esc_attr_x( 'Search', 'submit button' ) .'" />
                         </form>';
                 } else {
                         $form = '<form role="search" method="get"
 id="searchform" class="searchform" action="' . esc_url( home_url( '/' ) )
 . '">
                                 <div>
                                         <label class="screen-reader-text"
 for="s">' . _x( 'Search for:', 'label' ) . '</label>
                                         <input type="text" value="' .
 get_search_query() . '" name="s" id="s" />
                                         <input type="submit"
 id="searchsubmit" value="'. esc_attr_x( 'Search', 'submit button' ) .'" />
                                 </div>
                         </form>';
                 }
 }}}

 Allow developers to easily insert additional input elements between the
 search input and submit elements:

 {{{
                 if ( 'html5' == $format ) {
                         $form = '<form role="search" method="get" class
 ="search-form" action="' . esc_url( home_url( '/' ) ) . '">
                                 <label>
                                         <span class="screen-reader-text">'
 . _x( 'Search for:', 'label' ) . '</span>
                                         <input type="search" class
 ="search-field" placeholder="' . esc_attr_x( 'Search …',
 'placeholder' ) . '" value="' . get_search_query() . '" name="s" title="'
 . esc_attr_x( 'Search for:', 'label' ) . '" />
                                 </label>'.
                 apply_filters( 'search_form_content', '' ) .
                                 '<input type="submit" class="search-
 submit" value="'. esc_attr_x( 'Search', 'submit button' ) .'" />
                         </form>';
                 } else {
                         $form = '<form role="search" method="get"
 id="searchform" class="searchform" action="' . esc_url( home_url( '/' ) )
 . '">
                                 <div>
                                         <label class="screen-reader-text"
 for="s">' . _x( 'Search for:', 'label' ) . '</label>
                                         <input type="text" value="' .
 get_search_query() . '" name="s" id="s" />' .
                      apply_filters( 'search_form_content', '' ) .
                                         '<input type="submit"
 id="searchsubmit" value="'. esc_attr_x( 'Search', 'submit button' ) .'" />
                                 </div>
                         </form>';
                 }
 }}}


 There are a number of ways to implement this, including the possibility of
 passing in the initial label + input fields HTML as a parameter to the
 filter.

 The general idea is to keep the form HTML and submit HTML intact to ensure
 maximum functionality/compatibility with the core search feature.

 As long as the hook processor does not impart too much overhead, this may
 be a viable feature to keep more control over the search form and
 simplify/eliminate many custom searchform.php files that only need to add
 an extra input element such as "select a category" to further refine the
 search mechanism.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/32528>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list