How can I rewrite custom language based message types?

I am building a website with WordPress, using it more like a CMS and then a blogging platform. I have used many custom post types and custom taxonomies. Last but not least, I made it multilingual using the WPML plugin.

In CPT declarations, I wrapped the lines and bullets in gettext so that they are translated into WPML.

An example CPT ad is as follows:

 register_post_type('rooms', array( 'label' => __('Rooms','valletta'), 'description' => __('','valletta'), 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'capability_type' => 'post', 'hierarchical' => true, 'rewrite' => array('slug' => __('rooms','valletta')), 'query_var' => true, 'exclude_from_search' => false, 'menu_position' => 25, 'supports' => array('title','editor','excerpt','custom-fields','comments',), 'taxonomies' => array('features','typology',), 'labels' => array ( 'name' => __('Rooms','valletta'), 'singular_name' => __('room','valletta'), 'menu_name' => __('Rooms','valletta'), 'add_new' => __('Add room','valletta'), 'add_new_item' => __('Add New room','valletta'), 'edit' => __('Edit','valletta'), 'edit_item' => __('Edit room','valletta'), 'new_item' => __('New room','valletta'), 'view' => __('View room','valletta'), 'view_item' => __('View room','valletta'), 'search_items' => __('Search Rooms','valletta'), 'not_found' => __('No Rooms Found','valletta'), 'not_found_in_trash' => __('No Rooms Found in Trash','valletta'), 'parent' => __('Parent room','valletta'), ) ) ); 

As you can see, I also wrapped the term slug, and this (for me) makes sense. What I wanted to achieve was that when the user visits the β€œstandard” website in Italian, he will reach the CPT page in the room via / camere / nome -camera /, while the English user will receive it / rooms / room -name /. This works well for Italian, and WPML translates slug correctly for the English version, so if I look at the CPT number on the Italian website and I switch to the English version, WordPress is ported to / rooms / with the only problem, shows 404.

I checked the WPML website and they acknowledge that the translation of slugs is still ongoing, and that is the functionality that is likely to be released with the next version of the plugin. Ok, that's fine.

What I'm trying to achieve is a hack, and WPML updates are a plugin. My idea is to have some custom rewrite rules in my .htaccess and rewrite the URL of these CPT pages. I thought that I could remove the slug translation to have one slug for both languages, and then have a rewrite rule that when someone enters a translated slug, is β€œstandard” instead, but at least the content is correct , and: a) the user will not notice anything, since the browser does not redirect the page, but simply rewrites the URL, and b) nothing changes for WP (or is it so?).

So the question is: how can I do all this? Removing the translation of the plugins is okay, just deleting it on the WPML server, but it’s not very convenient for me to rewrite the rules and the like, so I'm looking for someone who could help me achieve my goal and, ultimately, show the best way to achieve that what I need (and, of course, to have feedback if my idea makes sense or not).

Sorry if I missed something. If you need more information, I am here to give them.

+4
source share
2 answers

Basically, I think running flush_rewrite_rules() every time the page loads will allow you to localize the slug of the page as you plan, but this is a very bad solution, because it will also write a .htaccess file every time the page loads, which slows down and potentially gives really strange results when more than one user visits the site. Looking at the documentation , you can specify false as the first argument that disables the .htaccess entry, but this is still not a good solution, as the documentation also states the following:

Important: Flushing rewrite rules is an expensive operation, there are tutorials and examples that suggest executing it in the 'init' hook. This is bad practice. Instead, you should reset the rewrite rules to the activation hook of the plugin, or when you know that rewrite rules must be changed (for example, adding a new taxonomy or post, enter your code).

Every time I had this problem, I ended up using a permlink structure that works for both languages, but this is also not a good solution.

A good starting point would be to check /wp-includes/rewrite.php and see if it is possible to somehow override the permalink for a given message type through filters and return slug in accordance with the user's language settings. Another option would be to try to add a second permalink for each language so that each post in your custom post type is accessible via the permalink specified in register_post_type , as well as the permalink that you specify elsewhere. You could do this using WP_Rewrite , or by adding some mod_rewrite .htaccess directives to you. In the latter case, something like this may occur:

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

UPDATE You might want to check out this thread .

+1
source

Maybe.

Since you use the WPML plugin, the php constant i ICL_LANGUAGE_CODE provides the current language. Then, during the time after registering the message type, use the 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' ); 

Pay attention to the line

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

You can use similar logic if you process the language yourself or use any other plugin. Good luck

+1
source

Source: https://habr.com/ru/post/1442683/


All Articles