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.

Share Improve this question edited Nov 7, 2018 at 2:29 l6ls asked Nov 7, 2018 at 1:36 l6lsl6ls 3311 gold badge3 silver badges10 bronze badges 6
  • 1 Try 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 on init. 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:48
  • @JacobPeattie i am refresh the ask. Please read it. – l6ls Commented Nov 7, 2018 at 2:22
  • 1 If you’re using an endpoint, just adding ‘en’ and using get_query_var( 'en' ) would be the way to do it. You wouldn’t use ‘lang’. – Jacob Peattie Commented Nov 7, 2018 at 2:35
  • @JacobPeattie ty, but i am how to detect eg/en/ or eg/ because /en/-null- don't fire the get_query_var('en') for this i want isset() but i try isset not work with functions. – l6ls Commented Nov 7, 2018 at 3:02
  • You can try array_key_exists( 'en', $wp_query->query_vars ) to detect an empty endpoint. – Milo Commented Nov 7, 2018 at 4:36
 |  Show 1 more comment

1 Answer 1

Reset to default 2

Query 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