WordPress.org

Make WordPress Core

Opened 7 months ago

Last modified 8 weeks 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:

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

Download all attachments as: .zip

Change History (4)

@pikamander2
7 months ago

Add attachment page image size filter to fix #49779

#1 @SergeyBiryukov
7 months ago

  • Component changed from General to Media

#2 @pikamander2
7 months ago

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

#3 @pikamander2
8 weeks ago

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

Note: See TracTickets for help on using tickets.