[wp-trac] [WordPress Trac] #34886: Search Form should not submit empty strings

WordPress Trac noreply at wordpress.org
Sun Dec 6 21:39:37 UTC 2015


#34886: Search Form should not submit empty strings
-------------------------+------------------------------
 Reporter:  gibrown      |       Owner:
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  Awaiting Review
Component:  Widgets      |     Version:  trunk
 Severity:  normal       |  Resolution:
 Keywords:               |     Focuses:
-------------------------+------------------------------

Comment (by westonruter):

 @gibrown Good idea.

 I think, however, that jQuery should be used for the new functionality,
 and that the JS should be loaded from a JS file instead of inlined to PHP.

 Additionally, I think that the `submit` event should be used instead, for
 example:

 {{{#!js
 jQuery( function( $ ) {
         $( 'form.search-form' ).on( 'submit', function( e ) {
                 var input = $( this ).find( 'input[name=s]' );
                 if ( ! input.val() ) {
                         e.preventDefault();
                         input.focus();
                 }
         } );
 } );
 }}}

 Alternatively, this same functionality can be implemented in HTML5 simply
 by adding a `required` attribute to the `s` input element:

 {{{#!diff
 diff --git src/wp-includes/general-template.php src/wp-includes/general-
 template.php
 index f554e1f..e65bd84 100644
 --- src/wp-includes/general-template.php
 +++ src/wp-includes/general-template.php
 @@ -226,7 +226,7 @@ function get_search_form( $echo = true ) {
                         $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' ) . '" />
 +                                       <input type="search" class
 ="search-field" required 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>';
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/34886#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list