Use a template file for a specific url without creating a page

You can just look at url, load the file and exit.

That can be done when WordPress loaded its environment, e.g. on 'init'.

add_action('init', function() {
  $url_path = trim(parse_url(add_query_arg(array()), PHP_URL_PATH), "https://wordpress.stackexchange.com/");
  if ( $url_path === 'retail' ) {
     // load the file if exists
     $load = locate_template('template-retail.php', true);
     if ($load) {
        exit(); // just exit if template was found and loaded
     }
  }
});

Note that doing so a real page with slug “retail” can never be used.

This is pretty easy, but also hardcoded, so if you need this for a single page it’s fine. If you need to control more urls, have a look to the solution proposed in this answer.

Leave a Comment