Rewriting every url

There’s not a good way to prepend a permalink with a URI base that I know of and keep WordPress happy at the same time. You can however add the language to the end of the URI with add_rewrite_endpoint();

add_action('init', 'foobar_rewrite_tag');
function foobar_rewrite_tag(){
     $languages = array('en', 'sp'); // Probably have this sourced from your plugin options
     foreach($languages as $language)
         add_rewrite_endpoint( $language, EP_PERMALINK | EP_PAGES);
}

You could use a simple getter function like this in your templates:

function get_language_code(){
    global $wp_query;
    $languages = array('en', 'sp'); //Source this from your plugin options
    foreach($languages as $language)
        if(isset($wp_query->query_vars[$language])) //Note this will not have a value assigned, so we check if it's set to determine the language
             return $language;
}

Your permalinks would look like the following:

  1. http://domain.com/page-name/en
  2. http://domain.com/page-name/sp

I know it’s not exactly as you desire, but it’s the easiest way to wrangle WordPress for this type of approach.

Reference: http://codex.wordpress.org/Rewrite_API/add_rewrite_endpoint

Leave a Comment