1

I am working on a multilingual WordPress site using WPML and I am trying to adjust the author_base URL slug based on the user's language. My goal is to have different slugs for each supported language (English, Spanish, German, Dutch), like 'author' for English, 'autor' for Spanish, etc.

Here is the current implementation in the theme:

function custom_author_base() {
    global $wp_rewrite;
    global $sitepress;
    $current_language = '';

    if (isset($sitepress)) {
        $current_language = ICL_LANGUAGE_CODE;
    }

    $author_slugs = array(
        'en' => 'author',
        'es' => 'autor',
        'de' => 'autor',
        'nl' => 'auteur',
    );

    if (array_key_exists($current_language, $author_slugs)) {
        $wp_rewrite->author_base = $author_slugs[$current_language];
    } else {
        $wp_rewrite->author_base = 'author';
    }
}

add_action('init', 'custom_author_base');

Please do forgive me the messy code. I am simply testing this to find a solution.

The issue is changing author_base requires flushing the rewrite rules. However, if I do this in one language (e.g., Spanish), the author_base is set to 'autor' for all languages until it is saved again in another language (e.g., Dutch), which then sets it to 'auteur' for all languages.

Is there a way to dynamically change the author_base based on the frontend language without the negative impact of continually needing to flush the rewrite rules? I've looked through the WordPress Codex and WPML documentation but haven't found a solution yet. Any suggestions or insights are greatly appreciated.

1

0