最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

permalinks - Adding URL prefix for foreign language support

programmeradmin0浏览0评论

I've done some research to use it for my own complicated system (Using Plugin or Functions.php).

In fact, what I want to do is add a prefix at the very beginning of the URL structure of WordPress and read it as a Query Variable when necessary.


For example:

DEFAULT: localhost/hello-world

EN: localhost/en/hello-world

FR: localhost/fr/category/apple

DE: localhost/de/2019/

And more...


The basic logic here is to add a predetermined list of languages, such as array('en', 'fr', 'ru', 'de'), as a frontend.

But actually localhost/en/hello-world should provide me with the query localhost/hello-world?Lang=en in the background.

At this point: I have tried the following function but I have no results.

function custom_rewrite_basic () {
  add_rewrite_rule ('^([0-9]+)/?', 'index.php?lang=$matches[1]', 'top');
  flush_rewrite_rules(false);
}
add_action('init', 'custom_rewrite_basic');

After all this, I wanted to do something like,

<?php add_rewrite_endpoint($name, $places); ?>

But this function adds the end of the URL instead of the beginning. I dont want this. Also this function doesn't return me a query like "lang = en".

<?php add_rewrite_endpoint('en', EPP_ALL); ?>

I'm not sure what I'm gonna do in the end.

All I want is,

If a query such as "/en/" or "lang=en" is made, pass the corresponding variable to my wordpress queries.

I've done some research to use it for my own complicated system (Using Plugin or Functions.php).

In fact, what I want to do is add a prefix at the very beginning of the URL structure of WordPress and read it as a Query Variable when necessary.


For example:

DEFAULT: localhost/hello-world

EN: localhost/en/hello-world

FR: localhost/fr/category/apple

DE: localhost/de/2019/

And more...


The basic logic here is to add a predetermined list of languages, such as array('en', 'fr', 'ru', 'de'), as a frontend.

But actually localhost/en/hello-world should provide me with the query localhost/hello-world?Lang=en in the background.

At this point: I have tried the following function but I have no results.

function custom_rewrite_basic () {
  add_rewrite_rule ('^([0-9]+)/?', 'index.php?lang=$matches[1]', 'top');
  flush_rewrite_rules(false);
}
add_action('init', 'custom_rewrite_basic');

After all this, I wanted to do something like,

<?php add_rewrite_endpoint($name, $places); ?>

But this function adds the end of the URL instead of the beginning. I dont want this. Also this function doesn't return me a query like "lang = en".

<?php add_rewrite_endpoint('en', EPP_ALL); ?>

I'm not sure what I'm gonna do in the end.

All I want is,

If a query such as "/en/" or "lang=en" is made, pass the corresponding variable to my wordpress queries.

Share Improve this question asked Aug 20, 2019 at 6:53 BilwoBilwo 751 silver badge4 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 1

Are you sure you have correct regex? Also you shouldn't use flush_rewrite_rules() on init hook. Better to just go and re-save permalinks options in admin, so you won't flush rules every time.

Try to change regex to all chars, otherwise it looks good.

add_rewrite_rule ('^([^/]*)/?', 'index.php?lang=$matches[1]', 'top');

UPDATE:

and then you need to add parameter to allowed query vars

function custom_rewrite_basic_query_vars( $query_vars ){
    $query_vars[] = 'lang';
    return $query_vars;
}
add_filter( 'query_vars', 'custom_rewrite_basic_query_vars' );

in theme you can then work with query like this

$lang = urldecode(get_query_var('lang'));
发布评论

评论列表(0)

  1. 暂无评论