Development

Solutions: WordPress + MultiSite + WPML + Simplied folder structure

THE QUESTION

A while back a potential customer asked me, if it is possible to restructure a WordPress Multisite setup and WPML with a more simplified and custom url structure?


THE ROUGH IDEA

1 . BASE.website web.site (with possibly different languages)

web.site/de/
web.site/en/

2. SUB.website web.site/nl-nl/

Languages would normally be added like this:

web.site/nl-nl/de/
web.site/nl-nl/en/

The customer wanted it to be restructured / simplified like this:

web.site/de-nl/
web.site/en-nl/

This basically mimics the structure of a single WPML website with custom languages, but with all the benefits of a multisite.

THE SOLUTION

This is nothing that WPML or WordPress Multisite provides out of the box.
I built a prototype setup to make it work.

Not something that I would propose for anyone, as it requires a lot of tweaks for anything that handles dynamic links (plugins, hooks, core systems, page.builder …)

Its doable :)

BASIC URL HANDLING

One thing that needs to be tweaked globally, is the mapping of the new url structure.

So web.site/nl-nl/en/ needs to become web.site/en-nl/

This needs to be handled on the server side, by proxying the original to the new structure.
This can be easily done using Apache or NGINX.

With that web.site/nl-nl/en/ will be proxied to web.site/en-nl/, but any core navigation will not work yet.

This is the fastest solution that I came up with, within the hour I gave myself ;)

There surely are other options, like the core rewrites / restructuring of the core shorturl handling. But these approaches might break things in far more areas.

Using the proxy approach, keeps the core as it is. The solution needs to be as simple as possible, allowing to maintain it in the future :)

HOOKS TO THE RESCUE

Just for the basic setup a couple of hooks are required to make this work, more might be needed depending on the plugins in use.

Here a couple of examples ….
WordPress site_url

  1. add_filter( 'site_url', 'custom_site_url' );
  2.  
  3. function custom_site_url( $url )
  4. {
  5.     if( is_admin() ) // you probably don't want this in admin side
  6.         return $url;
  7.  
  8.     return  str_replace( "/nl-nl/en/","/en-nl/", $url);
  9. }
  10.  

WordPress Nav Links

  1. add_filter( 'post_link',  'changePermalinks', 10, 3);
  2. add_filter( 'page_link', 'changePermalinks', 10, 3);
  3. add_filter( 'post_type_link',  'changePermalinks', 10, 3);
  4. add_filter( 'category_link',  'changePermalinks', 11, 3);
  5. add_filter( 'tag_link',  'changePermalinks', 10, 3);
  6. add_filter( 'author_link',  'changePermalinks', 11, 3);
  7. add_filter( 'day_link',  'changePermalinks', 11, 3);
  8. add_filter( 'month_link','changePermalinks', 11, 3);
  9. add_filter( 'year_link',  'changePermalinks', 11, 3);
  10.  
  11. function changePermalinks( $url, $post, $leavename=false ) {
  12.     $url = str_replace("/nl-nl/en/","/en-nl/", $url);
  13.     return $url;
  14. }

WPML

  1. add_filter( 'icl_ls_languages', 'wpml_url_fix');
  2.  
  3. function wpml_url_fix( $languages ) {
  4.  
  5.   global $wpml_url_converter;
  6.  
  7.   $abs_home = $wpml_url_converter->get_abs_home();
  8.   foreach( $languages as $lang => $element ){
  9.  
  10.  
  11.     $languages[$lang]['url'] = str_replace( "/nl-nl/en/","/en-nl/", $languages[$lang]['url'] );
  12.    
  13.   }
  14.   return $languages;
  15. }
  16.  
  17.  
  18.  
  19.  

Rankmath

  1. add_filter( 'rank_math/frontend/canonical', function( $canonical ) {
  2.         return str_replace( "/nl-nl/en/","/en-nl/", $canonical);
  3. });

This will not cover every angle, but will give you a starting point! I love my puzzles and there always is a viable solution :)

Need something similar … get in touch!

Happy coding …

Alex

I am a full-stack developer. I love programming,  design and know my way around server architecture as well.  I would never feel complete, with one of these missing. I have a broad range of interests, that’s why I constantly dive into new technologies and expand my knowledge where ever required. Technologies are evolving fast and I enjoy using the latest. Apart from that, I am a peace loving guy who tries to have people around him that think the same.  I truly believe in the principle: “If you help someone, someone will help you, when you need it."

Recent Posts

Particle Network Animations in Javascript

What are particle animations? Particle network animations in JavaScript typically involve creating visual representations of… Read More

6 days ago

B&B / Hotel Booking Solutions for WordPress | 2024

BOOKING SOLUTIONS 202x This is my take on a subset of booking, appointment, PMS or… Read More

1 month ago

WordPress Cron + WP-CLI + Ntfy

THE GOAL Create a system cron for WordPress, that is accessible and can be easily… Read More

3 months ago

2024 is here and now :)

2024, what's cooking? Slowly getting into the 2024 spirit. 3 projects coming to a close… Read More

4 months ago

2023 ends and whats next !

Short look back at 2023 This has been a busy and interesting year. I am… Read More

5 months ago

cubicFUSION Grid Tweaker – Elementor Grid made easy.

Elementor Pro provides grid containers as an experimental feature. The options provided are limited, when… Read More

5 months ago