grappler / polylang-slug

A unique post slug within the language in Polylang. This allows for a page to have same/identical slug in multiple languages.
268 stars 84 forks source link

Feature Request: allow duplicate taxonomy slugs #8

Open gopeter opened 9 years ago

gopeter commented 9 years ago

This would be very great! :+1:

Use case:

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

grappler commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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

grappler commented 9 years ago

I think I have found a trick.

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 commented 9 years ago

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 commented 8 years ago

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 commented 8 years ago

@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 commented 8 years ago

@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 commented 1 year ago

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 commented 1 year ago

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.