Search code examples
phpwordpresswoocommercewordpress-themingurlencode

WordPress how to encode the searched URL


I want to make a search URL look like this:

example.com/search/search+query?post_type=product.

I have tried many methods including this:

wp_redirect( home_url( "/search/?s=" ) . urlencode( get_query_var( 's' ) . "&post_type=" . urlencode( get_query_var( 'post_type' ) ) );

and this:

wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ). "?post_type=". urlencode( get_query_var( 'post_type' ) ) ) );

but the result I am getting:

example.com/search/search+query%3Fpost_type%3Dproduct

What should I do?


Solution

  • The following snippet will do the trick for you!

    $decoded_url = urldecode('example.com/search/search+query%3Fpost_type%3Dproduct');
    
    echo $decoded_url;
    

    And the result would be:

    example.com/search/search query?post_type=product