Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Chouby

    (@chouby)

    I believe there are 3 steps to make this work:
    1. register the string to store its translation
    2. modify the WordPress rewrite rules to have different rules by language
    3. filter the generated links

    NB: pll__ will always return the current language. For both steps 2 and 3, you will need to use low level functions to translate strings in other languages than the current one. if you have a lot of strings in your strings translations table and a lot of languages, the performance impact can be non negligible (since you will load all strings for all languages instead of only one).

    Thread Starter norboo

    (@norboo)

    Hi!
    Thanks for your reply!

    1.That’s exactly what I did. pll_register_string('Products Slug','products');
    en->products
    zh->产品

    2.How would I do that? It works with everything else, but the “products” segment

    3. How?

    What’s alow level function and how ca I apply it in my case?

    This is the link for my website. I’d really appreciate your help.

    I followed Chouby’s hints and some readings (https://shibashake.com/wordpress-theme/custom-post-type-permalinks-part-2) and ended up with this code that seems to work fine. Anyway it’s not perfect, the same page can be reached both from en/books and en/libri.
    Wish this was handled by polylang itself ??

    ...
    		'has_archive'         => true,
    		'publicly_queryable'  => true,
    		'rewrite'			  => array(),
    		'query_var'			  => true
    	);
    	register_post_type( 'libri', $args );
    }
    
    add_action( 'init', 'my_custom_post_types', 0 );
    
    // translation of CPT slugs
    add_action( 'init', function(){
    	global $wp_rewrite;
    	$wp_rewrite->add_permastruct('libri', '/%customslug%/%postname%', false);
    	add_rewrite_rule('en/books(/[^/]+)?(/[0-9]+)?/?$',
    		'index.php?post_type=libri&name=$matches[1]&page=$matches[2]','top');
    	add_rewrite_rule('libri(/[^/]+)?(/[0-9]+)?/?$',
    		'index.php?post_type=libri&name=$matches[1]&page=$matches[2]','top');
    }, 0);
    
    add_filter('post_type_link', function ($permalink, $post) {
    	global $polylang;
    	$t = $post->post_type;
    	if(empty($lang))
    		switch ($polylang->model->get_post_language($post->ID)->slug . $t) :
    			case "enlibri": $t = 'books'; break;
    			endswitch;
    	return str_replace(	array('%customslug%', '%postname%'),
    				array($t, $post->post_name), $permalink );
    }, 10, 2);
    
    add_filter('post_type_archive_link', function ($permalink) {
    	return = str_replace('/libri/', '/' . __('libri') . '/', $permalink );
    });
    Plugin Author Chouby

    (@chouby)

    Thanks for sharing.
    /en/libri works because it is added by Polylang
    Something like

    add_action('wp_loaded', 'remove_pll_cpt_archives_rewrite_rules', 20);
    function remove_pll_cpt_archives_rewrite_rules() {
    	global $polylang;
    	remove_filter('rewrite_rules_array', array(&$polylang, 'rewrite_rules'));
    }

    should remove the rewrite rules added by Polylang for *all* custom post types archives. This is not tested and at least in Polylang 1.2, it will be better, to use ‘pre_option_rewrite_rules’ rather than ‘wp_loaded’.

    Note for people reading this, the (very convenient) syntax used by @angoo needs PHP 5.3.

    Wish this was handled by polylang itself ??

    That’s in roadmap but dont expect this for soon. At least Polylang 1.2 will go without this feature. Maybe 1.3 or 1.4

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Slug rewrite for Custom Post Types and Custom Taxonomies’ is closed to new replies.