Skip to content

Early-bird lifetime licence: $199 once, for the first 200 buyers only. See the offer

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.

  • Type PHP
  • Runs Everywhere (runs as WordPress loads)
  • Category Search and archives
  • Tested with WordPress 7.1
PHP
<?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

Move your snippets over this afternoon.

Importing changes nothing until you switch over, and your old shortcodes keep working.