Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7669987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:43:10+00:00 2026-05-31T15:43:10+00:00

i’m currently working on a wordpress site that needs to have the option to

  • 0

i’m currently working on a wordpress site that needs to have the option to be offered in french. i’ve found a way to make the theme work with the fr_FR po and mo files when i add a querystring variable l. i.e.

site.tld will yield the vanilla english site, while site.tld/?l=fr will activate the following code in my functions.php to serve the french translation:

<?php
// http://codex.wordpress.org/Plugin_API/Filter_Reference/locale
add_filter( 'locale', 'set_my_locale' );
function set_my_locale( $lang ) {
    if ("fr" == substr(strtolower(trim(strip_tags(stripslashes($_GET['l'])))), 0, 2)) {
        // set language to french
        return "fr_FR";
    } else {
        // return original locale
        return $lang;
    }
}
?>

this setup is already working. my question is: how do i rewrite the url so instead of site.tld/?l=fr i can just prepend the folder structure with fr i.e. site.tld/fr/?

so like if there’s a page site.tld/portoflio/autumn/ in english, site.tld/fr/portfolio/autumn/ will spit out the french one. i got this idea from the apple website, where the language is always prepended to the folder structure.

i can already make this happen with an external redirect in htaccess:

RewriteBase /
RewriteCond ^[a-z]{2}/
RewriteRule ^([a-z]{2})/(.*)$ /$2?l=$1 [R,L]

this works, but once i remove the R flag it serves the french-translated 404 not found isntead. i’m guessing what i did is messing-up with wordpress’ rewrite rules because i need to use pretty permalinks. right now i’m set to use Month and name (/%year%/%monthnum%/%postname%/) in the admin general options.

my question is, how do i make wordpress ignore the /fr/ part and still serve the correct page/post/etc?

is this even possible? am i on the right track here? i need your help especially doing what’s WISE and not just what’s NICE. i tried this http://pmg.co/a-mostly-complete-guide-to-the-wordpress-rewrite-api but for the life of me i can’t make it work. :/


UPDATE: ok, so here’s some progress taking cue from @relu below:

i made sure my rules were canned from .htaccess

i then added the rules from @relu but in the following way, because Relu’s always sent me to the same address without the /fr:

<?
    // http://pmg.co/custom-wordpress-shortlinks
    add_action( 'init', 'pmgtut_add_rewrites' );
    function pmgtut_add_rewrites() {
        add_rewrite_rule( '^([a-z]{2})/?$', 'index.php?l=$matches[1]', 'top' );
        add_rewrite_rule( '^([a-z]{2})/(.*)$', 'index.php?l=$matches[1]&q=$matches[2]', 'top' );
    }
    add_filter( 'query_vars', 'pmgtut_query_vars', 10, 1 );
    function pmgtut_query_vars( $vars ) {
        $vars[] = 'l';
        return $vars;
    }
?>

now the /fr stays in the address bar, so that’s a good thing, but two problems persist:

  1. wordpress is serving me the main index page. seems like it’s not using the &q=$matches[2] part of the rule; and

  2. the locale still doesn’t get set properly. i checked if the variable l is getting fed, so i added echo 'l: $l'; after $l = get_query_var('l'); and interestingly i get two echoes: one l: and another l: fr right after that. is the locale filter being run twice? seems like the first time it doesn’t see the value of the queryvar, and then there seems to be a second time that it outputs l: with fr already in there.. at the end of the day the locale still doesn’t get changed.

aaaah help.


FINAL UPDATE: in my last breath of frustration i searched again, and found this plugin qtranslate. does what i need. thanks y’all, esp to @relu’s epic effort and stayin pow’r.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:43:11+00:00Added an answer on May 31, 2026 at 3:43 pm

    Add this to your functions.php

    add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' );
    add_filter( 'query_vars','my_insert_query_vars' );
    add_action( 'wp_loaded','my_flush_rules' );
    
    // flush_rules() if our rules are not yet included
    function my_flush_rules(){
        $rules = get_option( 'rewrite_rules' );
    
        if ( ! isset( $rules['^([a-z]{2})/(.*)$'] ) ) {
            global $wp_rewrite;
            $wp_rewrite->flush_rules();
        }
    }
    
    // Adding a new rule
    function my_insert_rewrite_rules( $rules )
    {
        $newrules = array();
        $newrules['^([a-z]{2})/(.*)$'] = 'index.php?l=$matches[1]&q=$matches[2]';
        return $newrules + $rules;
    }
    
    // Adding the l var so that WP recognizes it
    function my_insert_query_vars( $vars )
    {
        array_push($vars, 'l');
        return $vars;
    }
    

    Haven’t tested this, it may need some tweaking, but this is the way to do it.

    I’ve basically adapted the version found here.

    It also registers the “l” query var so you can get it’s value by calling: get_query_var

    add_filter( 'locale', 'set_my_locale' );
    function set_my_locale( $lang ) {
        $l = get_query_var('l');
    
        if ("fr" == $l) {
            // set language to french
            return "fr_FR";
        } else {
            // return original locale
            return $lang;
        }
    }
    

    UDATE: Ignore the above!


    Remove the my_insert_query_vars() filter and replace it with:

    function register_rewrite_tag() {
            add_rewrite_tag('%l%', '([a-z]{2})');
    }
    add_action('init', 'register_rewrite_tag'); 
    

    Here are also the updated rewrites for pages and posts, I’ve looked at WordPress’ $wp_rewrite->rules and adapted them to fit this special case:

    function pmgtut_add_rewrites() {
            add_rewrite_rule( '^([a-z]{2})/?$', 'index.php?l=$matches[1]', 'top' );
            add_rewrite_rule( '^([a-z]{2})/(.?.+?)(/[0-9]+)?/?$', 'index.php?l=$matches[1]&pagename=$matches[2]&page=$matches[3]', 'top' ); // Pages
            add_rewrite_rule( '^([a-z]{2})/([^/]+)(/[0-9]+)?/?$', 'index.php?l=$matches[1]&name=$matches[2]&page=$matches[3]', 'top' ); // Posts
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.