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

Last updated by Bruno 1 year, 9 months ago.

Assigned support staff: Bruno.

Author Posts
June 5, 2018 at 10:28 am #2274148

kristofV-3

In my website I am using WPML for translations. So my website's main language is English, and I am rewrite the permalinks of my cpt (artikelen) to website.com/postname/post-id. This works perfectly for the standard language, but when I use my language switcher to go to French for example, I get a 404 error. This is because the /fr/ is not used in the permalink anymore, because of the rewrite rule in my functions.php. So for another language the permalink for my custom post type should be website.com/fr/postname/post-id. Or for dutch: website.com/nl/postname/post-id. Is there any way to include this in the following code?

add_filter('post_type_link', 'wpse33551_post_type_link', 1, 3);

function wpse33551_post_type_link( $link, $post = 0 ){
    if ( $post->post_type == 'artikelen' ){
        return home_url( $post->post_name .'/'. $post->ID );
    } else {
        return $link;
    }
}

add_action( 'init', 'wpse33551_rewrites_init' );

function wpse33551_rewrites_init(){
    add_rewrite_rule(
        '^([^/]+)/([0-9]+)/?$',
        'index.php?post_type=artikelen&p=$matches[2]',
        'top'
    );
}
June 6, 2018 at 8:41 pm #2310120

Bruno
Supporter

Languages: English (English ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Thank you for contacting us. Please, my sincere apologies for the delay in answering.

Please as a test, could you disable all non-WPML plugins and see if the problem persists? Do you want to make translations in both languages?

I'm not sure if it's necessary to use the code you're using above, as WPML already allows you to have the translation in both languages.

Please see the links below that explain a bit about how to make your CPT translatable if this helps to solve the problem you are facing:

- https://wpml.org/faq/how-to-translate-custom-types/
- Look at the (How to create “Custom Post Types”?): https://wpml.org/2016/08/how-to-create-and-translate-custom-post-types/

If this is not what you are asking, can you please elaborate a little more so I can better understand? Sometimes screenshots illustrating what you mean may go a long way to clarify what words fail to transmit so if you like make use of "upload an image" below the comment area.

Thank you.

June 7, 2018 at 7:49 am #2386162

kristofV-3

Hey Bruno,

Thank you for your response. I deactivated all plugins, saved my permalinks again but It is still the same. But allow me to explain my problem a little further.

I want the permalinks/url's of my custom post type (artikelen) to look like this:

hidden link

-> website/post-name/post-id.

So to achieve this I am using the code I mentioned above. And after saving my permalinks, this is working fine for my default language (which is English). But when I want to go to a different language for example French, I get a 404 error. This is because the permalink for my CPT's in my other languages look also look like this:

website.com/fr/closet/1000

-> website/language/post-name/post-id.

But because of my code, the rewrite rule, my permalinks are forced to: website/post-name/post-id. In other words, removing the language parameter (/fr/ or /nl/) for my secondary languages.

So my code should be tweaked that it doesn't remove the language parameters for my secondary languages. But this is where I get stuck because I don't know how to.

But here comes the strangest part: When I go to an archive page (which is nut submitted to the rewrite rule) of my secondary language , and then click through to a cpt detail page, there is no 404 error and everything works fine. So in other words, When I go to a dpt detail page in my primary language, and try to use the language switcher in my navigation to go to the french version of this cpt detail page, I get a 404 error. BUT, when I first go to the archive page of my secondary language and click to continue to a cpt detail page, I go to the correct page without getting 404 troubles.

I hope this explains the problem better? If not please tell me.

Thank you in advance

Kind regards
Alexander Pauwelyn

June 7, 2018 at 11:00 pm #2398407

Bruno
Supporter

Languages: English (English ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Hi Alexander,

Thank you for the explanation. From what I understand, for this to work in the secondary language, as you're customizing the permalinks, it should be adding one more add_rewrite_rule function.

Something like that:

    // default language
    add_rewrite_rule(
        '^([^/]+)/([0-9]+)/?$',
        'index.php?post_type=artikelen&p=$matches[2]',
        'top'
    );

    // secondary language
    add_rewrite_rule(
        '^([^/]+)/([^/]+)/([0-9]+)/?$',
        'index.php?post_type=artikelen&p=$matches[3]&lang=$matches[1]',
        'top'
    );

I'm not sure why this is not working as you want, but maybe using two add_rewrite_rule help to do what you want.

Please let me know if this helps.

Thank you.

June 8, 2018 at 7:49 am #2399011

kristofV-3

Hey Bruno,

Thank you for your answer. Unfortunately it didn't solve the problem...

But here is something else I noticed yesterday: It is only in the language switcher that it goes wrong. If I go to the home page of my secondary language and then navigate to the archives page and then to a cpt detail page, I don't get a 404 error and I am sent to the correct page. But if I go to a cpt detail page in my primary language and then want to go to a cpt detail in one of the secondary languages, I get the 404 error...

Kind regards
Alexander

June 9, 2018 at 4:21 am #2401536

Bruno
Supporter

Languages: English (English ) Portuguese (Brazil) (Português )

Timezone: America/Sao_Paulo (GMT-03:00)

Hi Alexander,

In the case of the language selector, as you are using a different structure of permalinks (custom permalinks), you must create a new language selector.

You can see step by step in our documentation:
- https://wpml.org/documentation/getting-started-guide/language-setup/custom-language-switcher/
- https://wpml.org/tutorials/language-switchers/

Another way is to try changing the URLs using the WPML filter "wpml_ls_language_url". This way you can create custom links for this new permalink structure.

As this is about custom coding, unfortunately, is beyond the scope of our support.

You can also see a list of available hooks, which may help in the development of the requested feature: https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/

If you need custom programming work for your site, we recommend contacting one of our certified partners: http://wpml.org/documentation/support/wpml-contractors/

If creating a new version of the language selector does not work too, please let me know.

Thank you.