WordPress.org

Make WordPress Core

Opened 7 years ago

Closed 4 years ago

#21791 closed enhancement (wontfix)

Enhancement: locate_template() support for multiple file types (not just .php)

Reported by: benallfree Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Themes Keywords:
Focuses: template Cc:

Description

I have modified locate_template() to allow location of file types other than .php. The core currently only searches for .php extensions for template file names (search.php, 404.php, etc). I have a HAML parser, and this mod allows the core to locate .haml files (search.haml, 404.haml). Then I can use the 'template_include' filter to handle that file type.

Do you think this is the best way to accomplish it? I also made the function a little more DRY.

function locate_template($template_names, $load = false, $require_once = true ) {
  $located = '';
  $template_paths = array(
   STYLESHEETPATH,
   TEMPLATEPATH
  );
  $template_file_types = apply_filters('template_file_types', array('php'));
  foreach ( (array) $template_names as $template_name ) {
    if ( !$template_name )
      continue;
    foreach($template_file_types as $template_file_type)
    {
      $template_name_try = str_replace('.php', '.'.$template_file_type, $template_name);
      foreach($template_paths as $template_path)
      {
        $template_name_try = $template_path . '/' . $template_name_try;
        if ( file_exists($template_name_try)) {
          $located = $template_name_try;
          break 3;
        }
      }
    }
  }

  if ( $load && '' != $located )
    load_template( $located, $require_once );

  return $located;
}

you think this modification would be a good core enhancement?

Change History (5)

#1 @benallfree
7 years ago

After further testing, this solution is incomplete because it interferes with the template file structure and capability discovery process.

For example, renaming 'index.php' in a template folder makes it appear broken to WordPress. Also, support for page templates is missing if they don't have the correct file extension or commenting at the top.

If different template file formats were to be supported, we would need to hook into the template structure/capability discovery process.

#2 @SergeyBiryukov
7 years ago

  • Component changed from General to Template
  • Version trunk deleted

#3 @nacin
6 years ago

  • Component changed from Template to Themes
  • Focuses template added

#4 @obenland
6 years ago

  • Keywords close added

locate_template() is not specific to PHP files. It can be passed an array of any file names and it will go through them, check if they exist, and include them.

The scenario described above could be managed by either the template_include or the {$type}_template filter.

#5 @chriscct7
4 years ago

  • Keywords close removed
  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

See comment:4

Note: See TracTickets for help on using tickets.