1

I'm trying to add a local variable to my URL.

As an example I have this URL:

mysite.com/my-page-name/

And I want to add 'en' variable into it and leave the page working properly:

mysite.com/en/my-page-name/ 

I tried to deal with it using add_rewrite_tag() and add_rewrite_rule() but it isn't working so what am I doing wrong?

add_rewrite_tag('%locale%', '^([a-z]{2})');
add_rewrite_rule('^([a-z]{2})/(.+)[/$]', 'index.php?pagename=$matches[2]', 'top');
  • Why you want to /en/. Are you trying to create your website in Multi-language. – Aftab Jul 26 '16 at 18:12
  • Hi Aftab. No, I need to have this variable in $wp->query_vars to build pages programaticaly. Some parts I need to show, some parts I need to hide. – Stanislau Ladutska Jul 27 '16 at 8:00
1

There are various ways to achieve it. The most simplest one could be to make a new page and with name "en" so the slug will be mysite.com/en/

Now make the "en" page as parent of my-page-name or in other words make my-page-name as a child of en. So slug will be mysite.com/en/my-page-name/

  • thats a good one :) – Aftab Jul 26 '16 at 20:24
  • It's not good in my case. Because I have 31 locales and several pages with it. It's too much pages... I need to have this variable in $wp->query_vars to build pages programaticaly. Some parts I need to show, some parts I need to hide. – Stanislau Ladutska Jul 27 '16 at 8:02
1

It was just a mistake in regexp in rewrite rule. This one works perfectly!

add_rewrite_rule('^([a-z]{2})\/(.+)(\/)?$', 'index.php?page=&pagename=$matches[2]&locale=$matches[1]', 'top');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.