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

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

    (@realloc)

    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',
    		array(
    			'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
    https://dev.pero.ag/reinigungs_anlagen/waschtisch/
    the URL for the French page provided by language switcher still is:
    https://frdev.pero.ag/reinigungs_anlagen/fontaine-de-nettoyage/
    instead of
    https://frdev.pero.ag/install_de_nettoyage/fontaine-de-nettoyage/

    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
    https://msls.co/developer-docs/hook-reference.html#msls-options-get-permalink 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: https://dev.pero.ag/reinigungs_anlagen/waschtisch/
    Url after replace: https://dev.pero.ag/install_de_nettoyage/waschtisch/&#8221;

    It works different as I would expect: The German (!) url becomes translated to the french slug, but the french slug stays the same:
    https://frdev.pero.ag/reinigungs_anlagen/fontaine-de-nettoyage/

    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.

    Regards
    Reinhard

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