0

I have custom page templates that are not commonly used. How do I go about hiding these templates from selection in the 'Page attributes > Template' drop-down?

Unset is not an option as modifications to any pages using unset templates will revert to the default template.

Is there a way to 'hide' and not disable specific templates?

Edit: It looks like even using js to hide the selection you still get the same problem of it reverting to default template.

1

To answer my own question, I think the only solution is to hide the page template selector and build my own that only shows templates I wish to add.

  • +1 but you probably can filter the templates when they are being saved as well, but I have a feeling it will not be easier than writing your own metabox for that. – Mark Kaplun May 4 '18 at 9:46
1

The list of available templates is generated by get_page_templates. By the end of this function you see a filter that allows you to modify the output. You can use that to change it under certain conditions like this:

add_filter ('theme_page_templates','wpse302662_hide_templates', 10, 4);

function wpse302662_hide_templates ($post_templates, $this, $post, $post_type) {
  $templates_to_hide = array ('template-1', 'template-2');
  $current_template = get_page_template_slug ($post->ID);
  if (!in_array ($current_template, $templates_to_hide) {
    // remove unwanted templates from $post_templates
    }
  return $post_templates;
  }

Edited code to reflect Jacob's suggestion below. Some extra logic in the condition and the removal action may be necessary to reflect precisely when you want which templates to be hidden.

  • I'm pretty sure it's not an answer here. OP clearly stated that he don't won't to disable these templates and your code will do exactly that... And if you think that it is the same question as 302574 (it is not), then mark it as duplicate... ;) – Krzysiek Dróżdż May 4 '18 at 10:31
  • 1
    It should be possible to use this method to only unset the template if the current page's template is not that value. Which seems like a perfectly good approach to deprecating templates to me. – Jacob Peattie May 4 '18 at 12:16
  • It won't disable them, just make them unavailable in the dropdown. – cjbj May 4 '18 at 13:04
  • @cjbj yes, but if you unset the template and some page uses it, then it will go back to default in editor, so it will be impossible to edit such pages ;) – Krzysiek Dróżdż May 5 '18 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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