We’ve a shopper who has had a model new website developed by one other company, it’s multilingual through some customized fields and a few primary logic to show the related language through a easy language selector. No plugins concerned.
The difficulty is that the shopper needs to make sure that their website has website positioning that targets the varied international locations/languages they serve, however the present implementation just isn’t website positioning pleasant in any way on account of it being virtually locale-adaptive (ie no particular URLs for every language).
As a result of prices, the shopper just isn’t keen to rebuild the whole website with a plugin reminiscent of polylang/WPML and many others, and we have to determine an answer that may virtually be dropped in and combine with their present fields/translations.
Our company has proposed the next – we merely create a question variable “lang” that’s then prepended to URLs if the chosen language just isn’t English, so if French is chosen, website.com/web page turns into website.com/fr/web page, website.com/blog-category/publish turns into website.com/fr/blog-category/publish and many others. We will detect the question variable and serve the suitable language.
I’ve received this partially working with some rewrite guidelines which I’ve little expertise working with, the place lang is a two character question variable:
// Register lang question var
operate wf_register_query_vars( $vars ) {
$vars[] = 'lang';
return $vars;
}
add_filter( 'query_vars', 'wf_register_query_vars' );
// Add rewrite guidelines
operate wf_rewrite_tag_rule() {
add_rewrite_tag( '%lang%', '([a-z]{2})' );
add_rewrite_rule( '^([a-z]{2})/?$', 'index.php?lang=$matches[1]', 'high' );
add_rewrite_rule( '^([a-z]{2})/?$', 'index.php?lang=$matches[1]&page_id=12', 'high' ); // Residence web page
add_rewrite_rule( '^([a-z]{2})/instance/?$', 'index.php?lang=$matches[1]&post_type=instance', 'high' ); // Instance CPT Archive
add_rewrite_rule( '^([a-z]{2})/instance/(.?.+?)(/[0-9]+)?/?$', 'index.php?lang=$matches[1]&instance=$matches[2]', 'high' ); // Instance CPT
add_rewrite_rule( '^([a-z]{2})/(.?.+?)(/[0-9]+)?/?$', 'index.php?lang=$matches[1]&title=$matches[2]&web page=$matches[3]', 'backside' ); // Posts
add_rewrite_rule( '^([a-z]{2})/(.?.+?)(/[0-9]+)?/?$', 'index.php?lang=$matches[1]&pagename=$matches[2]&web page=$matches[3]', 'high' ); // Pages
}
add_action('init', 'wf_rewrite_tag_rule', 10, 0);
With the above, the house web page works (it is a static web page set as the house web page), as do single “publish” post-types, and single “instance” publish sorts. Pages do not work nevertheless (404), and haven’t but begun wanting into archives, classes, tags, taxonomies and many others.
If I swap the order of the underside two (posts and pages), posts will cease working (404), however pages will begin working.
So the query is, is there a method so as to add a rewrite rule that simply covers the whole lot – pages, posts, archives, customized publish sorts, customized taxonomies, pagination and many others?
I can deal with all different logic (serving 404’s for non-existent translations, producing hreflangs, altering WP’s locale, html lang attribute and many others), it’s simply the rewrite guidelines which have left me completely stumped!