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.

Sun Mon Tue Wed Thu Fri Sat
10:00 – 14:00 10:00 – 14:00 10:00 – 14:00 10:00 – 14:00 10:00 – 14:00 - -
16:00 – 20:00 16:00 – 20:00 16:00 – 20:00 16:00 – 20:00 16:00 – 20:00 - -

Supporter timezone: Asia/Jerusalem (GMT+03:00)

Tagged: 

This topic contains 9 replies, has 2 voices.

Last updated by Itamar 11 months, 1 week ago.

Assisted by: Itamar.

Author Posts
June 25, 2024 at 4:30 pm #15823068

nunoA-8

Background of the issue:
I have a rewrite rule for my custom post type archive, which results in something like this: /events/2024/06. However, the assigned translated page is not /pt-pt/events/2024/06, as expected, but instead /pt-pt/2024/06, which breaks functionality. This is the code I have:

// Add your language prefix detection logic
function get_language_prefix()
{
$current_language = apply_filters('wpml_current_language', null);
$default_language = apply_filters('wpml_default_language', null);

// Check if current language is not the default one
if ($current_language && $current_language !== $default_language) {
return $current_language . '/';
}

return '';
}

// Register rewrite rules with WPML language prefix
function custom_events_rewrite_rules($rules)
{
$prefix = get_language_prefix();

$new_rules = array(
$prefix . 'events' . '/soon/?$' => 'index.php?post_type=event&soon=true',
$prefix . 'events' . '/([0-9]{4})/?$' => 'index.php?post_type=event&year=$matches[1]',
$prefix . 'events' . '/([0-9]{4})/([0-9]{1,2})/?$' => 'index.php?post_type=event&year=$matches[1]&monthnum=$matches[2]',
$prefix . 'events' . '/([0-9]{4})/([0-9]{2})/([0-9]{1,2})/?$' => 'index.php?post_type=event&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]',
);

return $new_rules + $rules;
}
add_filter('rewrite_rules_array', 'custom_events_rewrite_rules');

Symptoms:
The assigned translated page is not /pt-pt/events/2024/06, as expected, but instead /pt-pt/2024/06, which breaks functionality.

Questions:
Why is the translated page not respecting the rewrite rules?
How can I ensure the translated page follows the correct rewrite rule format?

June 25, 2024 at 5:02 pm #15823320

Itamar
WPML Supporter since 02/2016

Languages: English (English )

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

Hi,

I'll get back to you once I have news from our second-tier supporters.

I appreciate your patience.
Itamar.

June 27, 2024 at 10:43 am #15843127

Itamar
WPML Supporter since 02/2016

Languages: English (English )

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

Hi,

This is the reply from our second-tier supporter. He is referring to your code.

I don't think that it will work like that. If your admin language differs from the one detected by the logic, it will return a bad permalink structure when you update the permalinks.

Does this help you in any way?

If it does not help, we must investigate this issue further on a sandbox site. Our second-tier supporter mentions that you have a pretty advanced logic in your code, and just from the information we have, it is hard to know why it is not working.

Regards,
Itamar.

June 28, 2024 at 9:43 am #15850863

nunoA-8

I originally had this function to create an archive for my custom post type based on the date of the event (a custom field, not the published date):

// Register rewrite rules
function custom_events_rewrite_rules($rules)
{
    $new_rules = array(
        'events/soon/?$' => 'index.php?post_type=event&soon=true',
        'events/([0-9]{4})/?$' => 'index.php?post_type=event&year=$matches[1]',
        'events/([0-9]{4})/([0-9]{1,2})/?$' => 'index.php?post_type=event&year=$matches[1]&monthnum=$matches[2]',
        'events/([0-9]{4})/([0-9]{2})/?$' => 'index.php?post_type=event&year=$matches[1]&monthnum=$matches[2]',
        'events/([0-9]{4})/([0-9]{2})/([0-9]{1,2})/?$' => 'index.php?post_type=event&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]',
    );

    return $new_rules + $rules;
}
add_filter('rewrite_rules_array', 'custom_events_rewrite_rules');

this allows me to generate urls like

site.com/events/2024/06

. the thing is that WPML is not respecting the URL when linking the translated page, it's losing the

/events/

portion of the URL, resulting in

site.com/pt-pt/2024/06

What more info can I provide?

June 28, 2024 at 11:55 am #15851652

Itamar
WPML Supporter since 02/2016

Languages: English (English )

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

Hi,

We need to replicate this issue on a fresh WordPress installation. Then, I'll be able to escalate it to our second-tier supporters team. For this, I created a test website with a clean WordPress install. You can access it through this link:

hidden link

With this link, you'll be directly logged in.

Please add your custom code to the sandbox site and see if the issue is replicable on a clean WordPress installation.

Please configure WPML as set on your site (just the basic configurations needed to replicate the issue are enough).

Please let us know when everything is finished, and you can replicate the problem. Please also explain to us where we can find your code.

Important! Do not import your site to the test site. We must replicate the problem on a fresh, clean WordPress installation.

Regards,
Itamar.

July 1, 2024 at 9:46 am #15867486

nunoA-8

Hi Itamar,

I've just added the basics to showcase the problem (the custom post type and the rewrite rules). You can find the rules at the end of the functions.php file

You can see here: hidden link or here: hidden link that the issue remains.

July 2, 2024 at 9:21 am #15873791

Itamar
WPML Supporter since 02/2016

Languages: English (English )

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

Thanks for replicating the issue on the sandbox site.

I escalated this to our second-tier supporters. I'll keep you updated here on any news regarding this issue.

I appreciate your patience.

Itamar.

July 8, 2024 at 11:26 am #15912632

nunoA-8

Hi Itamar, any updates regarding this issue?
Thanks.

July 9, 2024 at 7:43 am #15918636

Itamar
WPML Supporter since 02/2016

Languages: English (English )

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

Hi,

I have no update about it. I pinged our second-tier supporters and told them you were asking about it. I'll keep you updated here.

I appreciate your patience.
Itamar.

July 10, 2024 at 10:33 am #15927605

Itamar
WPML Supporter since 02/2016

Languages: English (English )

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

Hi,

The following is a "quick and dirty" workaround specifically for this case our second-tier supporter drafted.

function custom_event_month_link($monthlink, $year, $month) {
	if (is_post_type_archive('event') || is_singular('event') || is_tax('event_category')) {
		$monthlink = home_url(user_trailingslashit("events/{$year}/" . zeroise($month, 2)));
	}
	return $monthlink;
}
add_filter('month_link', 'custom_event_month_link', 10, 3);

Our second-tier supporter has escalated this issue to our developers. However, please be aware that we can't guarantee that this case will be resolved as it involves pretty advanced custom code.

We will update you with any news from our developers.

Thank you for your patience.
Itamar.