WordPress.org

Make WordPress Core

Opened 12 months ago

Last modified 11 months ago

#51532 new enhancement

$tag_templates should be customizable (instead of local array)

Reported by: xoomcoder Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 1.5
Component: Themes Keywords:
Focuses: Cc:

Description

In file wp-includes/template-loader.php (lines 57 - 75), a local associative array is defined: $tag_templates and it is used immediatly after at line 79 in a foreach loop.

The problem:
So there's no way for a plugin or theme to customize the value of this local variable.

Proposed enhancement:

  • create a new public property in class WP_Query called $router_templates (with same initial value as local variable $tag_templates)
<?php

// wp-includes/class-wp-query.php
class WP_Query {

    // ...
    public $router_templates = array(
                'is_embed'             => 'get_embed_template',
                'is_404'               => 'get_404_template',
                'is_search'            => 'get_search_template',
                'is_front_page'        => 'get_front_page_template',
                'is_home'              => 'get_home_template',
                'is_privacy_policy'    => 'get_privacy_policy_template',
                'is_post_type_archive' => 'get_post_type_archive_template',
                'is_tax'               => 'get_taxonomy_template',
                'is_attachment'        => 'get_attachment_template',
                'is_single'            => 'get_single_template',
                'is_page'              => 'get_page_template',
                'is_singular'          => 'get_singular_template',
                'is_category'          => 'get_category_template',
                'is_tag'               => 'get_tag_template',
                'is_author'            => 'get_author_template',
                'is_date'              => 'get_date_template',
                'is_archive'           => 'get_archive_template',
        );


}

  • add a new function in wp-includes/template.php called get_router_templates
<?php

// ...
function get_router_templates ()
{
    global $wp_query;
    return apply_filters('router_templates', $wp_query->router_templates);
}

  • upgrade the code in wp-includes/template-loader.php
<?php

// line 55
if ( wp_using_themes() ) {

    // line 57: Only modification: call function instead of defining local array    
    $tag_templates = get_router_templates();

    $template      = false;

    // Loop through each of the template conditionals, and find the appropriate template file.
    foreach ( $tag_templates as $tag => $template_getter ) {
            // ...
    }

  • Impacts:
  • There's no compatibility problem
  • Perfomance impact is really light
  • This will add more possibilities to the theme workflow.
  • I can provide diff patch if needed

If you have questions, don't hesitate to ask.

Cheers.

Change History (1)

#1 @hellofromTonya
11 months ago

  • Version changed from trunk to 1.5
Note: See TracTickets for help on using tickets.