ข้ามการนำทาง

Sometimes, third-party plugins call the flush_rewrite_rules(true) function too often. This leads to changes in the rewrite rules in your .htaccess file, which can cause issues on your WordPress site.

What is the flush_rewrite_rules(true) function and what happens when plugins call it too often?

The flush_rewrite_rules(true) function in WordPress is used to regenerate rewrite rules, which are rules that tell your website how to handle URLs. It’s a critical function but when used too often, it can cause WordPress to overwrite the .htaccess file. 

This becomes problematic when it occurs during every page request or while you browse your site in another language. When this happens, WPML goes ahead and adds the language folder to home_url as usual. However, this results in an incorrect RewriteBase in .htaccess, causing your website’s front-end to break. 

In this case, WPML is acting correctly and the problem is entirely in the plugin calling this function too often.

How can I identify the plugin causing this and fix the issue?

Identifying the specific plugin causing this issue can be challenging. It involves testing in a minimal environment and deactivating plugins one by one. After finding the responsible plugin, you would need to contact the author and request that they add a fix that limits the rewrite rule flushing to happen only once. 

As a quick workaround, you can instruct WPML not to add the language folder. To do this, add the following code to your theme’s functions.php file:

add_filter('mod_rewrite_rules', 'fix_rewritebase');
function fix_rewritebase($rules){
    $home_root = parse_url(home_url());
    if ( isset( $home_root['path'] ) ) {
        $home_root = trailingslashit($home_root['path']);
    } else {
        $home_root = '/';
    }
 
    $wpml_root = parse_url(get_option('home'));
    if ( isset( $wpml_root['path'] ) ) {
        $wpml_root = trailingslashit($wpml_root['path']);
    } else {
        $wpml_root = '/';
    }
 
    $rules = str_replace("RewriteBase $home_root", "RewriteBase $wpml_root", $rules);
    $rules = str_replace("RewriteRule . $home_root", "RewriteRule . $wpml_root", $rules);
 
    return $rules;
}

What should I do if I need more help?

If you continue to face problems or need specific assistance, please contact our support team.