sage: Custom templates not loading since latest update

Submit a feature request or bug report

Bug report

It looks like after the latest update of sage, the custom templates aren’t working anymore. I tested it on two different installations and for both the custom templates don’t appear under page attributes. I guess it has something to do with the move of templates/ to resources/views/ and src/ to app/ but unfortunately I wasn’t able to figure out where the problem is.

Specifically I’m talking about this template/templates resources/views/template-custom.blade.php

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 25 (23 by maintainers)

Commits related to this issue

Most upvoted comments

it looks like wordpress hardcodes looking for page templates beyond 1 level deep 😱, see http://wordpress.stackexchange.com/a/250024 / https://github.com/WordPress/WordPress/blob/503d42fe6bcd2f45b3af78657686f8a27ccb0136/wp-includes/class-wp-theme.php#L1034-L1045

from what i can tell so far, we’d have to duplicate the get_post_templates functionality and tell it to search beyond 1 folder deep

…then use the theme_page_templates filter to populate the list of template names & filenames

The following line $post_templates[$type][$file] = trim(preg_replace("/\s*(?:\*\/|\?>).*/", '', $header[1])); should be changed to $post_templates[$type][$full_path] = trim(preg_replace("/\s*(?:\*\/|\?>).*/", '', $header[1])); to maintain coherence with wordpress’ internal way of setting post template meta - and because changing it to a numerical id breaks ACF way of setting the rule for Page Templates.

oh jeeze

Alright so after playing with it for a bit I got to this working solution:

add_action('admin_init', function () {
    $templateRoot = trailingslashit(get_template_directory());
    $cache_hash = md5(trailingslashit(dirname($templateRoot)) . basename($templateRoot));
    $post_templates = [];
    $path = $templateRoot . 'resources/views/';
    $files = scandir($path);
    foreach ($files as $file) {
        if (is_dir($path . $file)) {
            continue;
        }
        if (!preg_match('|Template Name:(.*)$|mi', file_get_contents($path . $file), $header)) {
            continue;
        }

        $types = array('page');
        if (preg_match('|Template Post Type:(.*)$|mi', file_get_contents($path . $file), $type)) {
            $types = explode(',', _cleanup_header_comment($type[1]));
        }

        foreach ($types as $type) {
            $type = sanitize_key($type);
            if (!isset($post_templates[$type])) {
                $post_templates[$type] = array();
            }

            $post_templates[$type][$file] = trim(preg_replace("/\s*(?:\*\/|\?>).*/", '', $header[1]));
        }
    }
    wp_cache_add('post_templates-' . $cache_hash, $post_templates, 'themes', 1800);
}, 1);

Any feedback is most welcome. If accepted I’m happy to open a PR with it 😃

Thanks @ciromattia for pointing that out, code updated 😄