I have made a website that has 2 WordPress installs one for English language and one for Irish language. They are identical setups with same categories, page names etc.
I have 'English | Irish' links in my header on each page.
When you are on the english page and you click the 'irish' link at top I would like it to take you to the same page but on the Irish site.
The link structure is shown below:
http://mysite.com/english/about
http://mysite.com/irish/about
So I really only need 'english' in the url to be replaced by 'irish'
Their are standard wordpress plugins that handle multi-language issue's for you. But if you whant to stay with you choise this script does exactly what you asked.
$url = 'http://www.mysite.com/english/about/me/test';
$parsedUrl = parse_url($url);
$path_parts = explode("/",$parsedUrl[path]);
$newUrl = $parsedUrl[scheme] . "://" . $parsedUrl[host];
foreach($path_parts as $key =>$part){
if($key == "1"){
if($part == "english") $newUrl .= "/irish";
else $newUrl .= "/english";
} elseif($key > "1"){
$newUrl .= "/" . $part;
}
}
echo "Old: ". $url . "<br />New: " .$newUrl;
Are you using localization - see http://codex.wordpress.org/I18n_for_WordPress_Developers and http://codex.wordpress.org/Multilingual_WordPress? If so, see http://codex.wordpress.org/Function_Reference/get_locale. You can use this to detect the locale and update the link accordingly. If you're using a plugin, you should check the plugin documentation.
If not, you could parse the current URL and explode the path, then update the link this way - http://php.net/manual/en/function.parse-url.php
Example:
<?php
$url = 'http://www.domain-name.com/english/index.php/tag/my-tag';
$path = parse_url($url);
// split the path
$parts = explode('/', $path[path]);
//get the first item
$tag = $parts[1];
print "First path element: " . $tag . "\n";
$newPath = "";
//creating a default switch statement catches (the unlikely event of) unknown cases so our links don't break
switch ($tag) {
case "english":
$newPath = "irish";
break;
default:
$newPath = "english";
}
print "New path element to include: " . $newPath . "\n";
//you could actually just use $parts, but I though this might be easier to read
$pathSuffix = $parts;
unset($pathSuffix[0],$pathSuffix[1]);
//now get the start of the url and construct a new url
$newUrl = $path[scheme] . "://" . $path[host] . "/" . $newPath . "/" . implode("/",$pathSuffix) . "\n";
//full credit to the post below for the first bit ;)
print "Old url: " . $url . "\n". "New url: " . $newUrl;
?>
Adapted from http://www.codingforums.com/archive/index.php/t-186104.html