0

I have a directory structure in my theme like this:

src/
  templates/
    pages/
      - faqs.php
    views/
      - page.php

In my custom functions I have a hook that tells WordPress to look in the src/templates/views directory for basic templates like page.php, etc. This works fine.

What I would like to do is tell WordPress to look in src/templates/pages for any custom page templates. From what I understand, WordPress scans the theme directory structure for any page templates but it only goes one-level deep.

For example, my faqs.php file has a template name of FAQs:

<?php
/*
  Template Name: FAQs
*/
?>

If I move faqs.php to the src directory it appears in the WP Admin as an available option in the Template dropdown. But when I move it back to src/templates/pages it is no longer registered.

I have looked for a variety of solutions but haven't found exactly what I'm looking for. It seemed like the theme_page_templates hook might be the answer, but I don't want to have to create the names of the template files in the select list, etc. I would prefer to just tell WordPress where to look and then let WP handle the rest from there. Not sure if that is possible though.

  • Are you aware of the top level home.php template? A page template is unnecessary – Tom J Nowell Aug 17 at 16:56
  • Yes, sorry, perhaps a bad example of a use case. I want to be able to put ALL my custom page templates in src/templates/pages and have them appear as custom page template options in the WP admin. (As opposed to having to put them all at the root of the theme directory or in a subfolder that's just one-level deep.) – David Yeiser Aug 17 at 17:02
  • Updated the example with a more generic custom page template example. – David Yeiser Aug 17 at 20:18

Your Answer

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

Browse other questions tagged or ask your own question.