How can I rewrite custom post types slugs based on language? How can I rewrite custom post types slugs based on language? wordpress wordpress

How can I rewrite custom post types slugs based on language?


Basically, I think running flush_rewrite_rules() on every page load will let you localize the page slug the way you're intending, however that is an extremely poor solution since it will also write the .htaccess file on each page load which slow things tremendously and potentially giving really weird results once there is more than one user visiting the site. Looking at the documentation it appears you can supply false as the first argument, which disables writing of .htaccess, but this is still not a good solution as the documentation also states the following:

Important: Flushing the rewrite rules is an expensive operation, there are tutorials and examples that suggest executing it on the 'init' hook. This is bad practice. Instead you should flush rewrite rules on the activation hook of a plugin, or when you know that the rewrite rules need to be changed ( e.g. the addition of a new taxonomy or post type in your code ).

Each time I've had this problem myself I have ended up using a permlink structure that works for both languages, but that's not a very good solution either.

A good starting point might be checking out /wp-includes/rewrite.php and see if it is somehow possible to override the permalink for a given post type via filters, and return a slug according to the users language setting. Another option might be to try to add a second permalink for each language, so that each post in your custom post type is available at the permalink specified in register_post_type as well as through a permalink you specify elsewhere. You might be able to do this using WP_Rewrite, or by adding some mod_rewrite directives to you .htaccess. In the latter case, something like this might work:

$data = 'Rewrit­eRule ^/localized-type-name/(.*)$ /type-name/$1 [R,NC,L]';// You might want to do a little more work on the actual rewrite rule, it's very much from the top of my head so it might not even workinsert_with_markers( ABSPATH . '/.htaccess', 'NAME OF YOUR MARKER', $data );$wp_rewrite->flush_rules();

UPDATE: You might want to check out this thread.


It is possible.

Since you use WPML plugin, then php constant I ICL_LANGUAGE_CODE gives the current language. Then, in at the the post time of registering the post type use following code:

 $args = array (    'labels'            => array (        'name'              => _x( 'Destination','post type general name',TT_CPT_DOMAIN ),        'singular_name'     => _x( 'Destination','post type singular name',TT_CPT_DOMAIN ),        'add_new'           => _x( 'Add Destination','post type general name',TT_CPT_DOMAIN ),        'edit_item'         => __( 'Edit Destination',TT_CPT_DOMAIN ),        'not_found_in_trash' => __( 'No Destination found in Trash',TT_CPT_DOMAIN ),        'taxonomies'        => __('post_tag','category'),        'parent_item_colon' => '',        'menu_name'         => __( 'Destination',TT_CPT_DOMAIN )     ),    'has_archive'       => true,    'hierarchical'      => true,    'menu_position'     => 3,    'supports'          => array ('title'),    'menu_icon'         => TT_CPT_PLUGIN_URL. 'images/News.png',      'rewrite'           => array('slug' => ( (ICL_LANGUAGE_CODE=='sv')? 'resor':'reiser' ) ),    'menu_icon'              => TT_CPT_PLUGIN_URL . '/cpts/images/icon_news.png'        );

Notice the line

rewrite'           => array('slug' => ( (ICL_LANGUAGE_CODE=='sv')? 'resor':'reiser' ) ),

You can use similar logic if you handle language on you own or use any other plugin.Good luck!