• Resolved stdesigner

    (@jplavoie)


    Hi everyone,

    Here’s my issue. I have this multi-language website I’m working on and I’ve been trying to add a search button on top to browse around the products.

    Here’s the button code:

    <form role="search" method="get" class="woocommerce-product-search" action="<?php echo esc_url( home_url( '/'  ) ); ?>">
    	<label class="screen-reader-text" for="woocommerce-product-search-field"><?php _e( 'Search for:', 'woocommerce' ); ?></label>
                    <input class="trend-search-input" type="search" id="woocommerce-product-search-field" placeholder="<?php echo esc_attr_x( 'Search Products&hellip;', 'placeholder', 'woocommerce' ); ?>" value="<?php echo get_search_query();  ?>" name="s" title="<?php echo esc_attr_x( 'Search for:', 'label', 'woocommerce' ); ?>" />
                    <input class="trend-search-submit" type="submit" value="<?php echo esc_attr_x( 'Search', 'submit button', 'woocommerce' ); ?>" />
                    <input type="hidden" name="post_type" value="product" />
                    <input type="hidden" name="lang" value="<?php echo ICL_LANGUAGE_CODE; ?>"/>
                    <span class="trend-icon-search"></span>
    </form>

    Now the search works fine, but whenever I type a keyword containing é à è or any other characters with an accent, I end up with a 404 error because the URL includes the accent.

    ex. https://www.domain.com/?s=élite&post_type=product&lang=fr

    I’ve been trying to fix this using the sanitize and remove_accents functions but it’s not working..

    Any idea how I can modify the form so the URL doesn’t include the accents? (like this: https://www.domain.com/?s=elite&post_type=product&lang=fr)

    Thanks

    • This topic was modified 8 years, 1 month ago by stdesigner.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    You can pass accented characters in URLs by using urlencode() on the URL before using it. This will not remove the accents, but should prevent 404s. If you still want to remove the accents use remove_accents(). So obvious, who would have thought? ??

    Thread Starter stdesigner

    (@jplavoie)

    So obvious indeed, that’s why I did try remove_accents() like I said in my previous email ??

    Unfortunately, I’m not much of a developper so I guess I’m doing it wrong.

    <input class="trend-search-input" type="search" id="woocommerce-product-search-field" placeholder="<?php echo esc_attr_x( 'Search Products&hellip;', 'placeholder', 'woocommerce' ); ?>" value="<?php echo remove_accents ( get_search_query() ); ?>" name="s" title="<?php echo esc_attr_x( 'Search for:', 'label', 'woocommerce' ); ?>" />

    For the urlencode(), where do I call this function?

    Moderator bcworkz

    (@bcworkz)

    Heh, I guess the only thing obvious is I don’t read very carefully. ??

    The problem is where you’ve removed accents would only affect any default value of the field. It does nothing when a user enters an accented search string, then submits it. The browser is responsible for composing submitted query strings, it is beyond the ability of PHP to do this.

    What’s puzzling is accented query strings should not cause 404s. I put ?s=élite&post_type=product&lang=fr
    after my site’s URL and the one product where I added “élite” was returned — no 404.

    Are you sure you are getting a true 404 file not found error and not a no posts found WordPress error? There are products that have élite or whatever search terms in their description, correct?

    Please go to your site’s /wp-admin/options.php and locate the “blog_charset” entry. It should be UTF-8 for maximum compatibility in any language.

    If all that checks out, there is a plugin or your theme that is preventing proper use of the search facility. Please deactivate all plugins except Woocommerce (I’ve already confirmed it’s not a problem). Switch to the twentysixteen theme and put your search form on a template file, page.php for example. Open a page that uses the template and use your search form. It should work fine.

    Switch back to your normal theme, then activate plugins one at a time. Test the search form after each change. When it fails again, the last activated plugin is at fault.

    Thread Starter stdesigner

    (@jplavoie)

    Hey,

    Thanks for the advice. I checked options.php for blog_charset, everything was fine. So I deactivate every plugin and switch back to twentysixteen. The search query worked!

    So I started reactivating everything… until I was done. Never I’ve been able to replicate the same 404 error I’ve had before deactivating everything.

    So now it’s all back online, didn’t add or remove anything, and everything’s fine…

    “My code doesn’t work, I don’t know why. My code works, I don’t know why” never been so true!

    Thanks for the help by the way, much appreciated.

    Moderator bcworkz

    (@bcworkz)

    You’re welcome, though I didn’t really fix anything ??

    I think what happened is there was a stale rewrite rule fouling up the process. By going through all of that you caused the rewrite rules to be regenerated. Had we known going in, just re-saving the permalink settings may have solved the issue.

    At least the problem is gone!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Search Result URL contains accents (é è à) and result error 404’ is closed to new replies.