0

I have an i18n class.

The class find lang tag. For example: "?lang=en"

And i want it work with subdir. For example: eg.com/en and eg.com/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.com/anypost/lang/en this work now. But i want to remove lang slug /lang/

eg.com/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
    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 Nov 7 '18 at 1:48
  • @JacobPeattie i am refresh the ask. Please read it. – l6ls Nov 7 '18 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 Nov 7 '18 at 2:35
  • @JacobPeattie ty, but i am how to detect eg.com/en/ or eg.com/ 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 Nov 7 '18 at 3:02
  • You can try array_key_exists( 'en', $wp_query->query_vars ) to detect an empty endpoint. – Milo Nov 7 '18 at 4:36
1

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.com/page/
get_query_var( 'en' );        // ''
get_query_var( 'en', false ); // false

// example.com/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 ) {

}

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.