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.

WordPress 6.7 has introduced a new issue that impact translations, please update WooCommerce and WPML to the latest versions before you report issues. More about this here - https://wpml.org/errata/php-error-wp-6-7-notice-function-_load_textdomain_just_in_time-was-called/
Sun Mon Tue Wed Thu Fri Sat
- - 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00
- - - - - - -

Supporter timezone: America/Lima (GMT-05:00)

Tagged: 

This topic contains 0 replies, has 0 voices.

Last updated by Andreas W. 2 days, 11 hours ago.

Assisted by: Andreas W..

Author Posts
November 18, 2024 at 3:51 pm #16415667

denyD

Hi Andreas, I'm not entirely clear on what you're trying to do, but if this won't cause any issues or blocks on the main site, feel free to proceed as you prefer!

November 18, 2024 at 4:54 pm #16415978

Andreas W.
Supporter

Languages: English (English ) German (Deutsch )

Timezone: America/Lima (GMT-05:00)

I am sorry, but this approach is not working, as it seems that the servers firewall is not allowing me to access the database in this way.

To solve the issue on your staging, please follow this guide:
hidden link

Switching to a default theme or the Astra Parent Theme should solve the issue with the error.

Once the staging site is avaiable again I can offer to have another look at the issue.

November 19, 2024 at 9:43 am #16418441

denyD

I changed the theme, but it wasn't enough to unlock it. I had all the plugins disabled, and now it's working again.

November 19, 2024 at 11:00 pm #16421835

Andreas W.
Supporter

Languages: English (English ) German (Deutsch )

Timezone: America/Lima (GMT-05:00)

Here is an updated function, that will solve the issue:

function change_link_corsi( $permalink, $post ) {
    if ( $post->post_type === 'sfwd-courses' ) {
        $resource_terms = get_the_terms( $post, 'ld_course_category' );
        $term_slug = '';

        // Retrieve the translated slug for the custom post type
        $language_code = apply_filters( 'wpml_current_language', null ); // Get the current language code
        $default_slug = 'i-nostri-corsi'; // Default slug in your primary language
        $translated_slug = apply_filters( 'wpml_get_translated_slug', $default_slug, 'sfwd-courses', $language_code, 'post' );

        // Fallback if translated slug is null or empty
        if ( empty( $translated_slug ) ) {
            $translated_slug = $default_slug;
        }

        $home_url = trailingslashit( apply_filters( 'wpml_home_url', get_home_url() ) );

        if ( ! empty( $resource_terms ) ) {
            foreach ( $resource_terms as $term ) {
                // Skip the 'featured' category to get the main one
                if ( $term->slug === 'featured' ) {
                    continue;
                }
                $term_slug = $term->slug;
                break;
            }
        }

        // Ensure $term_slug has a fallback to avoid double slashes
        if ( empty( $term_slug ) ) {
            $term_slug = ''; // You can assign a default taxonomy slug here if needed
        }

        // Build the permalink with validated components
        $permalink = $home_url . trailingslashit( $translated_slug ) . ( $term_slug ? trailingslashit( $term_slug ) : '' ) . $post->post_name;
    }
    return $permalink;
}
add_filter( 'post_type_link', 'change_link_corsi', 10, 2 );
November 20, 2024 at 9:14 am #16423263

denyD

Hello Andreas, thank you so much. Can I ask if you tried it with all plugins enabled?

November 20, 2024 at 9:34 am #16423421

denyD

Sorry, Andreas, I had the code analyzed by ChatGPT to understand if it could affect my Italian SEO, and this is what it says:

Analyzing the new code provided, it appears to address the issue in a robust manner. However, there are some critical aspects that need to be reviewed to ensure it fully meets your requirements. Let’s break it down:

What the new code does:
Handles the translated slug for the custom post type (sfwd-courses):

It uses the wpml_get_translated_slug filter to fetch the translated slug based on the current language.
It provides a fallback to retain the original slug (e.g., i-nostri-corsi) if a translation is not available.

Builds the permalink:

Combines the translated slug, the category term (ld_course_category), and the post name (post_name).
Avoids errors with fallbacks and sanitization:

Ensures each component of the permalink is properly defined, avoiding malformed URLs or double slashes (//).
Potential issues or uncertainties:

Italian language:

While the code seems to handle Italian correctly, it doesn’t explicitly ensure that the original slug /i-nostri-corsi/ remains unchanged for Italian courses. If wpml_get_translated_slug returns a different slug for Italian, it might unintentionally alter the permalink.

Incomplete slug translation:

The fallback uses $default_slug (i-nostri-corsi) if no translation is found. However, this might not be sufficient in edge cases (e.g., if the translated slug is empty or differs from what you expect).
SEO impact:

The code should preserve existing permalinks in Italian, but if the slug changes for any reason, it could break existing URLs.

Expected outcomes:

If your goal is to:

Italian language: Keep the original /i-nostri-corsi/ slug intact.
Other languages: Translate the slug appropriately based on WPML settings.

The code will work only if:

The Italian slug (i-nostri-corsi) is explicitly preserved.
WPML is correctly configured to handle translations for the other languages

Possible improvements:
To ensure the Italian slug is never altered, you can explicitly enforce it in the code:

if ( $language_code === 'it' ) {
$translated_slug = $default_slug; // Keep the original slug for Italian
} else {
$translated_slug = apply_filters( 'wpml_get_translated_slug', $default_slug, 'sfwd-courses', $language_code, 'post' );
}

This ensures that for Italian, the slug always remains i-nostri-corsi, regardless of WPML’s configuration.

What do you think about it?

November 20, 2024 at 4:35 pm #16425749

Andreas W.
Supporter

Languages: English (English ) German (Deutsch )

Timezone: America/Lima (GMT-05:00)

Yes, this is correct and the findings of ChatGPT appear expected..

If you change the original Italian slug, this snippet will not longer work. Further, it is expected that WPML changes the slug accordingly to use the translated slugs in other languages.

I need to point out again, that I did already way more than expected here.

Custom code requestes are not covered by our support policy and our supporters are not supposed to provide custom code solution.

You could consult ChatGPT about how to obtain the original CPT slug dynamically and also ask it to handle edge cases in which a translation does not exists. It should come up with a working solution.

If you would like someone else to optimize your custom code, then I need to ask you reach out to on of our contractors:
https://wpml.org/contractors/