Created
August 4, 2025 19:23
-
-
Save 2ndkauboy/011d8a28cdc462e61a4f0dacd48326eb to your computer and use it in GitHub Desktop.
Limit searches on the whole site to only show products.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Product Search Only | |
| * | |
| * @package AllowPrivacyPolicyPageEdits | |
| * @author Bernhard Kau | |
| * @license GPL-2.0-or-later | |
| * | |
| * @wordpress-plugin | |
| * Plugin Name: Product Search Only | |
| * Plugin URI: https://gist.github.com/2ndkauboy/011d8a28cdc462e61a4f0dacd48326eb | |
| * Description: Limit searches on the entire site to only show products. | |
| * Version: 1.0.0 | |
| * Requires at least: 4.9 | |
| * Requires Plugins: woocommerce | |
| * Author: Bernhard Kau | |
| * Author URI: https://kau-boys.de | |
| * License: GPL v2 or later | |
| * License URI: https://www.gnu.org/licenses/gpl-2.0.html | |
| */ | |
| /** | |
| * Redirects search queries to the product search results page. | |
| * | |
| * This function ensures that search queries on the site are redirected | |
| * to the WooCommerce shop page with the search parameter appended. | |
| * It prevents execution in the admin panel and avoids redirection | |
| * when already on the relevant product search pages. | |
| * | |
| * @return void | |
| */ | |
| function product_search_only_redirect_search_to_products() { | |
| if ( is_admin() ) { | |
| return; | |
| } | |
| if ( ! is_search() ) { | |
| return; | |
| } | |
| // We are already at the right search result page. | |
| if ( is_shop() || is_tax( get_object_taxonomies( 'product' ) ) ) { | |
| return; | |
| } | |
| $search_query = get_search_query(); | |
| $shop_url = wc_get_page_permalink( 'shop' ); | |
| $redirect_url = add_query_arg( 's', rawurlencode( $search_query ), $shop_url ); | |
| wp_safe_redirect( $redirect_url ); | |
| exit(); | |
| } | |
| add_action( 'template_redirect', 'product_search_only_redirect_search_to_products' ); | |
| /** | |
| * Clears the 's' parameter from the query if the search term is empty in the request. | |
| * | |
| * @param WP $wp The WordPress environment object containing query variables. | |
| * | |
| * @return void | |
| */ | |
| function product_search_only_clear_empty_search_from_request( $wp ) { | |
| if ( isset( $_GET['s'] ) && empty( trim( $_GET['s'] ) ) ) { // phpcs:ignore WordPress.Security | |
| unset( $wp->query_vars['s'] ); | |
| } | |
| } | |
| add_action( 'parse_request', 'product_search_only_clear_empty_search_from_request' ); | |
| /** | |
| * Do not redirect to the single product page if only one product was found in the search. | |
| */ | |
| add_filter( 'woocommerce_redirect_single_search_result', '__return_false' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment