Wordpress, Redirects, and WPML

I have a new wordpress installation and I wrote some rewrite rule in my functions.php that looks like this:

// "rewrite" /assets/xyz to /assets/?asset=xyz function assets_rewrite_rule($rules) { global $wp_rewrite; $asset_rule = array( // (not tested) 'assets/(.+)/?' => 'index.php?pagename=Assets&asset=$matches[1]' ); return array_merge($asset_rule, $rules); } add_filter('page_rewrite_rules', 'assets_rewrite_rules'); 

And I have a sidebar translated using WPML and String Translation in the most primitive way.

Here's the English part (not translated) of my sidebar -

 <div class="sideall"> <div class="sidebar-mai1"> <div class="sidebar-name"> <img style="width: 25px; margin-right: 10px; margin-bottom: -7px;" title="first asset" src="/wp-content/images/sidbar-assets/firstasset.png" alt="first asset" />First Asset</div> <div class="sidebar-btn"> <a class="btn-sidebar" href="/assets/first-asset/">Review</a> <a class="btn-sidebar1" href="/buy/first-asset">Trade Now!</a></div> <div style="clear: both;"></div> </div> 

And, trying to translate into another language (say, Italian), wpml refuses to save my changes in the review links ... since my redirection rule somehow affects it.

Here's the translation I added to the sidebar -

 <div class="sideall"> <div class="sidebar-mai1"> <div class="sidebar-name"> <img style="width: 25px; margin-right: 10px; margin-bottom: -7px;" title="first asset" src="/wp-content/images/sidbar-assets/firstasset.png" alt="first asset" />First Asset</div> <div class="sidebar-btn"> <a class="btn-sidebar" href="/it/assets/first-asset/">Revisione</a> <a class="btn-sidebar1" href="/buy-it/first-asset">Scambia ora!</a></div> <div style="clear: both;"></div> </div> 

As you can see, the review and buy links have been changed. But after I clicked the "Save" button, it only saves the changes made to buy href, but it changes my change to a review link, and it looks like after saving -

 <div class="sideall"> <div class="sidebar-mai1"> <div class="sidebar-name"> <img style="width: 25px; margin-right: 10px; margin-bottom: -7px;" title="first asset" src="/wp-content/images/sidbar-assets/firstasset.png" alt="first asset" />First Asset</div> <div class="sidebar-btn"> <a class="btn-sidebar" href="/it/assets">Revisione</a> <a class="btn-sidebar1" href="/buy-it/first-asset">Scambia ora!</a></div> <div style="clear: both;"></div> </div> 

As you can see, after I clicked on the “Save” button, it removes the / first -asset part from my translation, and now this leads to an empty page (/ it / assets). I am wondering if this could be the reason as a result of the rewriting ..

+7
php wordpress wpml
source share
2 answers

An alternative way to approach this:

Listen to your client’s browser settings.

 locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']); 

https://secure.php.net/manual/en/locale.acceptfromhttp.php

Then you can rewrite it to a function like this:

 function my_get_langauge() { static $lang; if(is_null($lang)) { $lang = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2)); /** * Check list of allowed/accepted languages or revert to default */ if(!in_array($lang, ['nl','en','de','it']) ) { $lang = 'en'; } } return $lang; } 

This way, you don’t have to worry about redirecting to languages, and you can accept languages ​​because the user of your website wants to see it.

+4
source share

If you test your expression on the Internet, you will see that your regular expression removes the first part of the URL.

 Regular Expression: assets\/(.+)\/? Test String: /it/assets/first-asset/ 

This will return /it/assets/ .

+1
source share

All Articles