Skip to content Skip to sidebar

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 11 replies, has 2 voices.

Last updated by Long Nguyen 1 year, 8 months ago.

Assisted by: Long Nguyen.

Author Posts
November 17, 2023 at 9:23 am

corinneV-2

I am encountering a challenge with language switching on my WordPress site, specifically on pages of a custom post type named 'bien'. While the WPML plugin effectively translates the content and changes most URL parts accordingly, it fails to update the language prefixes (like /en, /ru) in the URLs when switching languages on these custom post type pages.

November 17, 2023 at 10:02 am
November 20, 2023 at 2:58 am #14872505

Long Nguyen
WPML Supporter since 02/2022

Languages: English (English )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Hi Corinne,

Thank you for contacting WPML support, I'd be happy to help you with this issue.

I see the language switcher displays the translation slug correctly, just missing the language code in the URL so it redirects to the 404 page. I suggest you add the language code when you translate the slug. For example:

// Modification du lien pour les types de publication 'bien'
function custom_post_type_link($link, $post) {
    if ($post->post_type === 'bien') {
        $station_terms = wp_get_post_terms($post->ID, 'station-de-ski');
        $programme_terms = wp_get_post_terms($post->ID, 'programmes');

        // Récupérer le code de langue actuel
        $current_language = apply_filters('wpml_current_language', NULL);

        $url = home_url('/');

        // Déterminer les slugs en fonction de la langue actuelle
        switch ($current_language) {
            case 'en': // Anglais
                $url .= 'en/'; // add the language code to $url
                $station_slug = 'ski-resort';
                $programme_slug = 'new-programs';
                $bien_slug = 'property';
                break;
            case 'ru': // Russe
                $url .= 'ru/'; // add the language code to $url
                $station_slug = 'лыжный-курорт';
                $programme_slug = 'новые-программы';
                $bien_slug = 'свойство';
                break;
            default: // Langue par défaut (Français)
                $station_slug = 'station-de-ski';
                $programme_slug = 'programme-neuf';
                $bien_slug = 'bien';
                break;
        }

        // Construire l'URL selon les termes disponibles et la langue

        if ($station_terms && !is_wp_error($station_terms)) {
            $url .= $station_slug . '/' . $station_terms[0]->slug . '/';
        }
        if ($programme_terms && !is_wp_error($programme_terms)) {
            $url .= $programme_slug . '/' . $programme_terms[0]->slug . '/';
        }
        $url .= $bien_slug . '/' . $post->post_name . '/';

        return $url;
    }
    return $link;
}
add_filter('post_type_link', 'custom_post_type_link', 10, 2);

If it does not help, please try to replicate the issue on a clean installation of WordPress. I will help you to check the issue. With this link, you can login directly to the admin area hidden link

Kindly follow the steps below:

- Don’t restore your site there.
- Try to replicate the issue with minimal steps.
- Then let me know the steps to replicate it.

Look forward to your reply.
Thanks

November 20, 2023 at 7:57 am #14873553

corinneV-2

Good morning,
I still have an error but different this time:

if you go to this url;
hidden link
and you try to language witcher on english or russian the url becomes
hidden link

the /en or /ru is added twice (example: en/en/ski-resort/courchevel/new-programs/la-calinette/property/3-rooms-courchevel-1650/)

I don't need to have it twice

November 20, 2023 at 8:53 am #14874033

Long Nguyen
WPML Supporter since 02/2022

Languages: English (English )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Hi,

Please try to replicate the issue on the sandbox site that I shared above, I will take a closer look.

Thanks.

November 20, 2023 at 9:28 am #14874371

corinneV-2

i préfer to give you an access in my dashboard

November 20, 2023 at 9:38 am #14874437

corinneV-2

tell me where can i give you the access

November 20, 2023 at 9:54 am #14874773

corinneV-2

This is my actualy code in my functions.php

