klicher / wp-polylang-translate-rewrite-slugs Goto Github PK
View Code? Open in Web Editor NEWWordpress plugin to help translate post types rewrite slugs with the Polylang plugin.
Wordpress plugin to help translate post types rewrite slugs with the Polylang plugin.
it would be awesome if tags and categories would be supported as well!
As you know, when registering a custom post type in WordPress you can define the rewrite slug. For example:
register_post_type( 'my_book_cpt', array(
...
'rewrite' => array( 'slug' => 'book' ),
...
) );
You can enable archives for your custom post type by setting has_archive
to true, like so:
register_post_type( 'my_book_cpt', array(
...
'rewrite' => array( 'slug' => 'book' ),
'has_archive' => true,
...
) );
Now your custom post archive will be available at http://example.com/book/lord-of-the-rings
However, it is also possible to customize the archive slug independently of the single post slug by defining has_archive
as a string (rather than true/false). So you could for example, have the custom post archive http://example.com/books (notice the 's' at the end) by doing this:
register_post_type( 'my_book_cpt', array(
...
'rewrite' => array( 'slug' => 'book' ),
'has_archive' => 'books',
...
) );
From what I can see, this plugin doesn't currently support translating the custom post type archive slug independently.
Hello!
Your little pluguin work great but I have a slight little problem. Everything is working but when I'm on the archive page of the custom post type (joueuses / players), the url in the language switcher is "/players" but it should be "/en/players"... /players in a 404 but /en/players is working as it should... I'm using the $translations = pll_the_languages(array('raw'=>1)); and generating my URL with $html_lnswitch = ''.$translation['slug'].''; Work perfectly for other page (it adds the language folder) but not for custom-post type...
Hi,
I am getting a 404 error only in child custom posts, because the rewrite filter seems to delete parent post from children permalink.
So mydomain.com/custom-post-type-slug/parent-custom-post is working in every languages but when I try to get the permalink of a "custom post child" the permalink is mydomain.com/custom-post-type-slug/child-custom-post instead mydomain.com/custom-post-type-slug/parent-custom-post/child-custom-post
Any help will be apreciated. Thanks a lot!
I added the filter in function.php, the slug is well translated but the page of translated slug is empty, can you help me?
Row 344 fix for is_category()
if( $polylang->options['force_lang'] == 1 && $lang == pll_default_language() ){
return home_url('/'.$translated_term->slug);
} else {
return home_url('/'.$lang.'/'.$translated_term->slug);
}
Hey - great plugin..
I have an issue - I'd like my custom post_type slugs to contain variable placeholders - which are taxonomy terms - for example:
// Add translation for "property".
$post_type_translated_slugs['property'] = array(
'en' => 'property/%property_type%/%location%',
'nl' => 'eigendom/%property_type%/%location%',
);
I've hacked your "post_type_link_filter" filter - and the slugs are displaying correctly:
// type ##
if ( false !== strpos( $post_link, '%property_type%' ) ) {
$get_term = get_the_terms( $post->ID, 'property_type' );
if ( $get_term && is_array( $get_term ) ) {
$post_link = str_replace( '%property_type%', array_pop( $get_term )->slug, $post_link );
}
}
However, all posts 404 when viewed - I've tried flushing the permalinks, but no joy.
Am I going about this the wrong way - is there an easier method to pass and correct those placeholders?
Cheers!
Q
Hey KLicheR,
First of: amazing work on adding the possibility for archive slugs! it works great!
I have a question: when using your plugin, i am not able to preview a post.
When i create a news post with slug "test-page" and i click Preview i am redirected to http://testsite.com/nieuws/test-page?preview=true and i get an 404 page. This should be http://testsite.com/nl/nieuws/test-preview?preview=true (because my default language is NL).
I tried looking for a clue but didnt find one yet. Can you help?
Thanks!
Just recently found out that this plugin removes the last slash from permalinks of custom post type. Could this be fixed?
HI, I have changed the base for product slug in "Veicoli" (my predefined language is italian). But using your example function the slug "veicolo" is not translated.
add_filter('pll_translated_post_type_rewrite_slugs', function($post_type_translated_slugs) {
// Add translation for "product" post type.
$post_type_translated_slugs = array(
'product' => array(
'en' => array(
'has_archive' => true,
'rewrite' => array(
'slug' => 'vehicle',
),
),
'de' => array(
'has_archive' => true,
'rewrite' => array(
'slug' => 'fahrzeug',
),
),
),
);
return $post_type_translated_slugs;
});
How do I return the translated slug previously defined with:
add_filter('pll_translated_post_type_rewrite_slugs', function($post_type_translated_slugs){
$post_type_translated_slugs['my_cpt'] = array(
'en' => 'en-slug',
'ru' => 'ru-slug'
);
return $post_type_translated_slugs;
});
If I use $post_type_slug = $post_obj->rewrite['slug'] it returns default not translated slug.
How can I translate the custom category taxonomy?
register_post_type -> product => translated ✓
register_taxonomy('furnitures', array('product'), array(
'hierarchical' => true,
'labels' => $labels[0],
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'furnitures' ),
));
The website I was working on has kind of crumbled apart a little bit with this plugin.
The problem is that your plugin rewrites the language from:
to:
Any way to fix this? Went through the plugin code, tried finding the bit that does it, nothing has changed. Can't find a rewrite rule on that in MySQL either.
Hi,
When i register a slug, for example 'projects', with has_archive = false the path 'projects' is not available for a page.
used code:
'project' => array(
'en' => array(
'has_archive' => false,
'rewrite' => array(
'slug' => 'projects',
),
)
But technically this should be possible because i do not have a archive and i could register this path for a page.
Do you know if this is possible?
Hi guys,
first off thanks a lot for creating this plugin. It seems to have helped many people so far - so good Karma your way!
Now to my question
Maybe I misunderstood the way this plugin works, but for me in the current state it doesn't make much sense. I developed a theme that I use on on several sites. The theme includes many custom post type plugins that - until now - had a configurable "slug_rewrite" option using Advanced Custom Fields (ACF PRO). Now I want to make the rewrite slugs translatable, I do not want to translate them myself, but offer the option to translate them for every language.
This seems not to be supported, neither with this plugin nor with the capabilities within Polylang.
I managed to come up with an idea but basic testing tells me that this would be a terrible way of doing it, since Polylang would need to load ALL languages on ALL requests ALL the time (unless you cache it, but still). This of course is resource hungry and not a great way of doing this.
add_filter('pll_translated_post_type_rewrite_slugs', function($post_type_translated_slugs) {
if (!isset($post_type_translated_slugs) || !is_array($post_type_translated_slugs)) {
$post_type_translated_slugs = [];
}
global $polylang;
$languages = $polylang->model->get_languages_list();
foreach ($languages as $language) {
$mo = new PLL_MO();
$mo->import_from_db($language);
$post_type_translated_slugs['test-cpt'][$language->slug] = [
'has_archive' => true,
'rewrite' => [
'slug' => $mo->translate('testslug')
]
];
}
return $post_type_translated_slugs;
});
This works but cannot possibly be the intended way?
Thanks!
I have custom taxonomy call "industrie" (french)
I want to have the slug "industry" in english.
I try you plugin with this code but I still get industrie in english version
en : industrie/machinery/
fr : /industrie/machinerie/
My code in functions.php :
add_filter('pll_translated_post_type_rewrite_slugs', function($post_type_translated_slugs) {
// Add translation for "product" post type.
$post_type_translated_slugs = array(
'industrie' => array(
'fr' => array(
'has_archive' => true,
'rewrite' => array(
'slug' => 'industrie',
),
),
'en' => array(
'has_archive' => true,
'rewrite' => array(
'slug' => 'industry',
),
),
),
);
return $post_type_translated_slugs;
});
Hi @KLicheR I found an issue using your plugin in combination with another Polylang plugin called "Polylang Slug"..
Since I don't want to duplicate content and generate confusion I'll link the post here. Please have a look at 404 Error in combination with Polylang Translate Rewrite Slugs.
Thank you.
Hi,
I've discovered this plugin yesterday. Amazing work. I spend few hours to translate own shop with Polylang plugin, because there were problems with shop (archive product) routing. Your plugin of course solves it. I want to become helpful and place here my snippets which can be usefull to create Polylang & WooCommerce Integration Plugin.
/* WOOCOMMERCE AND POLYLANG INTEGRATION */
/* Woocommerce Login Redirect */
add_filter('woocommerce_login_redirect', 'wi_login_redirect');
function wi_login_redirect( $redirect_to ) {
if(function_exists('pll_get_post')){
return get_permalink(pll_get_post(url_to_postid( $redirect_to )));
}
return $redirect_to;
}
/* Woocommerce PolyLang Proper Woocommerce Pages Routing */
add_filter('woocommerce_get_shop_page_id','pll_woocommerce_get_page_id');
add_filter('woocommerce_get_cart_page_id','pll_woocommerce_get_page_id');
add_filter('woocommerce_get_checkout_page_id','pll_woocommerce_get_page_id');
add_filter('woocommerce_get_terms_page_id','pll_woocommerce_get_page_id');
add_filter('woocommerce_get_myaccount_page_id','pll_woocommerce_get_page_id');
function pll_woocommerce_get_page_id($id){
if(function_exists('pll_get_post')){
return pll_get_post($id);
}
return $id;
}
Hey this seems like a great plugin... buuuuuuut I can't seem to get it to work at all?
I have followed the instructions (it might be time to update them? Can't find any info about implementing translations for custom archive slugs...) but I seem to be missing something. As soon as I set everything up, the re-writing of the URLs works perfectly, but all the urls give me a 404, even english (which I have set up in the filter hook) which is the same url is failing.
I know this isn't a support forum or anything but hoping someone can give me a nudge in the right direction?
function orn_cpt_slug_translate($post_type_translated_slugs) {
$post_type_translated_slugs = array(
'orn_article' => array(
'en' => array(
'has_archive' => true,
'rewrite' => array(
'slug' => 'article',
),
),
'fi' => array(
'has_archive' => true,
'rewrite' => array(
'slug' => 'artikkeli',
),
),
),
);
return $post_type_translated_slugs;
}
add_filter('pll_translated_post_type_rewrite_slugs', 'orn_cpt_slug_translate');
WP 4.3.1
Polylang 1.7.11
WP Polylang translate rewrite slugs 0.3.6
Thanks in advance for any advice
line 181
$post_language = $polylang->model->get_post_language($post->ID);
should now be:
$post_language = $polylang->model->post->get_language($post->ID);
line 282
$term_language = $polylang->model->get_term_language($term->term_id);
Should now be:
$term_language = $polylang->model->term->get_language($term->term_id);
Hi there!
I was wondering if you have time to look into the following problem, i would really appreciete it:
I have a 'overview page' called /nl/nieuws with a custom query and pagination. However when i go to /nl/nieuws/page/2 i get a 404 page.
I looked into it, and its because of the defined slug in the hook pll_translated_post_type_rewrite_slugs. I define the following code there:
$post_type_translated_slugs['news'] = array(
'nl' => 'nieuws',
'en' => 'news',
);
I get the 404 because of the nieuws i defined here. Its the same name as the overview page (for the sake of SEO / clean urls), but it wont seem to work with paging.
Do you have a solution?
Thanks in advance!
Paul from the Netherlands
Hi,
It would be great if your plugin can allow the same post link structure.
For example :
In your example :
add_filter('pll_translated_post_type_rewrite_slugs', function($post_type_translated_slugs) {
// Add translation for "product".
$post_type_translated_slugs['product'] = array(
'en' => 'products',
'fr' => 'produits',
);
return $post_type_translated_slugs;
});
I create a product with "camembert" permalink and I add the translatation but the english translation is the same. So I have "camembert-2".
It's possible to disable the functionnality of WordPress to have a solution bellow:
fr => fr/produits/camembert
en => en/products/camembert
Hi!,
Thanks for your interest in my plugin.
Unfortunately, I do not have all the time I would like to improve it. I work in a web shop and improve it when I work on projects that need it. It's one of the reason why my plugin is not on Wordpress.org yet but on GitHub. If people want to help with it, I will be happy to check pull request.
I will probably work on it again soon but can't make any promises but I assure you that this is not a "dead" project.
Hi there,
With the latest update of Polylang line 181 in the file polylang-translate-rewrite-slugs.php
Polylang instead suggest you to use:
$post_language = PLL()->model->post->get_language($post->ID);
Please note that updating this will drop support for Polylang prior to version 1.8.
Hope you update the plugin for future usage.
Thanks for nice work.
My Case:
Your plugin has translated all my custom post type slug, but it produce 404 error page for non-default language.
The default language show correct Archive page (due to still the original slug).
Any sample code for Custom Post Type Taxonomy translation? I see your code already have the code.
Thank you.
Voir branche "filter_extra_rules"
Filtrer ceci:
["(en|pl)/produits/?$"]=>
string(44) "index.php?lang=$matches[1]&post_type=product"
["produits/?$"]=>
string(35) "index.php?lang=fr&post_type=product"
["(en|pl)/products/?$"]=>
string(44) "index.php?lang=$matches[1]&post_type=product"
["products/?$"]=>
string(35) "index.php?lang=fr&post_type=product"
["(en|pl)/prowdukts/?$"]=>
string(44) "index.php?lang=$matches[1]&post_type=product"
["prowdukts/?$"]=>
string(35) "index.php?lang=fr&post_type=product"
comme ça:
["produits/?$"]=>
string(35) "index.php?lang=fr&post_type=product"
["(en)/products/?$"]=>
string(44) "index.php?lang=$matches[1]&post_type=product"
["(pl)/prowdukts/?$"]=>
string(44) "index.php?lang=$matches[1]&post_type=product"
Hi,
Nice tool, it works, thank you!
I found a php warning in a var inside function:
Warning var: $polylang->model->get_post_language(), line 181 from polylang-translate-rewrite-slugs.php deprecated in Polylang v1.8.
If we change to PLL()->model->post->get_language($post->ID) warning dissapears.
If canonical tag exists, your tool prints correct slug, so it doesn't redirect old slugs to new slug, we need add an adittional htacess redirect.
Tested in:
Wordpress: 5.4.2
Polylang: 2.7.4
Php: 7.4.2
Thanks!
PHP throws the following warning:
Warning: Illegal offset type in isset or empty in /wp-content/plugins/wp-polylang-translate-rewrite-slugs-master/polylang-translate-rewrite-slugs.php on line 347
I tried to edit the category base slug field using the following code:
add_filter( 'pll_translated_taxonomy_rewrite_slugs', function( $taxonomy_translated_slugs )
{
$taxonomy_translated_slugs = array(
// tax_name
'category' => array(
'it' => 'categoria',
'en' => 'category',
)
);
return $taxonomy_translated_slugs;
});
I updated the Permalinks and it seems that now when I change the language the slug in the url changes.
I noticed though that in the HTML code the HREFLANG tags have a problem, not showing the correct URL.
the correct URLs are:
IT: Mydomain.com/categoria/xxxxxxx
EN: Mydomain.com/en/category/yyyyyy
going to see the HREFLANG tags in the html code I find myself instead:
<link rel = "alternate" href = "Mydomain.com/xxxxxxx" hreflang = "it" />
<link rel = "alternate" href = "Mydomain.com/en/yyyyyy" hreflang = "en" />
So the slugs "category" and "category" are eliminated.
The links in the language switcher widget of Polylang also have the same problem.
Why not also update these elements?
First of all thanks for this plugin, it was really needed.
After updating to Polylang 1.4.2 the translate-rewrite-slugs plugin was giving a fatal error because the method get_languages_list()
couldn't be found. Actually it's the whole $polylang global that couldn't be found.
I changed the action hook at line 66 of polylang-translate-rewrite-slugs.php
from init to plugins_loaded, as I noticed that Polylang is also initializing its global variable using that hook.
This is the correct line which seems to work:
add_action('plugins_loaded', array($this, 'init_action'), 20);
I thought about sharing it here, hope it helps!
// Back Compat filter fix on row 321
else
$termlink = trailingslashit($termlink . $term->slug);
Hello,
Thanks for your great work.
I think I discovered a bug with your plugin.
I have a custom post type with three taxonomies.
For two of these taxonomies the translation is enabled in Polylang Settings Page and everything works correctly when I use pll_translated_taxonomy_rewrite_slugs.
But, for the one whose translation is not enabled in Polylang Settings Page, the url return by get_term_link() is not good.
By default, the url of this taxonomy is the one of the main language without the country code:
https://www.mywebsite.com/mytaxslug/myterm but when I use pll_translated_taxonomy_rewrite_slugs this url no longer works even if this taxomomy is not present in the $post_type_translated_slugs table.
When I delete add_filter(... it works again...
Thanks for your help,
Laurent
Hi there,
Scenario:
Post type called 'publication'
Overview page with custom query: /en/publications
rewrite slug filter with pll_translated_post_type_rewrite_slugs:
$post_type_translated_slugs['publication'] = array(
'nl' => 'publicaties',
'en' => 'publications',
);
When i change this to a different slug, it works.
So it seems paging does not work on a page that has the same name as the defined slug for that custom post type.
Can you reproduce this / do you have any solutions? Would be very much appreciated!
First, thanks for this useful extension.
Any news, for the known problem when the following option in polylang settings is checked/active?
"Hide URL language information for the default language"
Would be awesome if you can fix this!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.