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

The question:

I wonder if its possible to use a template file for a specific url without having to create a page for that template.

This is my simplified problem:

I have created a page in WP with some link content that points to a specific url with some trailing form data: (

I want that url (retail) to automatically use my template file template-retail.php that I have in the child theme directory, without having to create a page named ”retail” & select the template page from there. There is only external content in the template-retail.php file, nothing from WordPress itself.

Is this possible?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

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), '/');
  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.

Method 2

The init action isn’t appropriate for what you’re trying to achieve. You should be using the template_include filter instead. You’d combine this with get_query_var to retrieve the URL params to check what template needs to be loaded. Here are the links:


add_filter( 'template_include', 'portfolio_page_template', 99 );

function portfolio_page_template( $template ) {

    if ( is_page( 'portfolio' )  ) {
        $new_template = locate_template( array( 'portfolio-page-template.php' ) );
        if ( '' != $new_template ) {
            return $new_template ;

    return $template;

Method 3

The WordPress-way to do this is with page-templates.

You only need a code for the WordPress template. In your WordPress theme you can create a page template and rename it to


That particular page will automatically pick it up and use the template.

For example if your page has an id of 5874 you’ll name the template as page-5784.php

You can also name the template based on the page slug. For example if the page slug is hello-world then the template name will be page-hello-world.php

Also see:

Method 4

@shivanand-sharma this is perfect and cleaner method ( to create any page like any other in wordpress, and if you want to hide your page, i just use the simple and efective plugin ‘

I must say that i was in need of a URL to make POST or GET to my own page and save some session data ‘WC()->session’, and this solves this and other problems, because you can have a backbone of custom php code including all the ‘require(‘wp-load’) etc’ of the entire wordpress, woocommerce etc to work with through, …..

You just need to :

First: Create a file inside your theme location as a template of the new page something like ‘wp-content/themes/mytheme/customtemplate.php’ (Comment is important so the ‘Template Name’ can be observed by WordPress) :

<?php /* Template Name: WhateverName */ 
echo 'Hello World';echo '</br>';

Second: Create a page at wordpress normally through ‘wp-admin’ > Pages (Let’s say a name like MYPAGE, or you can change the slug whatever you want) and off course link the previous template as the template of this page which is name ‘WhateverName’ on the template attributtes section.

So, Let’s open the new page ‘’ and you will see.

Hello World
object(WC_Session_Handler)#880 .....................

Extras: Let’s create javascript or jquery functions in cart, checkout, whatever you can imagine inside ‘script’ HTML tags, and include code like this:

var data = { action : actionName, dataA : etcA, dataB : etcB}
    type:     'post',
    url:      'index.php/MYPAGE',
    data:     data,
    success:  function( response ) {
    complete: function() {

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment