Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: allow duplicate taxonomy slugs #8

Open
gopeter opened this issue May 18, 2015 · 10 comments
Open

Feature Request: allow duplicate taxonomy slugs #8

gopeter opened this issue May 18, 2015 · 10 comments
Assignees

Comments

@gopeter
Copy link

gopeter commented May 18, 2015

This would be very great! 👍

Use case:

example.com/de/leistungen/branding
example.com/en/services/branding

@grappler grappler self-assigned this May 18, 2015
@grappler
Copy link
Owner

I will have a look at it. It will take some time. I might work on this in June.

Are you going to WordCamp Köln?

@grappler
Copy link
Owner

grappler commented Jun 3, 2015

I am having a look at this. I am not sure it is possible as I could not find a hooks that I can use. Do you know of any plugin that has this feature? WPML does not have it.

@ihorvorotnov
Copy link

@grappler I'm not aware of any hooks atm, but check this and this.

@grappler
Copy link
Owner

grappler commented Jun 4, 2015

I think I have found a trick.

  • Create a term in wp_terms. Category term_id: 29
    image
  • Define the same term for both languages in wp_term_taxonomy as a:2:{s:2:"de";i:29;s:2:"en";i:29;}. 29 is the term id.
    image
  • Add the term_id 29 to the posts in wp_term_relationships. Post object_id: 4, 5
    image
  • Remove the term_language ID from term ID in wp_term_relationships. The term language was originally German but by removing it, we make it neutral.
    image

This shows how the the language is defined for a term

wp_term_taxonomy

image

wp_terms

image

I will look how this can be automated.

@grappler
Copy link
Owner

After doing a bit more research I found some filters in Polylang that allows a taxonomy to used without needing to translate it.

/*
 * Add taxonomies to the list to copy or to synchronize.
 *
 * @param array $taxonomies List of taxonomy names.
 * @param bool  $sync       True if it is synchronization, false if it is a copy.
 *
 * @return array List of taxonomy names.
 */
function polylang_slug_copy_taxonomies( $taxonomies, $sync ){
    $taxonomies[] = 'wpzoo_doc_cat';
    return $taxonomies;
}
add_filter( 'pll_copy_taxonomies', 'polylang_slug_copy_taxonomies', 10, 2 );

/*
 * Add taxonomies that need to be filtered.
 *
 * @param array $taxonomies An array of registered taxonomy names.
*
 * @return array An array of registered taxonomy names.
 */
function polylang_slug_filtered_taxonomies( $taxonomies ){
    $add_taxonomies = array(
        'wpzoo_doc_cat' => 'wpzoo_doc_cat',
    );
    return array_merge( $taxonomies, $add_taxonomies );
}
add_filter( 'pll_filtered_taxonomies', 'polylang_slug_filtered_taxonomies' );

Using this method the term is the same for both languages. This change applies to the whole taxonomy is meant for taxonomies that have terms which are the same in other languages.

@AdamQuadmon
Copy link

AdamQuadmon commented Jul 21, 2016

What's the state of this?
I tried to follow you on the db hack with no luck.
How is supposed to be used the code in your last post?

For now I'm thinking to just not use categories and only slugs:

mysite.com/video-my-post
mysite.com/es/video-my-post

@grappler
Copy link
Owner

@AdamQuadmon Polylang Pro offers this feature so I don't really have a reason to work on this anymore. I should perhaps add something to the documentation.

@floffimedia
Copy link

floffimedia commented Aug 14, 2016

@AdamQuadmon I was struggling with this issue too. Finally, I came up with a hack, but I can't actually recommend it, unless you're really desperate ...

Let's say you've created two duplicate categories in the dashboard, where one of them has the slug video and the other one gets renamed to video-es. You can then head over to phpMyAdmin, find the right entry in wp_terms and change the slug from video-es to video. The change will be persistent and you will have two identical categories, one for each language.

The only problem is that if you want to access the category archive in Spanish at example.com/es/category/video/, you will get redirected to example.com/en/category/video/ (or whatever your other language is; I think this has to do with either the default language or the category that was created first).

To fix this, you can create a rewrite rule that internally redirects your request to the category ID. Add this snippet to your functions.php, create a new array entry for each category, and don't forget to change the IDs.

function add_rewrite_rules($rules) {
    $newrules = array('es/(category/)?video/?(page/)?([0-9]+)?/?$' => '/es/?cat=1234&paged=$matches[3]');
    $rules = $newrules + $rules;
    return $rules;
}
add_filter('rewrite_rules_array', 'add_rewrite_rules');

When you're done, you'll have to flush your existing rewrite rules by heading to Settings > Permalinks and clicking Save Changes without making any changes.

Like I said, this solution is far from perfect. You're probably much better off buying Polylang Pro or switching to multisite. I'm sure this approach will mess up your XML exports and probably cause other problems that I'm not aware of right now. But so far, I haven't found any other workaround for this.

@SeoDeveloper01
Copy link

SeoDeveloper01 commented Nov 21, 2022

Wow. I came here to add this feature request, but it's been hanging since 2015...
As for me, I have no desire to buy a subscription to Polylang Pro just for the sake of being able to use the same slugs. So this plugin could be a good solution to my problem, but without taxonomy support it is incomplete.

@grappler
Copy link
Owner

Sorry, I don't plan to add this feature. I personally use the pro version, and it would cost more than €100 to develop the feature. I personally like the idea of supporting the original plugin developers so that the free version will be still available in the future.

I plan to leave this issue open so that the missing feature is visible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants