Make WordPress Core

Opened 4 years ago

Last modified 2 years ago

#51532 new enhancement

$tag_templates should be customizable (instead of local array)

Reported by: xoomcoder's profile xoomcoder Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 1.5
Component: Themes Keywords: has-patch
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 (4)

#1 @hellofromTonya
4 years ago

  • Version changed from trunk to 1.5

#2 @audrasjb
2 years ago

  • Keywords needs-patch added

Hello @xoomcoder and welcome to WordPress Core Trac!

Would you like to propose a patch for this ticket?
You can simply send a PR against the wordpress-develop Github repository. It would be much appreciated. Thanks!

(anyone is also welcome to jump on this)

This ticket was mentioned in PR #3737 on WordPress/wordpress-develop by applh.


2 years ago
#3

  • Keywords has-patch added; needs-patch removed

…ray $tag_templates.

  • Simple 1 line of code addition:
  • add new apply_filters to allow customization of $tag_templates by plugins/themes

Trac ticket: https://core.trac.wordpress.org/ticket/51532

#4 @applh
2 years ago

Hello.
Let's start with a simple "1 line of code" addition to implement this trac.
Thanks for your help.
LH

Note: See TracTickets for help on using tickets.