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 ) {
}