Support » Plugins and Hacks » Hacks » Language url Prefix for WordPress.

Language url Prefix for WordPress.

  • I’ve seen a lot of threads asking the same, but all of them seem really confusing and i couldn’t get the code to work properly.

    WordPress handles the redirects and has an API for that, however the Codex is really confusing also doesn’t really explain how to get to work the rewrite hooks and i don’t understand the workings of that API.
    I’m trying to include the language prefix in one plugin but i can’t seem to get it to work properly.
    This is the code i’ve got so far:

    function add_rewrite_rules( $wp_rewrite )
            $keytag = '%lang%';
    	$wp_rewrite->add_rewrite_tag($keytag, 'en', 'lang=en');
    	$keywords_structure = $wp_rewrite->root . "$keytag/";
    	$keywords_rewrite = $wp_rewrite->generate_rewrite_rules($keywords_structure);
    	$wp_rewrite->rules = $keywords_rewrite + $wp_rewrite->rules;
            $wp_rewrite->add_rewrite_tag($keytag, 'en/.+?', 'lang=en');
    	$keywords_structure = $wp_rewrite->root . "$keytag/";
    	$keywords_rewrite = $wp_rewrite->generate_rewrite_rules($keywords_structure);
    // Always add your rules to the top, to make sure your rules have priority
          $wp_rewrite->rules = $keywords_rewrite + $wp_rewrite->rules;
    add_action('generate_rewrite_rules', 'add_rewrite_rules');
    function query_vars($public_query_vars) {
    	$public_query_vars[] = "lang";
    	return $public_query_vars;
    add_filter('query_vars', 'query_vars');
    add_action('init', 'flush_rewrite_rules');
    function flush_rewrite_rules()
    		global $wp_rewrite;

    This will make possible to detect the query lang for urls like :
    http://www.arturoemilio.es/en/proba/ (for page)

    However although i get the query_var LANG = en, it doesn’t do the rest of the redirects so, it doesn’t show you the page (home, proba) but takes you to the list of entries page:
    for example url : http://www.arturoemilio.es/ideas-y-proyectos/

    array (
      'page' => '',
      'pagename' => 'ideas-y-proyectos',

    but the url http://www.arturoemilio.es/en/ideas-y-proyectos/ gives this

    array (
      'lang' => 'en',

    I have to say that the home is an static page (not the las entries).
    What am i missing here?

    Note: The hooks are only active to admins, so it will not work right know for visitors.

  • The topic ‘Language url Prefix for WordPress.’ is closed to new replies.
Skip to toolbar