[wp-trac] [WordPress Trac] #49779: Add a filter that controls the size of the images on the Attachment pages

WordPress Trac noreply at wordpress.org
Thu Apr 2 11:52:39 UTC 2020


#49779: Add a filter that controls the size of the images on the Attachment pages
-------------------------+-----------------------------
 Reporter:  pikamander2  |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  General      |    Version:
 Severity:  major        |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 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:

 * https://zolahost.com/change-wordpress-attachment-page-image-size/

 * https://wordpress.org/support/topic/attachment-page-image-size-3/

 * https://www.dmuth.org/fixing-image-sizes-on-wordpress-attachment-pages/

 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';
 }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/49779>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list