wordpressurl-rewritingelementorpolylangwp-rewrite

Rewrite rules breaking Wordpress Elementor page edition / Polylang


I have a Wordpress website (not publicly open for the moment, I'm finishing the dev). I'm using Wordpress 6.7.2, Elementor Pro 3.28.2, Polylang 3.6.7.

I have some issues with my rewrite rules (personnal plugin) that are breaking the edition of pages in Wordpress/Elementor. And this seems to be linked to Polylang (see why below).

These are the two rules that are causing issues :

'^fr/(?!garage|cars|elementor|car-page|compare-your-cars)([A-Za-z0-9-]+)/?$' => 'index.php?pagename=car-make-fr&make=$matches[1]',
'^en/(?!garage|cars|elementor|car-page|compare-your-cars)([A-Za-z0-9-]+)/?$' => 'index.php?pagename=car-make&make=$matches[1]',

They allow any page starting with "https://comparedrives.com/fr" or "https://comparedrives.com/en" to use a special page that will load car make information from my DB. For instance :

https://comparedrives.com/fr/bmw/
https://comparedrives.com/en/audi/

I need these rules for my site to work correctly. And they work perfectly...

But when I'm in Wordpress interface, and I click on "Edit with Elementor" on pages, edition does not work. It doesn't load and ends up asking me if I need to use "Safe Mode". If I disable these two rules, everything is OK. So I know the issue comes from them.

I tried using is_edit_mode to test and enable these rules only when NOT in edition mode, but it doesn't work. I also tried with a personnal function (FF_bElementor_Edition_mode_PHP_v2) with plenty of other tests to see if I'm in edition mode. No success.

But I also noticed that this issue seems linked to Polylang. Here's why :

Test: Page edition : error. "Safe Mode" required for edition.
Action: Polylang plugin disabled.
Test: Page edition OK. No issues.
Action: Polylang plugin enabled.
Test: Page edition OK. No issues. My rewrite rules (personnal plugin) are not working.
Action: Settings --> Permalink --> Save. (To re-enable my rewrite rules)
Test: Page edition : error. "Safe Mode" required for edition. My rewrite rules (personnal plugin) are working.

Here is the code for my rewrite rules :

function FF_rewrite_url_rules($rules) {
    $new_rules = array(
        // Car-Series
            // Version FR : fr/garage/{make}/{model}
            '^fr/garage/([^/]+)/([^/]+)/?$' => 'index.php?pagename=car-series-fr&make=$matches[1]&model=$matches[2]', // fr/garage/X/Y ==> fr/garage/bmw/3-series // Page "Car Series" / index.php?pagename=car-series&make=bmw&model=3-series
            // Version EN : en/garage/{make}/{model}
            '^en/garage/([^/]+)/([^/]+)/?$' => 'index.php?pagename=car-series&make=$matches[1]&model=$matches[2]',
    // Car-Versions
        // Version FR : fr/garage/{make}/{model}/{series}
        '^fr/garage/([^/]+)/([^/]+)/([^/]+)/?$' => 'index.php?pagename=car-versions-fr&make=$matches[1]&model=$matches[2]&series=$matches[3]', // fr/garage/X/Y/Z ==> fr/garage/bmw/3-series/sport-line // Page "Car Versions" / index.php?pagename=car-versions&make=$matches[1]&model=$matches[2]&series=$matches[3] / Encore utilisé ??
        // Version EN : en/garage/{make}/{model}/{series}
        '^en/garage/([^/]+)/([^/]+)/([^/]+)/?$' => 'index.php?pagename=car-versions&make=$matches[1]&model=$matches[2]&series=$matches[3]',
    
    // Car-page
        // Version FR : fr/cars/{ID_Car}
        '^fr/cars/([0-9]+)/?$' => 'index.php?pagename=car-page-fr&ID_Car=$matches[1]', // fr/cars/XXX ==> fr/cars/123 // Page "Car Page" / index.php?pagename=car-page&ID_Car=123
        // Version EN : en/cars/{ID_Car}
        '^en/cars/([0-9]+)/?$' => 'index.php?pagename=car-page&ID_Car=$matches[1]',

    // compare-your-cars v1
    '^fr/compare-cars-old/?$' => 'index.php?pagename=compare-your-cars-fr',
    '^en/compare-cars-old/?$' => 'index.php?pagename=compare-your-cars',

    // compare-your-cars v2
    '^fr/compare-cars/?$' => 'index.php?pagename=compare-your-cars-v2-fr',
    '^en/compare-cars/?$' => 'index.php?pagename=compare-your-cars-v2',

    // Car-make
        // Les règles "Car-make" posent problème en mode édition, donc on ne les ajoute que si on n'est PAS en mode édition.
        // On exclut ici :
        //      Les pages qui ne sont PAS des makes : 'garage', 'cars'
        //      Les pages d'édition Wordpress/Elementor : 'elementor', 'car-page', 'compare-your-cars' (et éventuellement d'autres slugs réservés)
        
    // Version FR : fr/{make}
    // '^fr/(?!garage|cars)([A-Za-z0-9-_]+)/?$' => 'index.php?pagename=car-make-fr&make=$matches[1]',
    '^fr/(?!garage|cars|elementor|car-page|compare-your-cars)([A-Za-z0-9-_]+)/?$' => 'index.php?pagename=car-make-fr&make=$matches[1]',
    // Version EN : en/{make} 
    // '^en/(?!garage|cars)([A-Za-z0-9-_]+)/?$' => 'index.php?pagename=car-make&make=$matches[1]',
    '^en/(?!garage|cars|elementor|car-page|compare-your-cars)([A-Za-z0-9-_]+)/?$' => 'index.php?pagename=car-make&make=$matches[1]',
);

// Les nouvelles règles sont placées avant les règles existantes
// custom_log('FF_rewrite_url_rules is returning new rules');

custom_log('FF_rewrite_url_rules / Returning rules merged with personnal rules');
return array_merge($new_rules, $rules);
}
// Ajouter le filtre pour générer les règles de réécriture
add_filter('generate_rewrite_rules', function($wp_rewrite) {
    // if(!FF_bElementor_Edition_mode_PHP_v2()){
    // N'appliquer les règles personnalisées que si l'éditeur Elementor n'est PAS actif
    if (\Elementor\Plugin::$instance->editor->is_edit_mode() ) {
        custom_log('generate_rewrite_rules filter / Elementor IS in Edition mode / Not changing rules');
    } elseif (\Elementor\Plugin::$instance->preview->is_preview_mode() ) {
        custom_log('generate_rewrite_rules filter / Elementor IS in Preview mode / Not changing rules');
    } else {
        custom_log('generate_rewrite_rules filter / Elementor is NOT in Edition or Preview mode / Merging rules');
        $wp_rewrite->rules = FF_rewrite_url_rules($wp_rewrite->rules); }
});

Please help. I've been on this issue for quite some time now ... Thanks !


Solution

  • So, no help ? :(

    If anyone is interested, I didn't manage to get my dynamic rule working without impacting the edition of Wordpress pages, so I just used a rule with a static array of makes instead :

    $all_makes_rules = array(
            'AC', 'Acura', 'Adler', 'Alfa Romeo', 'Alpina', 'Alpine', 'AMC', 'Apal', 'Aro', 'Asia', 'Aston Martin', [...]
    , 'ZX'
    );
    // The real code contains all the makes. Just making it shorter here.
    
    $all_makes_rules = array_map(function($make) {
         return str_replace(' ', '_', strtolower($make));
    }, $all_makes_rules);
    
    $pattern = implode('|', $all_makes_rules);
    
    [...]
    "^(?i:fr/($pattern)/?$)" => 'index.php?pagename=car-make-fr&make=$matches[1]',
    "^(?i:en/($pattern)/?$)" => 'index.php?pagename=car-make&make=$matches[1]'[...]
    

    This works fine.

    But if anyone has any idea on how to get my original dynamic rule working, I'm still interested ...