7

I have a fresh installation of wordpress, and I wrote some quick 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 my sidebar translated with WPML and String Translation in the most primitve 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 while trying to make a translation to other language (let's say italian), the wpml refuses to save my changes to the review links..like my redirection rule is affecting it somehow.

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 both the review and the buy links were changed.. but after I hit save, it only saves the change i made in the buy href, but it reverts my change to the review link, and it looks like this after I save -

<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 hit save, it removes the /first-asset part from my translation and now it leads to an empty page (/it/assets) .. I am wondering if it may be cause as a result of the rewrite..

Gonras Karols
  • 1,150
  • 10
  • 30

2 Answers2

4

An alternate way to approach this:

Listen to your customer's browser setting.

locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);

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

You can then 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 redirects for languages, and you can accept languages as your website user wants to see it.

Tschallacka
  • 27,901
  • 14
  • 88
  • 133
1

If you test your expression online you'll see that your regex is removing the first-asset part of the URL.

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

This will return /it/assets/.

Andrew Schultz
  • 4,092
  • 2
  • 21
  • 44