Created
July 31, 2010 17:46
-
-
Save mikeschinkel/502421 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Handles URL Rewriting in WordPress 3.0 like this: | |
fastigheter-spanien/X -> property/X&lang=sv | |
fastigheter-usa/X -> property/X&lang=sv | |
properties-spain/X -> property/X&lang=en | |
properties-usa/X -> property/X&lang=en | |
Answer for: http://wpquestions.com/question/show/id/694 | |
By: Mike Schinkel | |
Copy this to your theme's functions.php file or put the code in a plugin. | |
Be sure to rename the "my_*()" functions to something unique that makes | |
sense in your sites context. | |
*/ | |
add_action('init', 'my_init'); | |
function my_init() { | |
global $wp,$wp_rewrite; | |
$wp->add_query_var('lang'); | |
$wp_rewrite->add_rewrite_tag('%lang%','([^/]+)','lang='); | |
$wp_rewrite->add_permastruct('fastigheter', 'fastigheter-%lang%/%property%'); | |
$wp_rewrite->add_permastruct('properties', 'properties-%lang%/%property%'); | |
register_post_type('property', | |
array( | |
'label' => 'Property', | |
'public' => true, | |
'show_ui' => true, | |
'query_var' => 'property', | |
'rewrite' => array('slug' => 'property'), | |
'hierarchical' => true, | |
'supports' => array( | |
'title', | |
'editor', | |
'excerpts', | |
'custom-fields', | |
'page-attributes', | |
'thumbnail' | |
), | |
) | |
); | |
/* | |
* The line that follows should be deleted and go in a plugin activation hook | |
* or you can just update permalinks in the admin once (after adding this | |
* code) by going here: | |
* | |
* http://example.com/wp-admin/options-permalink.php | |
* | |
* (replace example.com above with your domain) | |
* | |
*/ | |
$wp_rewrite->flush_rules(true); | |
} | |
add_filter('request', 'my_request'); | |
function my_request($request) { | |
if (isset($request['lang'])) | |
switch ($request['lang']) { | |
case 'spanien': | |
case 'spain': | |
$request['lang'] = 'sv'; | |
break; | |
case 'usa': | |
default: | |
$request['lang'] = 'en'; | |
} | |
return $request; | |
} | |
function get_property_lang() { | |
global $wp_query; | |
return (isset($wp_query->query_vars['lang']) ? $wp_query->query_vars['lang'] : ''); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment