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.
Opens WPIDE on
Code
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 add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.