I’m developing a plugin that create some pages and set a specific template for each of these pages.
It creates a page in which it sets a template for 404 – not found, how can set this specifc page as WordPress 404 (so it will be displayed when needed)?
1 Answer
You can filter 404_template
and return the path of your custom file from the plugin directory like this:
add_filter(
'404_template',
fn() => plugin_dir_path( __FILE__ ) . '404.php'
);
This hook exists only when a 404 happens, so you don’t need any custom detection.