• Resolved aigarinsh

    (@aigarinsh)


    Hi! I have managed to get my custom taxonomy use slug based on language with some rewrite rules, but now the slug in language switcher also gets translated where it shouldn’t and be the one defined for corresponding language.

    For example if I’m in Russian version all my language switcher links are:
    https://mysite.com/меню-категории/lv-submenu-slug/
    https://mysite.com/en/меню-категории/en-submenu-slug/
    https://mysite.com/ru/меню-категории/карта-напитков/
    when they should be:
    https://mysite.com/menu-kategorijas/lv-submenu-slug/
    https://mysite.com/en/menu-categories/en-submenu-slug/
    https://mysite.com/ru/меню-категории/карта-напитков/

    Is there some hook in Polylang where I could fix this. Or may be I’m doing something wrong or somebody has another even better approach to achieve what I want until this plugin will have a feature to translate custom post type and taxonomy slugs.

    Here is my code:

    function setup_cpt() {
      my_cpt_bk_menu();
      my_taxonomies_bk_menu_item();
    }
    add_action( 'init', 'setup_cpt', 0 );
    
    function get_translated_cat_slug() {
      $current_ln = get_bloginfo('language');
      if( empty( $current_ln ) ) return 'menu-categories';
      $slugs = array(
        'lv'    => 'menu-kategorijas',
        'en-US' => 'menu-categories',
        'ru-RU' => 'меню-категории'
      );
      return $slugs[$current_ln];
    }
    
    function my_cpt_bk_menu() {
      $labels = array(
        'name'               => _x( 'Menu', 'post type general name', 'mythemedomain' ),
        'singular_name'      => _x( 'Item', 'post type singular name', 'mythemedomain' ),
        'add_new'            => _x( 'Add New', 'item' ),
        'add_new_item'       => __( 'Add New Item' ),
        'edit_item'          => __( 'Edit Item' ),
        'new_item'           => __( 'New Item' ),
        'all_items'          => __( 'All Items' ),
        'view_item'          => __( 'View Item' ),
        'search_items'       => __( 'Search Items' ),
        'not_found'          => __( 'No items found' ),
        'not_found_in_trash' => __( 'No items found in the Trash' ),
        'parent_item_colon'  => '',
        'menu_name'          => _x( 'Menu', 'post type menu name', 'mythemedomain' )
      );
      $args = array(
        'labels'        => $labels,
        'description'   => 'Holds our menu items',
        'public'        => true,
        'menu_position' => 5,
        'supports'      => array( 'title', 'editor', 'thumbnail' ),
        'has_archive'   => true,
        'rewrite'       => array( 'slug' => 'menu' )
      );
      register_post_type( 'bk_menu', $args );
    }
    
    function my_taxonomies_bk_menu_item() {
      $labels = array(
        'name'              => _x( 'Item Categories', 'taxonomy general name', 'mythemedomain' ),
        'singular_name'     => _x( 'Item Category', 'taxonomy singular name', 'mythemedomain' ),
        'search_items'      => __( 'Search Item Categories' ),
        'all_items'         => __( 'All Item Categories' ),
        'parent_item'       => __( 'Parent Item Category' ),
        'parent_item_colon' => __( 'Parent Item Category:' ),
        'edit_item'         => __( 'Edit Item Category' ),
        'update_item'       => __( 'Update Item Category' ),
        'add_new_item'      => __( 'Add New Item Category' ),
        'new_item_name'     => __( 'New Item Category' ),
        'menu_name'         => __( 'Item Categories' ),
      );
      $cat_slug = get_translated_cat_slug();
      $args = array(
        'labels'        => $labels,
        'hierarchical'  => true,
        'rewrite'       => array( 'slug' => $cat_slug )
      );
      register_taxonomy( 'bk_menu_item', 'bk_menu', $args );
    }
    
    add_action( 'init', 'bk_tax_rewrite' );
    function bk_tax_rewrite() {
      add_rewrite_rule('en/menu-categories/([^/]+)/?', 'index.php?bk_menu_item=$matches[1]', 'top' );
      add_rewrite_rule('ru/меню-категории/([^/]+)/?', 'index.php?bk_menu_item=$matches[1]', 'top');
      add_rewrite_rule('menu-kategorijas/([^/]+)/?', 'index.php?bk_menu_item=$matches[1]', 'top');
    }

    https://www.ads-software.com/extend/plugins/polylang/

Viewing 1 replies (of 1 total)
  • Thread Starter aigarinsh

    (@aigarinsh)

    OK I managed it myself with an ugly solution which wouldn’t work for the main language version where there is no langauge slug if you moved to other country code top-level domain as .lv But for the current situation I can live with that.

    Here is the code:

    add_filter('pll_the_languages', 'bk_ln_slugs', 10);
    function bk_ln_slugs($output) {
      $output = str_replace('.lv/menu-categories','.lv/menu-kategorijas', $output);
      $output = str_replace('.lv/меню-категории','.lv/menu-kategorijas', $output);
      $output = str_replace('en/меню-категории','en/menu-categories', $output);
      $output = str_replace('en/menu-kategorijas','en/menu-categories', $output);
      $output = str_replace('ru/menu-categories','ru/меню-категории', $output);
      $output = str_replace('ru/menu-kategorijas','ru/меню-категории', $output);
      return $output;
    }

Viewing 1 replies (of 1 total)
  • The topic ‘Custom taxonomy rewrite’ is closed to new replies.