Make WordPress Core

Opened 7 years ago

Last modified 7 years ago

#41564 new feature request

Search for hyphenated post templates for post types with underscores

Reported by: desrosj's profile desrosj Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: dev-feedback has-patch needs-testing
Focuses: template Cc:


Custom post type names adhere to the rules within sanitize_key() (lowercase alphanumeric characters, dashes, and underscores).

This means registering a post type some_post_type is perfectly fine. The archive and single templates would be be archive-some_post_type.php and single-some_post_type.php.

These file names do not adhere to the core standard for file names.

Files should be named descriptively using lowercase letters. Hyphens should separate words.

Searching for archive-some-post-type.php in addition to archive_some_post_type.php would allow this standard to be followed better.

Attachments (2)

41564.diff (594 bytes) - added by jmichaelward 7 years ago.
41564.2.diff (1.3 KB) - added by desrosj 7 years ago.

Download all attachments as: .zip

Change History (6)

#1 @SergeyBiryukov
7 years ago

  • Component changed from General to Posts, Post Types
  • Focuses template added

#2 @jmichaelward
7 years ago

@desrosj Here's a first-look patch that attempts to address this issue, by way of prepending a hyphenated archive template name within the logic of get_archive_template in template.php. When applied, this patch will allow a file to be named archive-custom_post_type.php or archive-custom-post-type.php, where the latter will take higher precedence in the template hierarchy.

To fully address the issue, I suspect that other templates - such as single - would need to be updated, as well. The larger question is whether this would be considered "code churn", but I do think that making updates to allow devs to name their templates in adherence to the WordPress Coding Standards is advisable and should be encouraged.

7 years ago

7 years ago

#3 @desrosj
7 years ago

  • Keywords has-patch needs-testing added

Thanks for the initial patch @jmichaelward! In 41564.2.diff, I added code for single templates and taxonomy archives. I think that if taxonomy-tax-name.php or single-post-type.php is searched for, then single-post-type-slug.php and taxonomy-tax-name-slug.php should probably should be searched for as well.

I had a few suggestions for changes in your patch that I made in the new patch and wanted to share them.

if ( strpos() ) could result in undesired behaviors, as 0 (first position in a string) would be considered falsy (_my_post_type, for example) false !== strpos() is a better check.

Also, instead of having to use array_unshift(), adding the value new before the current one is better.

Last edited 7 years ago by desrosj (previous) (diff)

#4 @jmichaelward
7 years ago

@desrosj Thanks for the updated patch. I think your changes and recommendations look good. Thanks!

Note: See TracTickets for help on using tickets.