Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
Answer to: http://wpquestions.com/question/show/id/694
By: Mike Schinkel
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
*/
add_action('init', 'my_test_rewrite');
function my_test_rewrite() {
global $wp,$wp_rewrite;
$wp->add_query_var('lang');
$wp_rewrite->add_permastruct('fastigheter-spanien', 'fastigheter-spanien/%property%');
$wp_rewrite->add_permastruct('fastigheter-usa', 'fastigheter-usa/%property%');
$wp_rewrite->add_permastruct('properties-spain', 'properties-spanien/%property%');
$wp_rewrite->add_permastruct('properties-usa', 'properties-usa/%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'
),
)
);
/*
* $wp_rewrite->flush_rules(false);
*
* This line above should go in a plugin activation hook or you
* can just update permalinks once after adding this code here:
*
* http://example.com/wp-admin/options-permalink.php
*
* (replace example.com with your domain)
*
*/
}
add_filter('rewrite_rules_array','my_rewrite_rules_array');
function my_rewrite_rules_array($rewrite_rules) {
$lang_lookup = array(
'fastigheter' => 'sv',
'properties' => 'en',
);
foreach($rewrite_rules as $match => $output) {
list($first_segment) = explode('/',$match);
if (in_array($first_segment,array('fastigheter-spanien','fastigheter-usa','properties-spanien','properties-usa'))) {
list($word,$country) = explode('-',$first_segment);
$rewrite_rules[$match] = $output . "&lang={$lang_lookup[$word]}";
}
}
return $rewrite_rules;
}
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
You can’t perform that action at this time.