Make WordPress Core

Opened 3 years ago

Last modified 11 months ago

#49779 new enhancement

Add a filter that controls the size of the images on the Attachment pages

Reported by: pikamander2's profile pikamander2 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: major Version:
Component: Media Keywords: reporter-feedback
Focuses: template Cc:

Description

The images (attachments) on the Attachment pages are hardcoded as "medium".

On narrow themes, that might be fine, but on wider themes it leads to a tiny-looking image with a lot of awkward whitespace.

Many users have wanted to change the size of the image on that page, which has lead to multiple guides suggesting that they replace the hardcoded value in /wp-includes/post-template.php

Examples of such guides:

The problem with that solution, of course, is that the changes will be overwritten when the next WordPress update is released, leading to a lot of confused and frustrated users.

To fix that, we could apply a filter to the attachment size.

Original /wp-includes/post-template.php code:

$p .= wp_get_attachment_link( 0, 'medium', false );
$p .= '</p>';

Modified code:

$attachment_size = apply_filters( 'prepend_attachment_size', 'medium' );
$p .= wp_get_attachment_link( 0, $attachment_size, false );
$p .= '</p>';

After that, it's very easy for users to change the attachment image's size in their child theme's functions.php file.

Example:

add_filter('prepend_attachment_size', 'set_attachment_image_size_to_large');

function set_attachment_image_size_to_large()
{
    return 'large';
}

Attachments (1)

post-template.php.patch (565 bytes) - added by pikamander2 3 years ago.
Add attachment page image size filter to fix #49779

Download all attachments as: .zip

Change History (9)

@pikamander2
3 years ago

Add attachment page image size filter to fix #49779

#1 @SergeyBiryukov
3 years ago

  • Component changed from General to Media

#2 @pikamander2
3 years ago

@SergeyBiryukov - Any chance of this making it into 5.5, or is it too late for that?

#3 @pikamander2
2 years ago

@SergeyBiryukov - Would it be possible to get this filter added in the next major release?

This ticket was mentioned in Slack in #core-media by antpb. View the logs.


11 months ago

#5 @joedolson
11 months ago

Is there any reason you can't resolve this problem via either editing or adding an attachments template to your theme?

The theme's attachment.php template file allows you to pass whatever size you want for the attachment page; I can't see a reason you'd ever need to edit core files for this.

More details: https://developer.wordpress.org/themes/template-files-section/attachment-template-files/

If there's something special about the scenario that means you can't solve this within the theme, please provide those details.

#6 @joedolson
11 months ago

  • Keywords reporter-feedback added

#7 @joedolson
11 months ago

  • Focuses template added

#8 @pikamander2
11 months ago

I'm sure that it's also possible to edit the attachment file directly, but adding a filter here would create a very simple, straightforward way to edit the image sizes that's in line with many of WordPress's other filters.

A common use-case is that somebody downloads a theme, generates a child theme for it, and then makes a few simple edits to the child theme's functions.php without ever needing to dig into other files. This also allows them to keep their custom code consolidated in one easy place for easier access/review later on. Not everyone's looking for a total theme overhaul with customized template files.

So while it may not be strictly necessary, I do think that it would beneficial for the sake of ease-of-use.

Note: See TracTickets for help on using tickets.