[Resolved] Menu search not working on EN website

Home Forums Support [Resolved] Menu search not working on EN website

Home Forums Support Menu search not working on EN website

Viewing 15 posts - 1 through 15 (of 26 total)
  • Author
    Posts
  • #891687
    Zuzana

    Hello!
    I am using plugin Polylang to have a Slovak and English version of the website.
    I have the search option in the main menu in both language versions. On the Slovak version, the search functionality work as supposed, however on the English version of the website, the search is not working (after writing search term and hitting ENTER it either turns 404 or reloads the current page).

    Do you have an idea what could cause this problem?
    Thank you very much in advance!

    Zuzana

    #892316
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    That’s very strange. I wonder if it’s something Polylang has seen before. Any chance you can report this to their support to see if it’s something they can help with? We’re happy to tweak something if necessary.

    Let me know πŸ™‚

    #892472
    Zuzana

    Thank you for your answer Tom! πŸ™‚

    Unfortunately I do not have a PRO version of the Polylang so I don’t have access to their official support forum, however, I was searching on their WordPress forum and on the Google and I found out these statements from Polylang regarding search issue:

    https://wordpress.org/support/topic/polylang-and-wordpress-search/
    https://polylang.pro/always-use-get_search_form-to-create-search-forms/
    https://gist.github.com/bramchi/d0767c32a772550486ea

    Do you think it could be a problem with the search form itself or search filter as mentioned in those links?
    There are pieces of code suggested, however, I don’t know how to incorporate them within the theme. In this website, I am using PHP Snippets to insert the PHP code.

    #893040
    Tom
    Lead Developer
    Lead Developer

    We don’t use get_search_form() for the navigation search.

    However, let’s try this:

    add_filter( 'generate_navigation_search_output', function() {
        $current_language = '';
    
        if ( function_exists( 'pll_current_language' ) ) {
            $current_language = pll_current_language();
    
            if ( 'sk' == $current_language ) {
    	    $current_language = '';
            }
        }
    
        return sprintf( // WPCS: XSS ok, sanitization ok.
            '<form method="get" class="search-form navigation-search" action="%1$s">
                <input type="search" class="search-field" value="%2$s" name="s" title="%3$s" />
            </form>',
            esc_url( home_url( '/' . $current_language ) ),
            esc_attr( get_search_query() ),
            esc_attr_x( 'Search', 'label', 'generatepress' )
        );
    } );

    Adding PHP: https://docs.generatepress.com/article/adding-php/

    Let me know πŸ™‚

    #893122
    Zuzana

    The code, unfortunately, did not work, it actually stopped the functionality of the language switcher in the menu and the site was appearing always in English (even if I changed URL in the browser to Slovak one, it linked it to English one). I switched off the code since the page is already live.

    However, now I realized that the search function actually does not work only on the first attempt. So when I am on the EN homepage, open the search bar and click ENTER to search, it reloads the homepage and does not redirect to search results. However after this reload when I again open the search bar and click ENTER, the search results are actually shown correctly.

    #893407
    Tom
    Lead Developer
    Lead Developer

    Hmm, it certainly shouldn’t have broken anything, but I won’t know for sure unless I can actually see the issue. It might be worth setting up a staging site if you can’t test on this site.

    I just did some more searching and adjusted the code above if you want to give it another shot: https://generatepress.com/forums/topic/menu-search-not-working-on-en-website/#post-893040

    #894991
    Zuzana

    This new code worked! Awesome, thank you a lot! πŸ™‚

    Just one more thing:
    The search is now working correctly on EN site.
    However on SK version, it turnes 404, I believe it is for a reason that it gives this link:
    https://marekstraka.com/sk/?s=biznis
    instead of
    https://marekstraka.com/?s=biznis.

    So on EN site the search url is created correctly with the /en/ after the domain name, however, for SK language version of the search site should be created without /sk/ after the domain.

    Is it still possible to correct the code based on this, please? πŸ™‚

    The code is running live so you should be able to see the issue now.

    #895507
    Tom
    Lead Developer
    Lead Developer
    #897545
    Zuzana

    Hi!

    I replaced the code, cleared the cache, but the functionality did not change. Still, on SK language pages there is /sk/ being added to the domain name which means that the search turns 404.

    #898062
    Tom
    Lead Developer
    Lead Developer

    Is SK the default language?

    #898714
    Zuzana

    Yes, SK is the default language. In settings I also have checked that when the page is in the default language, the information about language in URL is hidden – that is why SK URLs are without /sk/.

    #899200
    Tom
    Lead Developer
    Lead Developer

    Hmm, that code checks whether the default language is different than the current language before changing the search output:

    $current_language !== $default_language

    That means current language isn’t the default language. If they match (both SK), it shouldn’t change anything in the nav search.

    Can we try debugging something?

    After this line:

    $default_language = pll_default_language();

    Add this:

    var_dump($default_language);

    That should output the default language on your page. What does it say?

    #1321614
    Zuzana

    Hello Tom,

    sorry for a very late answer, just now we came back to this project. I was placing the var_dump code and what came out was: string(2) “sk”

    Thank you very much,
    Zuzana

    #1322440
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Can you try the updated code?: https://generatepress.com/forums/topic/menu-search-not-working-on-en-website/#post-893040

    Let me know πŸ™‚

    #1322457
    Zuzana

    Thank you for a fast response, the updated code made the search on the Slovak language version work, however now when I click on the search on the English version, the search is redirected to the Slovak one.

Viewing 15 posts - 1 through 15 (of 26 total)
  • You must be logged in to reply to this topic.