Make WordPress Core

Opened 19 months ago

Last modified 14 months ago

#49779 new enhancement

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

Reported by: pikamander2 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: major Version:
Component: Media Keywords:
Focuses: Cc:


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.


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 19 months ago.
Add attachment page image size filter to fix #49779

Download all attachments as: .zip

Change History (4)

19 months ago

Add attachment page image size filter to fix #49779

#1 @SergeyBiryukov
19 months ago

  • Component changed from General to Media

#2 @pikamander2
19 months ago

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

#3 @pikamander2
14 months ago

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

Note: See TracTickets for help on using tickets.