Doc WordPress WordPress Tricks, Tips & Snippets


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


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 "" but the site-url is set as "" WordPress will automatically redirect the visitor to "".

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!