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');
2
  • Why you want to /en/. Are you trying to create your website in Multi-language.
    – Aftab
    Commented Jul 26, 2016 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. Commented Jul 27, 2016 at 8:00

2 Answers 2

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/

2
  • thats a good one :)
    – Aftab
    Commented Jul 26, 2016 at 20:24
  • 1
    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. Commented Jul 27, 2016 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');
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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