You can achieve this with a rewrite rule, you just need to exit execution before the main query is run.
First, add a query var that will get set by the rewrite rule:
function wpd_query_vars( $query_vars ){
$query_vars[] = 'my_var';
return $query_vars;
}
add_filter( 'query_vars', 'wpd_query_vars' );
Next, the rewrite rule:
function wpd_rewrite_rule(){
add_rewrite_rule(
'^my-url/?$',
'index.php?my_var=true',
'top'
);
}
add_action( 'init', 'wpd_rewrite_rule' );
Lastly, we hook parse_request
and check for our custom var, run our code, and exit:
function wpd_parse_request( &$wp ){
if ( array_key_exists( 'my_var', $wp->query_vars ) ){
echo 'do something';
exit;
}
return;
}
add_action( 'parse_request', 'wpd_parse_request' );