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

Read More

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

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.

Related posts

Leave a Reply

2 comments

  1. 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' );
    
  2. WordPress already has a built-in Page Template hierarchy to handle this exact situation. You don’t need to mangle your page.php code or add a filter, just follow the naming conventions for the hierarchy:

    • custom template file : The Page Template assigned to the Page. See
      get_page_templates().
    • page-{slug}.php – If the page slug is
      recent-news, WordPress will look to use page-recent-news.php
    • page-{id}.php – If the page ID is 6, WordPress will look to use page-6.php
    • page.php
    • index.php

    from http://codex.wordpress.org/Template_Hierarchy

    The most flexible way is to use the Page Slug. Create a template, eg: page-my-slug.php and then create a new page with my-slug as it’s slug.

    In your example, ‘page-something.php’ will automatically be loaded by WordPress for the page with the slug ‘something’. If you want to use the page ID instead, from your example create ‘page-22.php’ as your template.