Skip Navigation

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 3 replies, has 2 voices.

Last updated by mariaA-45 1 year, 11 months ago.

Assisted by: Eran Helzer.

Author Posts
July 9, 2023 at 10:16 am #13972553

mariaA-45

Tell us what you are trying to do?
My sites runs with Danish as main language and English as Translation. WPML is configured to use subfolders for languages, also the main language (Danish). Currently, for URLs that do not start with "DeveloBaby.com/en" or "DeveloBaby.com/da" (and don't exist), the Danish 404 page is shown. I would like for that to be changed to the English translation. Is that possible (URLs that start with "DeveloBaby.com/da" should their their Danish 404-page).

Example
"DeveloBaby.com/foobar" should return "404 – We looked everywhere!" (instead of the Danish version of that message).

What is the link to your site?
DeveloBaby.com

July 9, 2023 at 12:24 pm #13972733

Eran Helzer
Supporter

Languages: English (English ) Hebrew (עברית )

Timezone: Asia/Jerusalem (GMT+03:00)

Hi,

Using WPML there is no way to configure different 404 pages per language, according to the url, however that can be achieved with some little custom code.

For example, you can use the WPML action `wpml_switch_language` in your 404 page template to switch languages based on any custom login you want, for example urls containing '/en/'.

A general example of this idea is adding this code to the 404.php file (TwentyTwentyOne theme):

if (str_contains($_SERVER['REQUEST_URI'], '/es/')) {
    do_action( 'wpml_switch_language', 'es' );
}

Take into account that this does not mean we take responsibility for this custom code and will not provide support for it, this is merely a friendly suggestion on one way you can achieve what you want.

Additionally, you can make use of our contractor program to hire an experienced developer to do this job, here:
https://wpml.org/contractors/

Please tell me if this answers your question or if you have any more related to this topic.

July 9, 2023 at 7:49 pm #13973469

mariaA-45

Hi Eran,

thanks for your response. You misunderstood my question (a bit), but your response was golden anyway. Thanks!

I use the Kadence Theme and thus a Kadence Element to customize the content of my 404 page. Inside the Kadence Element, I wrap that custom content in a vanilla-Wordpress-reusable-Gutenberg-block to be able to use WPMLs Automatic Translation Editor to translate my custom 404 page. This way, I already have a Danish and a English 404 page. Nothing magic.
The problem, I still had, was that I wanted to control, which language the 404-page is shown in, if it is not clear from the URL, which language the visitor is browsing my website in. The default behaviour is that the 404-page is shown in the main-language, which is Danish in my case, but I want English instead. I changed your code example a bit to define the desired behaviour and quote it here for others for inspiration:

function bofy_pre_handle_404($preempt, $wp_query) {
	if ( !str_contains($_SERVER['REQUEST_URI'], '/en/')  && !str_contains($_SERVER['REQUEST_URI'], '/da/') ) {
		do_action( 'wpml_switch_language', 'en' );
	}
	return $preempt; 
}
add_filter( "pre_handle_404", "bofy_pre_handle_404", 10, 2 );

The code simply checks, whether the request URL that provoked the 404 contains a language-specific subfolder ("/da/" or "/en/") and, if not, forces English as language. The rest is default 404-handling.

Thanks again, Eran, for pointing my nose!

Best,
Maria