Make WordPress Core

Opened 8 years ago

Last modified 5 days ago

#41773 new enhancement

Page Templates // Post Type Templates | Any Post Type?

Reported by: michaelecklund's profile michael.ecklund Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.7
Component: Posts, Post Types Keywords: reporter-feedback has-patch
Focuses: ui, administration, template Cc:

Description

If I add a template like this:

<?php
/*
Template Name: Full-width layout
*/

It seems to only be visible on the page Post Type in the meta box drop down.

If I add a template like this:

<?php
/*
Template Name: Full-width layout
Template Post Type: post, page, artist, album, track
*/

It's available in the meta box drop down for the specified Post Types.

I would like to add a template and have it available to all Post Types.

Perhaps add like this:

<?php
/*
Template Name: Full-width layout
Template Post Type: any
*/

Change History (5)

#1 @johnbillion
8 years ago

  • Keywords needs-patch reporter-feedback added
  • Version changed from 4.8.1 to 4.7

I thought some discussion around this had happened on #18375, but I can't immediately see it.

What's the real-world use-case for this?

#2 follow-up: @michael.ecklund
8 years ago

  • I would like to be able to control content and sidebar placement.
  • I'd rather not have 50 million static templates which are all essentially the same code.
  • I'd like the freedom and flexibility to change between: sidebar left, sidebar right, or no sidebar. No matter what type of content it is.

As a temporary solution I've been able to implement my template idea for all post types by looping all post types and using the hook: add_filter( "theme_{$post_type}_templates", "add_my_templates" );

Just seems like it would be a simple fix to add an "any" parameter. Much like WP_Query(); allows.

P.S. Why did you revert the version number from 4.8.1 to 4.7? I'm using 4.8.1 as reported.

#3 in reply to: ↑ 2 @SergeyBiryukov
8 years ago

Replying to michael.ecklund:

P.S. Why did you revert the version number from 4.8.1 to 4.7? I'm using 4.8.1 as reported.

The Version field indicates the earliest affected version, not the latest one.

Template Post Type header was introduced in [38951] for WordPress 4.7.

This ticket was mentioned in PR #8781 on WordPress/wordpress-develop by @sahilgidwani.


5 days ago
#4

  • Keywords has-patch added; needs-patch removed

Trac Ticket: https://core.trac.wordpress.org/ticket/41773

---

This patch enhances the flexibility of custom templates in WordPress themes by allowing developers to specify Template Post Type: any in the template file header. This addition automatically registers the template for all public post types, removing the need to explicitly enumerate them.

Why this change was needed:

Prior to this patch, template files using the Template Name: header would only be available for the page post type by default. If developers wanted to assign a template to additional post types, they had to use the Template Post Type: header and list each post type explicitly:

/*
Template Name: My Template
Template Post Type: post, page, book, movie
*/

However, this approach does not scale well when working with themes or plugins that need the same template across many or dynamically registered post types. Maintaining a hard-coded list becomes tedious and error-prone.

By supporting:

/*
Template Name: My Template
Template Post Type: any
*/

template authors can now register the file for all public post types without redundancy or extra logic.

Implementation Details:

The change occurs within the WP_Theme::get_post_templates() method. The following logic was added:

if ( in_array( 'any', $types, true ) ) {
    $types = get_post_types( array( 'public' => true ) );
}

This check occurs after parsing the Template Post Type: header. If any is found in the list, it dynamically replaces $types with all current public post types retrieved via get_post_types().

The rest of the logic remains unchanged and continues to associate templates with the relevant post types by sanitizing and mapping them into the $post_templates array.

#5 @sahilgidwani
5 days ago

Hi @michaelecklund ,

I've submitted a PR that proposes a core-level fix for this issue by enhancing the logic in WP_Theme::get_post_templates() to support a Template Post Type: any header. This allows a single template to be made available across all public post types without having to explicitly list them.

In the meantime, if you’d like to achieve similar functionality without modifying core, you can use the following workaround by hooking into the init action and applying the theme_{$post_type}_templates filter dynamically:

add_action('init', function () {
    $post_types = get_post_types(['public' => true], 'names');

    foreach ($post_types as $post_type) {
        add_filter("theme_{$post_type}_templates", function ($templates) {
            $templates['your-template-file.php'] = 'Your Template Label';
            return $templates;
        });
    }
});

This will register your custom template across all public post types regardless of what’s declared in the template header.

Note: See TracTickets for help on using tickets.