/*REECRITURE DES URL POUR LES BIEN*/
function custom_rewrite_rule() {
// Obtenir les langues disponibles via WPML
$languages = apply_filters('wpml_active_languages', NULL, 'orderby=id&order=desc');

// Boucle sur chaque langue et créer des règles de réécriture spécifiques
foreach ($languages as $lang_code => $lang) {
// Traductions des slugs en fonction de la langue
switch ($lang_code) {
case 'en': // Anglais
$programmes_slug = 'programs';
$station_slug = 'ski-resort';
$programme_neuf_slug = 'new-programs';
$bien_slug = 'property';
break;
case 'ru': // Russe
$programmes_slug = 'Программы';
$station_slug = 'лыжный-курорт';
$programme_neuf_slug = 'новые-программы';
$bien_slug = 'свойство';
break;
default: // Langue par défaut (Français dans votre cas)
$programmes_slug = 'programmes';
$station_slug = 'station-de-ski';
$programme_neuf_slug = 'programme-neuf';
$bien_slug = 'bien';
break;
}

// Ajouter des règles de réécriture pour chaque langue
add_rewrite_rule(
$station_slug . '/([^/]+)/' . $programmes_slug . '/([^/]+)/' . $bien_slug . '/([^/]+)/?$',
'index.php?station-de-ski=$matches[1]&programmes=$matches[2]&bien=$matches[3]',
'top'
);
add_rewrite_rule(
$station_slug . '/([^/]+)/' . $programme_neuf_slug . '/([^/]+)/' . $bien_slug . '/([^/]+)/?$',
'index.php?station-de-ski=$matches[1]&programmes=$matches[2]&bien=$matches[3]',
'top'
);
add_rewrite_rule(
$station_slug . '/([^/]+)/' . $bien_slug . '/([^/]+)/?$',
'index.php?station-de-ski=$matches[1]&bien=$matches[2]',
'top'
);
add_rewrite_rule(
$programme_neuf_slug . '/([^/]+)/' . $bien_slug . '/([^/]+)/?$',
'index.php?programmes=$matches[1]&bien=$matches[2]',
'top'
);
}

// Ajouter des paramètres de requête personnalisés
add_filter('query_vars', function ($vars) {
$vars[] = 'bien';
$vars[] = 'station-de-ski';
$vars[] = 'programmes';
return $vars;
});
}
add_action('init', 'custom_rewrite_rule', 10, 0);

// Modification du lien pour les types de publication 'bien'
function custom_post_type_link($link, $post) {
if ($post->post_type === 'bien') {
$station_terms = wp_get_post_terms($post->ID, 'station-de-ski');
$programme_terms = wp_get_post_terms($post->ID, 'programmes');

// Récupérer le code de langue actuel
$current_language = apply_filters('wpml_current_language', NULL);

$url = home_url('/');

// Déterminer les slugs en fonction de la langue actuelle
switch ($current_language) {
case 'en': // Anglais
$url .= 'en/'; // add the language code to $url
$station_slug = 'ski-resort';
$programme_slug = 'new-programs';
$bien_slug = 'property';
break;
case 'ru': // Russe
$url .= 'ru/'; // add the language code to $url
$station_slug = 'лыжный-курорт';
$programme_slug = 'новые-программы';
$bien_slug = 'свойство';
break;
default: // Langue par défaut (Français)
$station_slug = 'station-de-ski';
$programme_slug = 'programme-neuf';
$bien_slug = 'bien';
break;
}

// Construire l'URL selon les termes disponibles et la langue

if ($station_terms && !is_wp_error($station_terms)) {
$url .= $station_slug . '/' . $station_terms[0]->slug . '/';
}
if ($programme_terms && !is_wp_error($programme_terms)) {
$url .= $programme_slug . '/' . $programme_terms[0]->slug . '/';
}
$url .= $bien_slug . '/' . $post->post_name . '/';

return $url;
}
return $link;
}
add_filter('post_type_link', 'custom_post_type_link', 10, 2);

November 20, 2023 at 10:18 am #14874911

Long Nguyen
WPML Supporter since 02/2022

Languages: English (English )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Hi,

I would like to request temporary access (wp-admin and FTP) to your site to take a better look at the issue. It would be better to have a testing site where the issue is replicated. Your next reply is set to private to share the info.

❌ IMPORTANT: Please backup your database and website before proceeding ❌
You can use this plugin to create the backup: https://wordpress.org/plugins/duplicator/
✙ I would need your permission to de-activate and re-activate Plugins and the Theme to change configurations on the site if needed. This is also a reason the backup is critical.

Look forward to your reply.
Thanks

November 20, 2023 at 11:09 am
November 20, 2023 at 12:30 pm #14876727

corinneV-2

Can you tel me if you need something !

November 21, 2023 at 2:33 am #14881525

Long Nguyen
WPML Supporter since 02/2022

Languages: English (English )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Hi,

The function home_url() returns the site URL including the current language code so I changed it to site_url() and now the language switcher works correctly.

$url = site_url('/');

Here is the screen record hidden link

Look forward to your reply.
Thanks

November 21, 2023 at 7:17 am #14882199
corinneV-2

Oh, thank you very much. i'ts perfect.

maybe you can help me for an other litle probleme.

in my footer I have a js script which adds the country codes to the contact form.
but on translated pages it doesn't work.
if you look here on the French version you can see at the bottom of the page on the contact form the telephone country code field:
hidden link
but if you go to the English or Russian version by switching the language the script doesn't work.

can you tell me why and how I fix this?

New threads created by Long Nguyen and linked to this one are listed below:

https://wpml.org/forums/topic/the-script-doesnt-work-on-the-translation-page/

November 22, 2023 at 1:43 am #14892395

Long Nguyen
WPML Supporter since 02/2022

Languages: English (English )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Hi,

We can handle one issue per ticket only so I will split your new issue with the JavaScript code on the translation page into a new ticket. If you see the main issue of this ticket with the custom post type slug is resolved, please mark it as Resolved.

Thanks.