Thursday, 18 April 2013

Blank search issue in wordpress

In wordpress default search form the blank search will redirects to home page. We can use request filter to the trick. Following code can be used to redirect the blank search to search page.

Just paste the following code in either plugin files or in the functions.php ( theme file ).

add_filter( 'request', 'blank_search_fix' );

if(!function_exists('blank_search_fix')){
 function blank_search_fix( $query_vars ) {
     if( isset( $_GET['s'] ) && empty( $_GET['s'] ) ) {
         $query_vars['s'] = " ";
     }
     return $query_vars;
 }
}

in this code $_GET['s'] denotes the search field in the (search)form  
<input type="text" name="s" id="s" value="<?php echo trim( get_search_query() ); ?>" />


No comments:

Post a Comment