WordPress.org

Make WordPress Core

Opened 8 years ago

Closed 7 years ago

Last modified 7 years ago

#21891 closed enhancement (duplicate)

Need ability to filter page templates

Reported by: mrwweb Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.4.2
Component: Template Keywords: has-patch
Focuses: Cc:

Description

The old way of removing a parent theme's templates (as documented here, here, and here) broke with the big theme-handling rewrite in 3.4.

The primary use case of a new filter would be to allow a child theme to remove or rename a parent theme page template in the Dashboard. It's possible that part of the get_page_templates() function might have to be tweaked to allow for the scenario where one or more pages is using a template that no longer "exists," but I'm not sure.

Hopefully this is as simple as adding apply_filters( 'page_templates', $page_templates ); somewhere in class-wp-theme.php.

Attachments (2)

21891.patch (467 bytes) - added by ethitter 8 years ago.
Introduces the page_templates filter in WP_Theme::get_page_templates()
21891.2.patch (765 bytes) - added by ethitter 8 years ago.
Introduces the page_templates_to_remove filter

Download all attachments as: .zip

Change History (13)

@ethitter
8 years ago

Introduces the page_templates filter in WP_Theme::get_page_templates()

#1 @ethitter
8 years ago

  • Cc erick@… added
  • Keywords has-patch needs-testing added

#2 @westi
8 years ago

I like the idea of a simple way to suppress page templates in child themes using a filter.

I think it would be cleaner to implement this as a filter of an array of template names to remove which defaults to an empty array and therefore make it explicitly a removal filter rather than a function result filter.

#3 @mrwweb
8 years ago

That's an interesting thought to make it just a removal filter. Are there other places in WordPress that do that or would this be more in the direction of creating some function like wp_remove_template( $templates );?

One other possible use case of this filter (which I haven't confirmed but want to play with) would be the ability to "create" templates without associated files. I think I can be talked into thinking this is a bad idea, but I've always dreamed of the ability to let people use the "Template" select interface to trigger changes that only require filters or actions done on page.php. (I also described this idea here.)

I'm almost sure that the current patch would be insufficient for this use case, but maybe it's worth considering for now or down the road.

#4 follow-up: @nacin
8 years ago

The bulk actions list table filter only allows removals, not adds. It also allows the name to be changed (but not the identifying slug/key). This approach also then allows for a future change to make the filter fully functional.

This is a duplicate of #13265. There, I mention some potential issues for allowing arbitrary/filtered page templates: http://core.trac.wordpress.org/ticket/13265#comment:14. We need a sample plugin that fully implements this.

@ethitter
8 years ago

Introduces the page_templates_to_remove filter

#5 follow-up: @ethitter
8 years ago

As a simple second pass at this, 21891.2.patch introduces a page template removal filter along the lines of what Nacin suggested.

#6 in reply to: ↑ 5 ; follow-up: @nacin
8 years ago

Replying to ethitter:

As a simple second pass at this, 21891.2.patch introduces a page template removal filter along the lines of what Nacin suggested.

That's what westi suggested. I pointed toward http://core.trac.wordpress.org/browser/tags/3.4.2/wp-admin/includes/class-wp-list-table.php#L279.

#7 in reply to: ↑ 6 @ethitter
8 years ago

Replying to nacin:

That's what westi suggested.

Indeed. Apparently I can't read.

I think a removal filter is sufficient here, though undoubtedly someone will argue that being able to rename templates would be helpful. Worth pursuing?

#8 @mrwweb
8 years ago

As the guy who opened the ticket and then the possibly derailed the conversation (sorry about that), I think starting with just a removal filter (maybe including renaming, maybe not) is totally appropriate. I can understand why adding templates would be way more complicated, and, because the use-case for and implications of a removal filter seem much simpler, I think it's better to just move forward with that and get into a release sooner rather than later.

To return to where I started, this is a feature request, but it's to replace a function that "broke" in 3.4. Hence, patching for just removal seems like a good way to go forward.

#9 in reply to: ↑ 4 @SergeyBiryukov
8 years ago

  • Keywords close added

Suggest closing in favor of #13265, which now has a sample plugin per the request mentioned in comment:4.

#10 @c3mdigital
7 years ago

  • Keywords needs-testing close removed
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #13265.

#11 @SergeyBiryukov
7 years ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.