- This topic has 25 replies, 2 voices, and was last updated 2 years, 11 months ago by
Tom.
-
AuthorPosts
-
May 7, 2019 at 6:25 am #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
May 7, 2019 at 4:42 pm #892316Tom
Lead DeveloperLead DeveloperHi 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 π
May 7, 2019 at 11:24 pm #892472Zuzana
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/d0767c32a772550486eaDo 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.May 8, 2019 at 8:32 am #893040Tom
Lead DeveloperLead DeveloperWe 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 π
May 8, 2019 at 9:29 am #893122Zuzana
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.
May 8, 2019 at 2:02 pm #893407Tom
Lead DeveloperLead DeveloperHmm, 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
May 10, 2019 at 2:35 am #894991Zuzana
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.
May 10, 2019 at 9:14 am #895507Tom
Lead DeveloperLead DeveloperI just made another adjustment: https://generatepress.com/forums/topic/menu-search-not-working-on-en-website/#post-893040
Let me know π
May 13, 2019 at 12:49 am #897545Zuzana
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.
May 13, 2019 at 8:55 am #898062Tom
Lead DeveloperLead DeveloperIs SK the default language?
May 14, 2019 at 3:52 am #898714Zuzana
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/.
May 14, 2019 at 8:53 am #899200Tom
Lead DeveloperLead DeveloperHmm, 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?
June 10, 2020 at 12:09 am #1321614Zuzana
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,
ZuzanaJune 10, 2020 at 9:46 am #1322440Tom
Lead DeveloperLead DeveloperHi there,
Can you try the updated code?: https://generatepress.com/forums/topic/menu-search-not-working-on-en-website/#post-893040
Let me know π
June 10, 2020 at 9:58 am #1322457Zuzana
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.
-
AuthorPosts
- You must be logged in to reply to this topic.