Support » Plugin: Multisite Language Switcher » Custom Post Type rewrite / translating slug

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Dennis Ploetner


    You would normally translate the label and the slug for a custom post type but not the post_type_key…

    function wporg_custom_post_type() {
    	register_post_type( 'wporg_product',
    			'labels'      => array(
    				'name'          => __( 'Products', 'textdomain' ),
    				'singular_name' => __( 'Product', 'textdomain' ),
    			'public'      => true,
    			'has_archive' => true,
    			'rewrite'     => array(
    				'slug' => _x( 'products', 'slug', 'textdomain' ),
    add_action( 'init', 'wporg_custom_post_type' );

    In my example the key ‘wporg_product’ is the same for all installations.

    Hi Dennis,

    thanks for your reply. I tried what you suggested, on the French site for instance the permalink gets translated in the backend when I open the post there (if the post_type_key stays the same) and now I can assign the translated pages to each other, as the slug key is the same by using the msls-dropdown in the backend of the pages of this post type.

    It also gets translated in the “normal” menu on French Site. But when I click it, the url with translated slug doesn’t work as the translated CPT-slug still doesn’t seem to exist. The untranslated slug in the URL still works though. I tried and resaved permalinks with no effect.

    Secondly: I need the slug translated when I visit the post type in the frontend, switching between languages with your plugin. So when I am on the German site the translated version for the German site should be “reinigungs_anlagen” (stay the same), for English version “cleaning_plants” and for French version “install_de_nettoyage”.

    I tried this on Dev-Server thus you can’t access it (if you like I can send you htaccess credentials via private message, so you can take a look).

    There, if I am on the German version of the page
    the URL for the French page provided by language switcher still is:
    instead of

    But this seems to be logical, as the french translation just comes into play on the French site, not on the German or English one. So this doesn’t work that way.

    I found a hook ‘msls_options_get_permalink’ from your plugin page. Using it as in the example on doesn’t bring me to the solution either.

    function my_msls_options_get_permalink( $url, $language ) {
      $language = get_locale();
      echo "String-Replace for $language<br>";
      echo "Original url: $url<br>";
      if ( 'de_DE' == $language ) {
          $url = str_replace( '/reinigungs_anlagen/', '/reinigungs_anlagen/', $url );
      if ( 'fr_FR' == $language ) {
          $url = str_replace( '/reinigungs_anlagen/', '/install_de_nettoyage/', $url );
      if ( 'en_US' == $language ) {
        $url = str_replace( '/reinigungs_anlagen/', '/cleaning_plants/', $url );
      echo "Url after replace: $url<br>";
      return $url;
    add_filter( 'msls_options_get_permalink', 'my_msls_options_get_permalink', 10, 2 );

    When I read those debugging messages on the French page it gives back:
    “String-Replace for fr_FR (so it will enter the second “if”)
    Original url:
    Url after replace:;

    It works different as I would expect: The German (!) url becomes translated to the french slug, but the french slug stays the same:

    I really don’t understand why and need further assistance on this.

    If you have further questions please let me know. Thanks for your help.


Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom Post Type rewrite / translating slug’ is closed to new replies.