Skip Navigation

This thread is resolved. Here is a description of the problem and solution.

Problem:
You are trying to redirect incoming links that use the ?lang=en parameter to a specific path, but the system still redirects to the base language despite using custom code.
Solution:
We recommend using a tested and approved redirection plugin that works well with WPML. You can find this plugin here: https://wpml.org/plugin/redirection/
If you need further customization, consider hiring a specialist from our Contractors service, available here: https://wpml.org/contractors/

If this solution does not apply to your case, or if it seems outdated, we highly recommend checking related known issues at https://wpml.org/known-issues/, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. Should you need further assistance, please open a new support ticket at WPML support forum.

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.

Tagged: 

This topic contains 1 reply, has 0 voices.

Last updated by Lucas Vidal de Andrade 2 months ago.

Assisted by: Lucas Vidal de Andrade.

Author Posts
April 9, 2025 at 2:58 pm #16914673

babetter-2

Background of the issue:
I am trying to redirect incoming links that use the ?lang=en parameter to hidden link. I have implemented a code snippet to achieve this, but the system still redirects to the base language if it finds a link with ?lang=en. Here is the code I am using: add_action('init', function () { if (isset($_GET['lang']) && $_GET['lang'] === 'en') { $request_uri = $_SERVER['REQUEST_URI']; $parsed_url = parse_url($request_uri); $clean_path = $parsed_url['path']; if (strpos($clean_path, '/en/') === false) { if (function_exists('icl_register_string')) { do_action('wpml_switch_language', 'en'); } $redirect_url = home_url('/en' . $clean_path); wp_redirect($redirect_url, 301); exit; } } });

Symptoms:
The system still redirects to the base language even when a link with ?lang=en is used.

Questions:
How can I ensure that links with ?lang=en are redirected to /en/ instead of the base language?

April 9, 2025 at 3:29 pm #16914890

Lucas Vidal de Andrade
WPML Supporter since 11/2023

Languages: English (English ) Spanish (Español ) German (Deutsch ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Hey there,

The creation of custom code is unfortunately outside the scope of our support. Nonetheless, we have a recommended redirection plugin that's been thoroughly tested and works well with WPML:

https://wpml.org/plugin/redirection/

Alternatively, you can also check our Contractors service, where you'll find specialized work to help you with your custom code:
https://wpml.org/contractors/

Let me know if you have any other questions.