I created a plugin that sets up a custom route and then loads a template file for that url. Everything works fine, except that WordPress seems to think it's a 404 even though it's correctly rendering my template.
For instance it says 404 in the document title and an error404
class is added to <body>
The custom url is domain.com/path/:id where :id
is a dynamic value corresponding to a post id, so the URL could be domain.com/path/275. In the example below some_id
is used as the post id variable.
Here's a reduced version of my plugin:
<?php
class MyPlugin {
public function __construct() {
add_action( 'init', array($this, 'add_response_endpoint') );
add_filter( 'template_include', array($this, 'add_response_template') );
}
public function add_response_endpoint() {
add_rewrite_rule(
'^path/([0-9]+)/?',
'index.php?pagename=my_custom_url&some_id=$matches[1]',
'top'
);
add_rewrite_tag('%some_id%', '([^&]+)');
}
public function add_response_template($template) {
if ( get_query_var( 'pagename' ) === 'my_custom_url' ) {
$template = trailingslashit( dirname( __FILE__ ) ) . 'templates/custom-page-template.php';
}
return $template;
}
}
new MyPlugin();
Am I missing something here? Or should I start looking for this bug elsewhere?
my_custom_url
is not the slug of an actual page, you will get a 404. Ifsome_id
is a valid post ID, then set the correct native query vars with that ID so the query succeeds.