Hi (again ;)),
why would you want to do this?
Please have a look at this article on language negotiation.
Kind regards,
Thorsten
Thks for your link.
Why I would do this ? for me everyone understand english. so if a non french and no english browser user see a link of my website fr.donkeez.com, he have to be redirected to my english version of website. It’s about usability.
The Russian user who click on fr.mywebsite.com should be redirected to my English version of website if I don’t have the russian version. no?
Ah, okay, so you only want to redirect users who don’t have French included in their language set at all. I originally assumed you (also) wanted to redirect users who might speak French, but not as first language (i.e., with a lower priority).
In order to redirect users who do not speak French at all, you could do something like this (assuming your English site’s ID is 1):
add_filter(
'mlp_redirect_url',
function ( $url, $redirect_match, $current_site_id ) {
if ( 'fr-FR' === $redirect_match['language'] ) {
return $url;
}
if ( 1 === $current_site_id ) {
return '';
}
$urls = get_interlinked_permalinks(
get_queried_object_id()
);
$url = ! empty( $urls[1] )
? $urls[1]
: get_site_link( 1 );
return $url;
},
10,
3
);
Kind regards,
Thorsten
In case you already tried the above code, I just updated it. 🙂
I’m struggling in which file I have to put that ?
multilingual-press/inc/functions.php ?
Sorry.
You can paste it into your theme‘s functions.php file.
Or you can create an MU plugin. For this, you just have to create a new file in the mu-plugins folder (e.g., wp-content/mu-plugins/mlp-redirect.php). If the mu-plugins folder doesn’t exist, just create it as well. Paste the following inside the new file:
<?php
add_filter(
'mlp_redirect_url',
function ( $url, $redirect_match, $current_site_id ) {
if ( 'fr-FR' === $redirect_match['language'] ) {
return $url;
}
if ( 1 === $current_site_id ) {
return '';
}
$urls = get_interlinked_permalinks(
get_queried_object_id()
);
$url = ! empty( $urls[1] )
? $urls[1]
: get_site_link( 1 );
return $url;
},
10,
3
);
The advantage of this is that you keep the functionality even when you switch themes.
Kind regards,
Thorsten
Hi,
I do have this error:
Fatal error: Call to undefined function get_interlinked_permalinks() in /home/website/www/wp-content/mu-plugins/mlp-redirect.php on line 15
So I’ve edited to mlp_get_interlinked_permalinks
But then I had Call to undefined function get_site_link() so i put get_site_url instead.
Is that ok ? it seems weird as result
$ curl -I -H “Accept-Language: en” “http://fr.domain.com/” 2>/dev/null
HTTP/1.1 302 Found
Location: Array
$ curl -I -H “Accept-Language: fr” “http://domain.com/” 2>/dev/null
HTTP/1.1 302 Found
Location: http://fr.donkeez.com/?noredirect=fr_FR
$ curl -I -H “Accept-Language: ru” “http://fr.domain.com/” 2>/dev/null
HTTP/1.1 302 Found
Location: Array
The correct code after debugging:
<?php
add_filter(
'mlp_redirect_url',
function ( $url, $redirect_match, $current_site_id ) {
if ( 'fr-FR' === $redirect_match['language'] ) {
return $url;
}
if ( 1 === $current_site_id ) {
return '';
}
$urls = mlp_get_interlinked_permalinks(
get_queried_object_id()
);
$url = ! empty( $urls[1] )
? $urls[1]
: get_site_url( 1 );
return $url['permalink'];
},
10,
3
);
Thanks
Hi giln343,
can you tell me if that solves your question? We would mark this topic as resolved if so.
Kind regards
Robert Windisch