Make WordPress Core

Opened 8 years ago

Closed 2 years ago

#38160 closed enhancement (wontfix)

Attachment template hierarchy selects generic attachment.php before specific post type templates

Reported by: johnbillion's profile johnbillion Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Themes Keywords: needs-unit-tests
Focuses: template Cc:

Description

When viewing a single attachment at its permalink, the template hierarchy is as follows (using foo.jpg as an example).

  1. image-jpeg.php
  2. jpeg.php
  3. image.php
  4. attachment.php
  5. single-attachment-foo.php
  6. single-attachment.php
  7. single.php
  8. singular.php
  9. index.php

Note that the generic attachment.php appears higher in the hierarchy than the more specific single-attachment-foo.php. This means it's impossible to target a specific attachment if an attachment.php file is in place.

Attachments (1)

38160.diff (695 bytes) - added by birgire 7 years ago.

Download all attachments as: .zip

Change History (3)

@birgire
7 years ago

#1 @birgire
7 years ago

  • Keywords needs-patch removed

In 38160.diff the get_attachment_template() and get_single_template() are modified accordingly. There are various ways to handle this, but I think this approach has the smallest code change that I could think of.

I looked into creating a test, but stumbled upon a problem.

The plan is to create a theme under tests/phpunit/data/themedir1/38160 with files like attachment.php and single-attachment-foo.php appart from index.php and style.css.

Then switch to that theme with:

$theme = get_theme( '38160' );
$this->assertNotEmpty( $theme );
switch_theme( $theme['Template'], $theme['Stylesheet'] );

where we could add our test method to the Tests_Admin_includesTheme class to be able to use the setUp there src, or move the similar setup to another class.

Then create an attachment and go to that attachment page.

$attachment_id = self::factory()->attachment->create_object( 'foo.jpg', 0, array(
    'post_name'      => 'foo',
    'post_mime_type' => 'image/jpeg',
) );

$this->go_to( get_permalink( $attachment_id ) );

Then try using get_attachment_template() and get_single_template() to verify the wanted results.

But here locate_template() only returned an empty string, because the TEMPLATEPATH was stuck for the default theme tests/phpunit/includes/../data/themedir1/default.

I checked the tests in Tests_Theme::test_switch_theme():

https://core.trac.wordpress.org/browser/tags/4.8.2/tests/phpunit/tests/theme.php#L285

that uses get_attachment_template() and get_single_template(), but they are also only returning an empty string, because the TEMPLATEPATH is also stuck there for the default theme.

Maybe I'm missing something here, but I will have to look into the tests further.

Version 0, edited 7 years ago by birgire (next)

#2 @johnbillion
2 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

It would be nice to fix this but there's no sign of any demand for it.

Note: See TracTickets for help on using tickets.