1

I'm wondering if it's in any way possible to categorize page templates and display them in the dropdown by category. In other words, having page templates defined as being for parent pages that show up together with some sort of heading and page templates for child pages that do the same.

Mostly, I'm just wanting to know if there's a way to organize page templates in the dropdown.

Thanks!

3
  • How did you go with this Jesse?
    – Tim Malone
    Jul 6, 2016 at 4:07
  • I didn't end up needing it after all. Jul 7, 2016 at 0:54
  • Haha ok. Thanks for replying nonetheless. Hopefully it will help someone else! Please do consider accepting an answer on some of your questions too, it will help avoid these questions continuning to come up in our unanswered lists (eg. this one and this one). Thanks!
    – Tim Malone
    Jul 7, 2016 at 1:03

1 Answer 1

1

Great question. As far as I can tell there isn't a way to filter this list (there's a long trac ticket about a proposal to, if you're interested, and also related (but different) questions on this site here and here).

Because there's no filter, you may have to resort to doing it with jQuery.

We can add our own scripts to the admin footer using the admin_print_footer_scripts action:

add_action("admin_print_footer_scripts", "wpse_227485_categorise_page_templates");

function wpse_227486_categorise_page_templates(){
  ?>
  <script>
    // run scripts here
  </script>
  <?php
}

What we then need to figure out is how to access and categorise the templates.

Looking at the source for the post editor page, the templates are inside the #pageparentdiv in a select element called #page_template. So we can access its options like so:

jQuery("#pageparentdiv #page_template option")

Using this, you can effectively 'categorise' the templates in a custom way by adding additional blank, disabled options above the item you know you wish to categorise it at, for example:

jQuery("#pageparentdiv #page_template option[value='my-template.php']")
  .before("<option disabled='disabled'>My Template Heading</option>");

Putting the above block of code in place of the section marked // run scripts here in the first block above will add 'My Template Heading' as an unselectable option above wherever the template 'my-template.php' is in the list.

Of course, add as many of these wherever you need them :)

It's important to note this isn't going to be dynamic in any way - but that could be coded in depending on exactly what you wanted. There's no built-in way in Wordpress to have templates defined as being for parent pages/child pages etc., but if you needed this to be dynamic you could theoretically approach that in different ways such as adding a custom variable to the top of the template files and then reading them before generating this script.

Your Answer

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

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