# Redirect Empty Searches to the Homepage

> Sends visitors who submit the search form without typing anything back to the homepage instead of showing every post.

- Type: PHP
- Runs: Run everywhere
- Category: [Search](https://wpide.com/library/category/search/)
- Tags: search, redirect, ux
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/redirect-empty-search/

## Code

```php
add_action( 'template_redirect', function () {
    if ( isset( $_GET['s'] ) && is_string( $_GET['s'] ) && '' === trim( wp_unslash( $_GET['s'] ) ) ) {
        wp_safe_redirect( home_url( '/' ) );
        exit;
    }
} );
```

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Redirect Empty Searches to the Homepage" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
