• Resolved Jan

    (@krugmedien)


    Hi,

    when I am using a html form on my website, WordPress redirects this url:

    • example.com/?test=my+input+data
    • to this url: example.com/?test=my%20input%20data

    This results in a double page load (!) and the reason is this action:

    #wp-includes/default-filters.php:606
    add_action( 'template_redirect', 'redirect_canonical' );

    When I comment out this line everything is fine.

    I noticed that this does not happen, when I use the “s” parameter.

    • for example: example.com/?s=my+input+data

    How can I exlude another parameter (so that it behaves like “s”)?
    Or remove this action?

    I tried things like this, but I could not make it work:

    function remove_wp_redirect()
    {
        if ( strpos($_SERVER["REQUEST_URI"], '+') !== false )
        {
            remove_action( 'template_redirect', 'redirect_canonical' );
        }
    }
    add_action( 'init', 'remove_wp_redirect' );

    Any ideas? 🙂

    Thank you

Viewing 2 replies - 1 through 2 (of 2 total)
  • Jay

    (@jaygumanid)

    It looks like you are trying to prevent WordPress from redirecting a URL that includes the test parameter, and you have noticed that the s parameter does not trigger the redirect.

    To exclude the test parameter from the redirect_canonical function, you can use the redirect_canonical filter hook provided by WordPress. This hook allows you to modify the redirect URL before it is actually redirected.

    Here is an example of how you can use the redirect_canonical filter to achieve what you want:

    function exclude_test_parameter_from_redirect( $redirect_url, $requested_url ) {
        // Check if the requested URL contains the "test" parameter
        if ( strpos( $requested_url, 'test=' ) !== false ) {
            // If the "test" parameter is present, return the requested URL without modification
            return $requested_url;
        }
    
        // Otherwise, return the default redirect URL
        return $redirect_url;
    }
    add_filter( 'redirect_canonical', 'exclude_test_parameter_from_redirect', 10, 2 );
    

    This code will check if the requested URL contains the test parameter, and if it does, it will return the requested URL without modification, effectively preventing the redirect. If the test parameter is not present, the default redirect URL will be returned.

    I hope this helps! Let me know if you have any questions.

    Thread Starter Jan

    (@krugmedien)

    @jaygumanid Thank you very much, this helps a lot!
    Do you have PayPal? I want to thank you and contacted you on LinkedIn.
    Have a great day.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Double page load because WordPress redirects plus char to percent 20’ is closed to new replies.