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?

  • 1
    You would need to create an HTAccess redirect to the file, then process the request there.
    – Howdy_McGee
    Mar 30, 2015 at 15:09
  • I dont have access to the .htaccess file in this case. Is there any other way?
    – Keat
    Mar 30, 2015 at 15:13
  • there are plugins that may give you access to do these type of things, either way you'll need to upload a custom file to the server so it has something to hit.
    – Howdy_McGee
    Mar 30, 2015 at 15:16
  • 4
    Not sure about all of the details of your project but the first things that come to mind are template_redirect or an endpoint
    – s_ha_dum
    Mar 30, 2015 at 15:29

4 Answers 4


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.

  • Thanks for your answer. I can not get the above code to work. This is never true: "if ( $url_path === 'retail' ) {" The $url_path is a string with the value: "example.com/retail"
    – Keat
    Apr 1, 2015 at 9:42
  • Try to var_dump($url_path); and see how it looks like and adjust the code. That value depends on your real url. E.g. for custom home url, or WP installed in subfolder may be different. @Keat
    – gmazzap
    Apr 1, 2015 at 9:45
  • 1
    Thanks for the reply, and sorry for the late answer. I used this solution and it works perfect. $url_path = trim(parse_url(add_query_arg(array()), PHP_URL_PATH), '/'); $templatename = 'retail'; $pos = strpos($url_path, $templatename); if ($pos !== false) {
    – Keat
    Apr 13, 2015 at 13:28
  • Hi, I have just tried this and it loaded my template fine but I notice that a few other bits in the page are not working ie I have a echo do_shortcode() in the header but it's not working and the actual shortcode is being displayed - is this something to do with Wordpress not initialising these once the template has loaded due to the exit()?
    – Adrian
    Sep 20, 2020 at 22:25
  • 2
    Further to previous comment, have changed the hook from init to wp_loaded and shortcodes are now loading.
    – Adrian
    Sep 20, 2020 at 22:32

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;

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


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: - https://developer.wordpress.org/files/2014/10/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>';

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.