Can I create a page template, use it once, then hide/remove the option to use it again?

I am working on a WP theme/project that includes several custom page templates that will only be used once each. For example, there is a page with a map and search form for finding things on the map. Clearly the map page needs a unique page template, but that template will never be used for any other pages. I would like to hide or remove this page template from the “Template” drop-down menu, so when the client is selecting page templates they only see options they should select from. Anyone know how to do that, or should I be approaching this from an entirely different angle?

Thanks,
Kirkland

++++++++++++++++

UPDATED – Solution:

For each of the page templates that I wanted to use one time only (ie: during setup, before the client had access) I simply removed…

/*
Template Name: NAME_HERE
*/

So that it wouldn’t show as an option when adding a new page. Then I replaced everything in page.php with this…

if ( is_front_page() ) { include('page-home.php'); }
elseif ( is_page(22) ) { include('page-something.php'); }
elseif ( is_page(48) ) { include('page-whatever.php'); }
else{ include('page-default.php'); }

Everything that was in page.php gets moved to page-default.php. Or you could just put it all inside the else statement at the end. Either way.

This works for me because I will be doing the setup for the client, so I will be creating the “one time only” pages before handing this project off to them – I will know the page IDs.

BTW… You do not need to keep the naming scheme page-XXX.php for this to work. I just did that so I can group them more easily.

I left the “Template Name” comment in the pages I do want the client to be able to select when adding pages (ex: “full width” page-full.php) and did not add a if condition or include to them in page.php.

2 Answers
2

If a page won’t work without a specific template, I would just remove the need for them to select a template. Filter template_include and select the template based on the requested page:

function wpse50455_template_include( $template ) {
    // check if it's a page
    if ( is_page() ):
        $this_page_id = get_query_var( 'page_id' );
        // check for presence of meta data to determine what template you need
        // return the desired template if required
        return get_stylesheet_directory_uri() . '/my-special-template.php';
    endif;

    return $template;
}
add_filter( 'template_include', 'wpse50455_template_include' );

Leave a Comment