Doc WordPress WordPress Tricks, Tips & Snippets

23Jan/170

Allow empty search

The default WordPress search function just redirects you to the home page should the search form be empty, which can be confusing for the visitors. Should you prefer to correctly display the search result page with a "Sorry your search returned no results instead" message you can fix this by adding this code to your functions.php:

add_filter( 'request', 'my_request_filter' );
function my_request_filter( $query_vars ) {
    if( isset( $_GET['s'] ) && empty( $_GET['s'] ) && !is_admin()) {
        $query_vars['s'] = " ";
    }
    return $query_vars;
}

You can test this behaviour right here on docwordpress.com.

12Oct/150

Disable automatic redirect to site url

By default WordPress redirects incoming links automatically to the proper URL based on the site url, e.g. it would redirect incorrect incoming "www." URLs automatically to the correct form, as set in the site url setting. For example: when a requested URL is "http://yourdomain.com/whatever/" but the site-url is set as "http://www.yourdomain.com" WordPress will automatically redirect the visitor to "http://www.yourdomain.com/whatever/".

This is a good thing as search engines consider www. and non-www. as different URLs and this would otherwise lead to duplicate content. But should you for whatever reason wish to disable this behaviour you can easily do so by just adding this snippet to your theme's function.php:


remove_action('template_redirect', 'redirect_canonical');

Please note: this will also disable canonical redirects!