phphttp-redirectwoocommercetemplate-redirect

Re-Direct Empty WooCommerce Search Result To Custom Page


I am trying to achieve a re-direct to a custom URL for all product search results that return a 0 result. With the code below I get "Fatal error" message screen in WordPress.

This is my code:

add_action( 'template_redirect', 'redirect_empty_search_result', 2 );
function redirect_empty_search_result() {

    if ( isset( $_GET['s'] ) && strcasecmp( $_GET['s'] ) == 0 ) {
        
        wp_safe_redirect( '/empty-search/' );
    
    exit();
    
    }
}

Solution

  • I haven't checked your code but "strcasecmp" requires two parameters exmaple :

    add_action( 'template_redirect', 'redirect_empty_search_result', 2 );
    function redirect_empty_search_result() {
    
        if ( isset( $_GET['s'] ) && strcasecmp( $_GET['s'], '' ) == 0 ) {
        
            wp_safe_redirect( '/empty-search/' );
            exit();
       }
    }