How to make Wordpress permalinks ignore custom URL rewrite

I am currently working on a wordpress site that should be offered in French. I found a way to get the theme to work with fr_FR po and mo files when I add the querystring variable l. i.e.

site.tldwill give a vanilla English site, and site.tld/?l=fractivate the following code in my .php functions for translating a 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 setting is already working. my question is: how can I rewrite url, so instead site.tld/?l=frI can just add the folder structure using frie site.tld/fr/?

, site.tld/portoflio/autumn/ , site.tld/fr/portfolio/autumn/ . - Apple, .

htaccess:

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

, R, - 404, isntead. , , wordpress 'rewrite, . Month and name (/%year%/%monthnum%/%postname%/) .

: wordpress /fr/ - //etc?

? ? , - WISE, , NICE. http://pmg.co/a-mostly-complete-guide-to-the-wordpress-rewrite-api, .:/


UPDATE: , , @relu :

, .htaccess

i @relu, , Relu /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;
    }
?>

/fr , , :

  • Wordpress . , &q=$matches[2];

  • . , l, echo 'l: $l'; $l = get_query_var('l'); : l: l: fr . locale ? , , queryvar, , , l: fr .. '.

aaaah help.


: qtranslate. , . y'all, esp @relu stayin pow'r.

+5
2

. 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;
}

, , .

.

l ", , : 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: !


my_insert_query_vars() :

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

, WordPress $wp_rewrite->rules :

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
}
+3

, , ; en_us/ jp/

add_rewrite_rule( '^([a-z]{2}_[A-Z]{2}|^[a-z]{2})/?$', 'index.php?l=$matches[1]', 'top' );
0

All Articles