Skip Navigation

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

Problem:
The client wants to translate custom post type (CPT) slugs without using WPML's String Translation, aiming for a cleaner approach while maintaining language-specific URLs (e.g., English: example.com/en/products/, German: example.com/produkte/). They are experiencing issues with 404 errors and incorrect language detection in requests.
Solution:
We recommend using WPML's String Translation for translating CPT slugs as it is a mandatory plugin for such functionality. Ensure that the 'has_archive' is set to true in your code, as shown below:

'has_archive' => true,

WPML supports custom slug translation when the custom type is registered but does not support custom permalink structures. For more details, please refer to our documentation on translating slugs for custom post type archives.
If you require a custom solution, we suggest using the services of WPML contractors, which can be found here: WPML contractors.

Please note that this solution might be outdated or not applicable to your specific case. 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. If the issue persists, 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 Dražen 3 weeks, 3 days ago.

Assisted by: Dražen.

Author Posts
January 16, 2025 at 8:58 am #16600397

Peter

Background of the issue:
My default language is DE, but everything in my theme is essentially set up in EN. I don’t want to rely on WPML String Translation just for the slug, as it feels inconsistent (and would force me to store related strings in DE when the base theme is in EN). Instead, I’d like a cleaner approach, while still having:
• English: example.com/en/products/
• German: example.com/produkte/

Symptoms:
• I’m using WPML but want to avoid installing String Translation just for slug translations.
• I have a code snippet using the register_post_type_args filter to change the CPT slug based on the current language. The URL rewrites correctly (showing /en/products/ or /produkte/), but sometimes I get 404 errors or see that WPML detects multiple languages in one request (log says “Current language: de” then “Current language: en”).
• I want the CPT content to remain translatable (so each post can have an EN and a DE version), but the actual slug rewriting should stay in my own code rather than relying on WPML’s slug translation.

Questions:
Current Approach
1. Filter approach

add_filter('register_post_type_args', function ($args, $post_type) {
if ($post_type === 'product') {
$current_language = apply_filters('wpml_current_language', null);

if ($current_language === 'de') {
$args['rewrite'] = [
'slug' => 'produkte',
'with_front' => false,
];
$args['has_archive'] = 'produkte';
} elseif ($current_language === 'en') {
$args['rewrite'] = [
'slug' => 'products',
'with_front' => false,
];
$args['has_archive'] = 'products';
}
}
return $args;
}, 99, 2);

• CPT is set to “translatable” in WPML, but I’m not using String Translation for slugs.
• I flush permalinks whenever I change anything.
• The slug does update, but I occasionally get 404 errors or the wrong slug in the wrong language.

2. WPML settings
• I have “Language in directories” activated.
• The posts themselves translate fine; only the CPT slug is problematic.

Question for WPML Support
• How can I reliably translate the CPT slug without String Translation while keeping the CPT posts fully translatable in WPML?
• Why might WPML or WordPress see the language as both “de” and “en” in the same request, potentially causing a 404?
• Is there a recommended approach or best practice for using a custom code filter to handle CPT slugs in a WPML setup?

Thank you in advance for any advice or guidance on how to properly integrate custom slug rewrites alongside WPML’s translation flow!

January 17, 2025 at 7:54 am #16604607

Dražen
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+01:00)

Hello,

thanks for contacting us.

I am afraid the only way is as explained in our documentation and then translating from WPML Settings / String translation. Note that string translation is a mandatory plugin, and has archive has set to be true.

'has_archive' => true.

- WPML supports only custom slug translation (when the custom type is registered) but not custom permalink structure.

- https://wpml.org/documentation/support/translating-slugs-for-custom-post-type-archives/

Other then that not much I can suggest from our side, for custom solutions, I suggest using the service of WPML contractors:

- https://wpml.org/contractors/

Regards,
Drazen