Last active
November 1, 2022 12:07
-
-
Save bramchi/d0767c32a772550486ea to your computer and use it in GitHub Desktop.
Roots.io searchform.php template hack to get search widget working in Polylang, only searching within the active language. 'soil-nice-search' disabled btw.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* | |
* Roots.io searchform.php template hack to fix Polylang search | |
* | |
* Note: Polylang setting 'Hide URL language info for default language' should be enabled for this to work. | |
* Soil-nice-search disabled in Roots. | |
* | |
*/ | |
if(function_exists('pll_current_language')){ | |
$current_language = pll_current_language(); | |
$default_language = pll_default_language(); | |
if($current_language != $default_language){ | |
$language_subdir = $current_language.'/'; | |
} else { | |
$language_subdir = ''; | |
} | |
} | |
?> | |
<form role="search" method="get" class="search-form form-inline" action="<?php echo esc_url(home_url('/'.$language_subdir)); ?>"> | |
<label class="sr-only"><?php _e('Search for:', 'roots'); ?></label> | |
<div class="input-group"> | |
<input type="search" value="<?php echo get_search_query(); ?>" name="s" class="search-field form-control" placeholder="<?php _e('Search', 'roots'); ?> <?php bloginfo('name'); ?>"> | |
<span class="input-group-btn"> | |
<button type="submit" class="search-submit btn btn-default"><?php _e('Search', 'roots'); ?></button> | |
</span> | |
</div> | |
</form> |
Just wanted to point out, your checking if a function exists (good) then setting a variable.
Then you use $language_subdir
without checking if it exists, in the case of the function check failing this would be null and depending on your config throw an error.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I just stumbled upon this.
@AlainMelsens, this is actually integrated in polylang since more than 5 years. See the source code here: https://github.com/polylang/polylang/blame/3e4551ef00acce48e2f8199541b46fb72dcf4b34/include/links.php#L53-L56
You can use it like this:
PLL()->links->get_home_url(NULL, TRUE)
. The second argument/parameter is to tell if the URL is for the search form or not.