I think you are using endpoints incorrectly. When a endopoint is created, a new rewrite rule is created using a query var with the same name of the endpoint; its value is taken from the string that follows the endpoint definition. So, if you register “my-endpoint” and build this URL:
/page-slug/my-endpoint/123/some-string
The endpoint is passed as my-endpoint=123/some-string
.
As far I understand from your code, you don’t need the my-endpoint
query var, so you don’t need to register it; or you can register it with false
value for query var argument (this argument is not currently documented in codex but exists in source code):
add_rewrite_endpoint( $var, EP_PERMALINK | EP_PAGES, false );
Or, if you want the endpoint only in pages:
add_rewrite_endpoint( $var, EP_PAGES, false );
Also, you check 'my-endpoint' == $key
buth then include $var
as endpoint name and as part in the URL, not $key
.
Without knowing what $this->query_vars
and get_some_page_id()
are, I can not test your code exactly but this should work:
foreach ( $this->query_vars as $key => $var ) {
if ( 'my-endpoint' == $key ) {
add_rewrite_tag( '%some_id%', '([^&]+)' );
add_rewrite_tag( '%some_string%', '([^&]+)' );
$page_id = get_some_page_id();
$page_slug = get_post( $page_id )->post_name;
// e.g. /page-slug/my-endpoint/123/some-string/
// $key is equal to my-endpoint here because of the previous check
add_rewrite_rule(
"^{$page_slug}/{$key}/([^/]*)/([^/]*)/?$",
'index.php?page_id=' . $page_id . '&some_id=$matches[1]&some_string=$matches[2]',
'top'
);
}
}
Note: Do not forget to flush rewrite rules before testing this code.