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

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: (mysite.com/retail/?a=test&b=1234).

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?

Solutions Collecting From Web of "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), '/');
  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.

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:

Code:

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;
}

The WordPress-way to do this is with page-templates. https://developer.wordpress.org/themes/template-files-section/page-template-files/

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

page-id.php

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:
http://img.mywordpresstips.com/url-rewriting/template-hierarchy.png

@shivanand-sharma this is perfect and cleaner method (https://developer.wordpress.org/themes/template-files-section/page-template-files/) 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 ‘https://wordpress.org/plugins/exclude-pages/’

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, mysite.com/index.php/MYPAGE …..

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>';
var_dump(WC()->session); 
var_dump($_POST);
var_dump($_GET);
?>

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 ‘mysite.com/index.php/MYPAGE’ 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}
$.ajax({
    type:     'post',
    url:      'index.php/MYPAGE',
    data:     data,
    success:  function( response ) {
    },
    complete: function() {
    }
});