admin管理员组文章数量:1290963
I have an i18n class.
The class find lang tag. For example: "?lang=en"
And i want it work with subdir. For example: eg/en
and eg/hello-world/en
For this i am using: add_rewrite_endpoint
function lang_add_endpoints(){
global $wp_rewrite;
add_rewrite_endpoint('en', EP_ALL);
$wp_rewrite->flush_rules();
}
add_action('init', 'lang_add_endpoints');
But the class work with lang
tag. For this i am using: add_rewrite_rule
function custom_rewrite_basic() {
global $wp_rewrite;
add_rewrite_rule('^en/?', 'index.php?lang=en', 'top');
$wp_rewrite->flush_rules();
}
add_action('init', 'custom_rewrite_basic');
But i tested $_GET['lang']
don't set.
So i want, all: page, post, archive and home pages support "/en/" subdir. And this fire $_GET["lang"] = "en"
Now i solve with:
function lang_add_endpoints(){
add_rewrite_endpoint('lang', EP_ALL);
}
add_action('init', 'lang_add_endpoints');
and
if(get_query_var('lang')) $_GET["lang"] = get_query_var('lang');
eg/anypost/lang/en
this work now. But i want to remove lang slug /lang/
eg/anypost/en
this how to fire $_GET["lang"] = "en"
i try: get_query_var('en')
don't set, because /en/
haven't a value.
I think if(isset(get_query_var('en')))
like this solve it.
I have an i18n class.
The class find lang tag. For example: "?lang=en"
And i want it work with subdir. For example: eg/en
and eg/hello-world/en
For this i am using: add_rewrite_endpoint
function lang_add_endpoints(){
global $wp_rewrite;
add_rewrite_endpoint('en', EP_ALL);
$wp_rewrite->flush_rules();
}
add_action('init', 'lang_add_endpoints');
But the class work with lang
tag. For this i am using: add_rewrite_rule
function custom_rewrite_basic() {
global $wp_rewrite;
add_rewrite_rule('^en/?', 'index.php?lang=en', 'top');
$wp_rewrite->flush_rules();
}
add_action('init', 'custom_rewrite_basic');
But i tested $_GET['lang']
don't set.
So i want, all: page, post, archive and home pages support "/en/" subdir. And this fire $_GET["lang"] = "en"
Now i solve with:
function lang_add_endpoints(){
add_rewrite_endpoint('lang', EP_ALL);
}
add_action('init', 'lang_add_endpoints');
and
if(get_query_var('lang')) $_GET["lang"] = get_query_var('lang');
eg/anypost/lang/en
this work now. But i want to remove lang slug /lang/
eg/anypost/en
this how to fire $_GET["lang"] = "en"
i try: get_query_var('en')
don't set, because /en/
haven't a value.
I think if(isset(get_query_var('en')))
like this solve it.
1 Answer
Reset to default 2Query parameters added using add_rewrite_endpoint()
are available using get_query_var()
.
So if you register /en/
as an endpoint:
function wpse_318560_en_endpoint(){
add_rewrite_endpoint('en', EP_ALL);
}
add_action('init', 'wpse_318560_en_endpoint' );
And don't flush rewrite rules on init
. Rewrite rules only need to be flushed once. See this note in the developer docs for a way to properly flush rewrite rules programmatically.
Then you can use get_query_var()
to check if the endpoint is present.
One important thing to note is that get_query_var
returns the value of the query var. When adding an endpoint, the 'value' of the endpoint is whatever comes after the /en/
in the URL. In your use-case this will be an empty string: ''
. The thing to be careful of is that the default value of get_query_var()
when /en/
is missing is also ''
.
So to properly check if /en/
is set in the URL, you need to use the 2nd argument of get_query_var()
to change the default value to false
:
// example/page/
get_query_var( 'en' ); // ''
get_query_var( 'en', false ); // false
// example/page/en/
get_query_var( 'en' ); // ''
get_query_var( 'en', false ); // ''
So to properly check for /en/
you need to do it like this:
if ( get_query_var( 'en', false ) !== false ) {
}
本文标签: rewrite rulesAssociate the quotaddrewriteendpointquot and quotGETquot
版权声明:本文标题:rewrite rules - Associate the "add_rewrite_endpoint" and "$_GET" 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741504832a2382258.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
get_query_var( 'lang' )
. WordPress removes any query args it recognises from$_GET
and puts them in 'query vars'. Also, don't flush rewrite rules oninit
. Either do it manually by going to Settings > Permalinks, or do it once. See this note as an example. – Jacob Peattie Commented Nov 7, 2018 at 1:48get_query_var( 'en' )
would be the way to do it. You wouldn’t use ‘lang’. – Jacob Peattie Commented Nov 7, 2018 at 2:35eg/en/
oreg/
because/en/-null-
don't firethe get_query_var('en')
for this i wantisset()
but i tryisset
not work with functions. – l6ls Commented Nov 7, 2018 at 3:02array_key_exists( 'en', $wp_query->query_vars )
to detect an empty endpoint. – Milo Commented Nov 7, 2018 at 4:36