How would I rewrite category based urls using WP Rewrite?
Moving a multilingual site to WordPress that requires permalink preservation, the problem is that some words are the same in both languages for categories, and WordPress does not allow for duplicate category names.
So in English I have a category called "Cuisine" but in french (using WPML) the category cannot be a duplicate and so WordPress appends a hyphen to it and it becomes "Cuisine-2".
This happens with several names.
An example of what I need the urls to look like, the /fr/ is the french part of the site;
www.example.com/fr/cuisine-2/whatever / (current url)
www.example.com/fr/cuisine/whatever / (needs to be this)
www.example.com/cusine/ (this works and should not be effected, since it is the English version)
So is there any easy way to just strip out any hyphen (plus anything after the hyphen) if the url contains /fr/...
Or alternatively I can create a rule for each hyphenated category in /fr/.. (since there are only about 15)
Maybe alter something directly in the DB?
Upon further study it looks like this is rather difficult due to WordPress itself, if anyone knows a solution I would be willing to up the amount.
Cosmin Popovici answers:
To strip the hypen, simply go to Categories in the Dashboard, edit a category and change it's name. Since you can't have it with the same name, you could have cuisine2 or cuisine-fr.
Also, if you're doing custom post types, you can specify the slug to rewrite with.
Bob comments:
<strong>Moving a multilingual site to WordPress that requires permalink preservation</strong>
John Cotton answers:
You can't just remove the hyphens - WordPress has to know which posts to retrieve and so you must give it a definitive category, either as a slug or an id.
What you need to do depends on how complicated your previous URLs were.
If there aren't many, then just adding some fixed rewrites will do the job. ie:
function my_rewrite_rules_array( $rules ) {
$newrules = array();
$newrules['^fr/cuisine$'] = 'index.php?cat=5';
return $newrules + $rules;
}
add_filter('rewrite_rules_array','my_rewrite_rules_array');
(replace 5 with whatever the cuisine2 category is). Just add more lines for each category.
If your original categories are more numerous then you need to come up with a plan for how to interpret old to new (perhaps name your WP categories are 'xxx-new' where xxx is the old name and you just strip -new in a loop.
Bob comments:
This solution would be fine, there are only about 10 categories to change. I tried the code and flushed the rewrite rules (by saving permalinks) a few times but it does not seem to do anything.
The url is still , www.example.com/fr/cuisine-2
For referance:
Permalink structure: /%category%/%postname%/
Cusine-2 : taxonomy=category&tag_ID=104&post_type=post&lang=fr
John Cotton comments:
Hi Bob
I don't understand your reply - could you expand a bit on what the problem is?
Do you mean that you get no page for www.example.com/fr/cuisine? Or that you do get the desired page, but it appears as www.example.com/fr/cuisine-2?
John
Bob comments:
Trying all sorts of rewrite rules seems to have no effect, I tried your example ( and others) and the url for www.example.com/fr/cuisine-2 still shows up as www.example.com/fr/cuisine-2.
www.example.com/fr/cuisine-2 stays as www.example.com/fr/cuisine-2 when I want it as www.example.com/fr/cuisine.
The reference I gave is just some additional info about the structure, I'm a fairly experienced WordPress developer, thought have worked very little with the WP Rewrite API. I thought someone might have some quick info on this but it looks to me that it's just not possible without some serious database tweaking. WordPress is just not set up for this type of stuff.
John Cotton comments:
<blockquote>WordPress is just not set up for this type of stuff.</blockquote>
You'll find it is, I'm just not clear on where you're going wrong.
Here's a live example:
The original link is [[LINK href="http://fantasyscotus.org/cases/m-b-z-v-clinton/"]]http://fantasyscotus.org/cases/m-b-z-v-clinton/lesson-plan/[[/LINK]]. However, you can also access the page through this link [[LINK href="http://fantasyscotus.org/cases/m-b-z-v-clinton/lesson-plan/"]]http://fantasyscotus.org/cases/m-b-z-v-clinton/lesson-plan/[[/LINK]]. Although different content is being displayed (that's the spec), it's actually the same post in the dashboard, with the same permalink.
The rewrite rule is this:
$newrules['^cases/(.+?)/lesson-plan$'] = 'index.php?lesson-plan=1';
I could show you countless examples of making one url look like another, so it definitely works :)
...unless I've misunderstood your question.
John Cotton comments:
Sorry, those links should have been (respectively):
[[LINK href="http://fantasyscotus.org/cases/m-b-z-v-clinton/"]]http://fantasyscotus.org/cases/m-b-z-v-clinton/[[/LINK]]
[[LINK href="http://fantasyscotus.org/cases/m-b-z-v-clinton/lesson-plan/"]]http://fantasyscotus.org/cases/m-b-z-v-clinton/lesson-plan/[[/LINK]]
John Cotton comments:
Bob
I think I know you're problem - is the correct page being displayed, just with it's "normal" url? Is that what you don't like?
If so, in order to replicate the example above (which keeps the rewrite url), you need to look at the template_redirect action. I have these lines in there:
if( get_query_var( 'lesson-plan' ) == 1 ) {
include_once('template-lesson-plan.php');
exit();
}
which ensures that the url stays as is.