I would like to write my own little plugin for language switching. For that, the very first thing would be to get the rewrite rules running. I have been looking around the web for 2 hours, but I couldn’t find an answer to my question yet.
I would like to have it like this: http://www.mysite.com/de/post-title/
or http://www.mysite.com/de/projects/project-title/
So basically, what I’m trying to do is to keep the current url and all it’s parameters, just add the currently active language snippet between the site url and the path. Here is my code so far:
function rewrite_rule_de(){
add_rewrite_tag('%lang%','([^&]+)');
add_rewrite_rule('^de/(.+?)/?$', 'index.php?p=$matches[1]&lang=de', 'bottom');
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_action('init', 'rewrite_rule_de');
I got it now , After searched many resources :
Add this code in
functions.php
. After added code go towp-admin/options-permalink.php
and pressSave Changes
So now you can check/use lang code in your template like..
<?php echo lang();?>
or
if( lang() === "en" )
Example work urls with my example code :
Better idea / bug fixer / found error + bug ? https://gist.github.com/l2aelba/5244912