Skip Navigation

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

Problem:
The client is experiencing issues with creating English courses using Learndash on an Italian website. The permalinks for the courses are incorrect and not translated, leading to 404 errors and too many redirects.
Solution:
1. Ensure that Learndash is translated using the WordPress Editor. For more details, visit https://wpml.org/plugin/learndash-lms/.
2. Duplicate the course with WPML and set it as independent. Adjust the permalink settings in WordPress and translate the slug in WPML > Settings > Post Type Translation.
3. Take note, that this issue was related to custom code in the child theme's functions.php file and we provided a workaround for the issue.
5. Verify that the custom post type slug is translated in WPML > Settings > Post Type Translation. For more information, visit https://wpml.org/documentation/getting-started-guide/translating-page-slugs/.

If these steps do not resolve your issue, or if the solution seems outdated or irrelevant to your specific case, we recommend opening a new support ticket. Additionally, please check related known issues at https://wpml.org/known-issues/, verify the version of the permanent fix, and confirm that you have installed the latest versions of themes and plugins. For further assistance, visit our 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 0 replies, has 2 voices.

Last updated by Andreas W. 1 month 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 ) Spanish (Español ) 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 ) Spanish (Español ) 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 ) Spanish (Español ) 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/

November 27, 2024 at 10:27 am #16448602

denyD

I switched the code from staging to live, and there’s only one difference: in the staging environment, when I open a course in English, after /en/ there’s courses, while in the live environment, it still shows i-nostri-corsi. Could you explain why?

November 27, 2024 at 11:00 am #16448757

Andreas W.
Supporter

Languages: English (English ) Spanish (Español ) German (Deutsch )

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

Possibly the custom post type slug has not been translated.

You find the translation at WPML > Settings > Post Type Translation.

The slug translation option needs to be enabled to be able to translate the slug:
https://wpml.org/documentation/getting-started-guide/translating-page-slugs/