Using different domains for different languages


/ Published in: PHP
Save to your folder(s)

courtesy of mark8barnes


Copy this code and paste it in your HTML
  1. <?php
  2. /*
  3. Plugin Name: EMW Wordpress Hacks
  4. Version: 0.2
  5. Plugin URI: http://www.emw.org.uk/
  6. Description: Various hacks to make the bi-lingual site work with two domains.
  7. Author: Mark Barnes
  8. Author URI: http://www.4-14.org.uk/
  9. */
  10.  
  11. //Returns the language requested in the URL
  12. function emw_get_language() {
  13. if ($_GET['lang'])
  14. return $_GET['lang'];
  15. else {
  16. $uri = $_SERVER['REQUEST_URI'];
  17. $query = $_SERVER['QUERY_STRING'];
  18. if (substr($uri, -strlen($query)) == $query)
  19. $uri = substr($uri, 0, -strlen($query)-1);
  20. return substr(trailingslashit($uri), -3, 2);
  21. }
  22. }
  23.  
  24. //Updates the WordPress address
  25. function emw_retain_uri($content) {
  26. $code = emw_get_language();
  27. if ($code == "en")
  28. return "http://www.emw.org.uk";
  29. elseif ($code == "cy")
  30. return "http://www.mudiad-efengylaidd.org";
  31. else
  32. return "http://".$_SERVER['SERVER_NAME'];
  33. }
  34.  
  35. //Redirects the page if wrong domain is used
  36. function emw_redirect () {
  37. $code = emw_get_language();
  38. $host = $_SERVER['HTTP_HOST'];
  39. if ($code=="en" && $host != "www.emw.org.uk") {
  40. header('HTTP/1.1 301 Moved Permanently');
  41. header('Location: http://www.emw.org.uk'.$_SERVER'REQUEST_URI');
  42. }
  43. elseif ($code=="cy" && $host != "www.mudiad-efengylaidd.org") {
  44. header('HTTP/1.1 301 Moved Permanently');
  45. header('Location: http://www.mudiad-efengylaidd.org'.$_SERVER'REQUEST_URI');
  46. }
  47. }
  48.  
  49. //Ensures list_pages and similar functions return correct domain name
  50. // This function requires Gengo to append language links automatically.
  51. function emw_rewrite_link ($link) {
  52. $code = substr($link, -3, -1);
  53. if ($code=="en")
  54. return str_replace ("http://www.mudiad-efengylaidd.org", "http://www.emw.org.uk", $link);
  55. elseif ($code=="cy")
  56. return str_replace ("http://www.emw.org.uk", "http://www.mudiad-efengylaidd.org", $link);
  57. else
  58. return $link;
  59. }
  60.  
  61. //Adds translation widget
  62. function emw_translation_links($args) {
  63. extract($args);
  64. if(function_exists("the_translations")) {
  65. echo $before_widget;
  66. echo $before_title.$after_title;
  67. echo '<div id="change-language">';
  68. the_translations('pre=&post=&inner=&title_exists=&snippet=view_page');
  69. echo '</div>';
  70. echo $after_widget;
  71. }
  72. }
  73.  
  74. //Registers all custom widgets
  75. function emw_widget_init() {
  76. register_sidebar_widget('Translation Links', 'emw_translation_links');
  77. }
  78.  
  79. add_action('template_redirect', 'emw_redirect');
  80. add_filter('option_home', 'emw_retain_uri', 1);
  81. add_filter('option_siteurl', 'emw_retain_uri', 1);
  82. add_filter('page_link', 'emw_rewrite_link', 101);
  83. add_filter('post_link', 'emw_rewrite_link', 101);
  84. add_filter('category_link', 'emw_rewrite_link', 101);
  85. add_filter('category_feed_link', 'emw_rewrite_link', 101);
  86. add_action('widgets_init', 'emw_widget_init');
  87. ?>

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.