Polylang redirects to home page when it's not able find a translation for current page. Following code will enable it to stay on the same page but still to update the language cookie.
Refresh instead of Redirection
Path
./plugins/polylang/include/switcher.php
Original Code
$url = empty( $url ) || $args['force_home'] ? $links->get_home_url( $language ) : $url; // If the page is not translated, link to the home page
Replace with
$url = $args['force_home'] ? $links->get_home_url( $language ) : $url;
if ($url = empty( $url )) {
$terminal_url = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$url = $terminal_url;
}
Force updating cookie
if (document.getElementsByClassName("no-translation")[0]){
document.getElementsByClassName("no-translation")[0].firstElementChild.addEventListener("click",update_language_cookie);
}
function update_language_cookie(){
switch(document.getElementsByClassName("no-translation")[0].firstElementChild.getAttribute("lang"))
{
case 'zh-CN':
writeCookie('zh', 'pll_language');
break;
case 'en-US':
writeCookie('en', 'pll_language');
break;
default:
}
}
function writeCookie(value, name, key) {
var Days = 365;
var exp = new Date();
exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
if (key == null || key == "") {
document.cookie = name + "=" + encodeURI(value) + ";expires=" + exp.toGMTString()+";path=/";
}
else {
var nameValue = getCookie(name);
if (nameValue == "") {
document.cookie = name + "=" + key + "=" + encodeURI(value) + ";expires=" + exp.toGMTString() + ";path=/";
}
else {
var keyValue = getCookie(name, key);
if (keyValue != "") {
nameValue = nameValue.replace(key + "=" + keyValue, key + "=" +encodeURI ( value));
document.cookie = name + "=" + nameValue + ";expires=" + exp.toGMTString() + ";path=/";
}
else {
document.cookie = name + "=" + nameValue + "&" + key + "=" + encodeURI(value) + ";expires=" + exp.toGMTString() + ";path=/";
}
}
}
}
Force delete translation designation
# find designation
SELECT `object_id`, `wp_term_relationships`.`term_taxonomy_id`
INTO @object_id, @term_taxonomy_id
FROM `wp_term_taxonomy`
INNER JOIN `wp_term_relationships`
ON `wp_term_taxonomy`.`term_taxonomy_id` = `wp_term_relationships`.`term_taxonomy_id`
WHERE `taxonomy` LIKE 'language' && `object_id` = [id];
# delete designation
DELETE FROM `wp_term_relationships`
WHERE `object_id`=@object_id && `term_taxonomy_id` = @term_taxonomy_id;
# update language page count
UPDATE `wp_term_taxonomy` SET `count` = `count`-1 WHERE `term_taxonomy_id` = @term_taxonomy_id;
Query All Languages
functions.php
/*====== Altering the main query with query_posts() ======*/
add_action('pre_get_posts','alter_query');
function alter_query($query) {
//gets the global query var object
global $wp_query;
if ( !$query->is_main_query() )
return;
$query-> set('tax_query','');
}
Reference
js 操作cookie cookie路径问题
Three techniques to alter the query in WordPress
License
Polylang is licensed under GPL V2 + or GPL V3+ licence, so is my work. See
https://polylang.pro/terms/.