No results page for empty searches
Searching for nothing (an empty search box) sends the visitor back to the home page instead of listing every post.
<?php
add_action(
'template_redirect',
static function () {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only.
if ( isset( $_GET['s'] ) && '' === trim( (string) wp_unslash( $_GET['s'] ) ) && ! is_admin() && empty( $_GET['post_type'] ) ) {
wp_safe_redirect( home_url( '/' ) );
exit;
}
}
);
In these packs
- Search and archives: Better site search and cleaner archive pages.