Return to Snippet

Revision: 26760
at May 10, 2010 06:42 by iroybot


Initial Code
<?php
/*
Plugin Name: EMW Wordpress Hacks
Version: 0.2
Plugin URI: http://www.emw.org.uk/
Description: Various hacks to make the bi-lingual site work with two domains.
Author: Mark Barnes
Author URI: http://www.4-14.org.uk/
*/

//Returns the language requested in the URL
function emw_get_language() {
	if ($_GET['lang'])
		return $_GET['lang'];
	else {
		$uri = $_SERVER['REQUEST_URI'];
		$query = $_SERVER['QUERY_STRING'];
		if (substr($uri, -strlen($query)) == $query)
			$uri = substr($uri, 0, -strlen($query)-1);
		return substr(trailingslashit($uri), -3, 2);
	}
}

//Updates the WordPress address
function emw_retain_uri($content) {
	$code = emw_get_language();
	if ($code == "en")
		return "http://www.emw.org.uk";
	elseif ($code == "cy")
		return "http://www.mudiad-efengylaidd.org";
	else
		return "http://".$_SERVER['SERVER_NAME'];
}

//Redirects the page if wrong domain is used
function emw_redirect () {
	$code = emw_get_language();
	$host = $_SERVER['HTTP_HOST'];
	if ($code=="en" && $host != "www.emw.org.uk") {
		header('HTTP/1.1 301 Moved Permanently');
		header('Location: http://www.emw.org.uk'.$_SERVER'REQUEST_URI');
	}
	elseif ($code=="cy" && $host != "www.mudiad-efengylaidd.org") {
		header('HTTP/1.1 301 Moved Permanently');
		header('Location: http://www.mudiad-efengylaidd.org'.$_SERVER'REQUEST_URI');
	}
}

//Ensures list_pages and similar functions return correct domain name
// This function requires Gengo to append language links automatically.
function emw_rewrite_link ($link) {
	$code = substr($link, -3, -1);
	if ($code=="en")
		return str_replace ("http://www.mudiad-efengylaidd.org", "http://www.emw.org.uk", $link);
	elseif ($code=="cy")
		return str_replace ("http://www.emw.org.uk", "http://www.mudiad-efengylaidd.org", $link);
	else
		return $link;
}

//Adds translation widget
function emw_translation_links($args) {
	extract($args);
	if(function_exists("the_translations")) {
		echo $before_widget;
		echo $before_title.$after_title;
		echo '<div id="change-language">';
		the_translations('pre=&post=&inner=&title_exists=&snippet=view_page');
		echo '</div>';
		echo $after_widget;
	}
}

//Registers all custom widgets
function emw_widget_init() {
	register_sidebar_widget('Translation Links', 'emw_translation_links');
}

add_action('template_redirect', 'emw_redirect');
add_filter('option_home', 'emw_retain_uri', 1);
add_filter('option_siteurl', 'emw_retain_uri', 1);
add_filter('page_link', 'emw_rewrite_link', 101);
add_filter('post_link', 'emw_rewrite_link', 101);
add_filter('category_link', 'emw_rewrite_link', 101);
add_filter('category_feed_link', 'emw_rewrite_link', 101);
add_action('widgets_init', 'emw_widget_init');
?>

Initial URL
http://wordpress.org/support/topic/221850

Initial Description
courtesy of mark8barnes

Initial Title
Using different domains for different languages

Initial Tags
wordpress

Initial Language
PHP