Thanks to toschos hint I could solve the problem. This is the code that I use:
// Register the endpoint
function my_init() {
add_rewrite_endpoint('votes', EP_ALL);
}
add_action('init', 'my_init');
// Execute a function when the endpoint is called
function my_redirect() {
global $wp_query;
if (isset($wp_query->query_vars['votes'])) {
$wp_query->is_404 = false;
add_filter( 'wp_title', 'my_title_votes', 10, 2 );
// this will output the template-file "all-votes.php"
get_template_part( 'all-votes' );
// Prevent WordPress from adding the front-page or 404 page to my output...
die();
}
}
add_action( 'template_redirect', 'my_redirect' );
function my_title_votes( $title, $sep ) {
return 'Votes-Title ' . $sep . ' ' . $title;
}
When I open the URL mysite.com/votes/ I get my custom output, even though there is no page with that slug 🙂