#28512 closed enhancement (fixed)
Make the post thumbnail size filterable for the Featured Image meta box
Reported by: | DrewAPicture | Owned by: | DrewAPicture |
---|---|---|---|
Milestone: | 4.4 | Priority: | normal |
Severity: | normal | Version: | 2.9 |
Component: | Post Thumbnails | Keywords: | has-patch |
Focuses: | administration | Cc: |
Description (last modified by )
I'd like to propose we make the image size used to in the Featured Image meta box filterable.
Currently, the only way to change the size is by duplicating the code in _wp_post_thumbnail_html
on the admin_post_thumbnail_html
hook. This approach is not ideal:
<?php function change_thumbnail_size( $content, $post_id ) { $thumbnail_id = get_post_thumbnail_id( $post_id ); $upload_iframe_src = esc_url( get_upload_iframe_src( 'image', $post_id ) ); $set_thumbnail_link = '<p class="hide-if-no-js"><a title="' . esc_attr__( 'Set featured image' ) . '" href="%s" id="set-post-thumbnail" class="thickbox">%s</a></p>'; $content = sprintf( $set_thumbnail_link, $upload_iframe_src, esc_html__( 'Set featured image' ) ); if ( $thumbnail_id && get_post( $thumbnail_id ) ) { $thumbnail_html = wp_get_attachment_image( $thumbnail_id ); if ( ! empty( $thumbnail_html ) ) { $ajax_nonce = wp_create_nonce( 'set_post_thumbnail-' . $post_id ); $content = sprintf( $set_thumbnail_link, $upload_iframe_src, $thumbnail_html ); $content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail" onclick="WPRemoveThumbnail(\'' . $ajax_nonce . '\');return false;">' . esc_html__( 'Remove featured image' ) . '</a></p>'; } } return $content; } add_filter( 'admin_post_thumbnail_html', 'change_thumbnail_size', 10, 2 );
Note this line:
<?php $thumbnail_html = wp_get_attachment_image( $thumbnail_id );
I'm relying on the default image size of 'thumbnail' in wp_get_attachment_image()
. My specific use case is to display the actual featured image size that will be used in the theme as a method to reduce confusion for content managers.
I looked at adding a filter to image_downsize()
further down the call stack, but determined the context would be too-easily muddled.
Related-ish is #20205, which proposes making the return of wp_get_attachment_image_src()
filterable, though that wouldn't really help here as you're still basically required to regenerate the return markup.
Attachments (3)
Change History (17)
This ticket was mentioned in IRC in #wordpress-dev by DrewAPicture. View the logs.
10 years ago
#4
@
10 years ago
- Keywords dev-feedback added
@SergeyBiryukov: I still think this worth pursuing, if only because the workarounds are bordering on cleverness. Patch still applies.
Please feel free to close if you disagree.
#5
follow-up:
↓ 6
@
9 years ago
I’d like to see this added as well. In my case, I’d like to display a different size for the featured image, depending on the post type. If I have a theme which displays different post thumbnail sizes for different post types, it would be nice to have the Featured Image meta box display the image in the proper size. To this end, it would be helpful if the $post variable was also passed in as an argument for the filter proposed by @DrewAPicture.
#6
in reply to:
↑ 5
;
follow-up:
↓ 7
@
9 years ago
Replying to dboulet:
I’d like to see this added as well. In my case, I’d like to display a different size for the featured image, depending on the post type. If I have a theme which displays different post thumbnail sizes for different post types, it would be nice to have the Featured Image meta box display the image in the proper size. To this end, it would be helpful if the $post variable was also passed in as an argument for the filter proposed by @DrewAPicture.
Care to update the patch with your suggested change?
#7
in reply to:
↑ 6
@
9 years ago
Replying to DrewAPicture:
Care to update the patch with your suggested change?
Done. Haven’t submitted a patch before, hope that it’s properly formatted.
#8
@
9 years ago
- Focuses administration added
- Keywords dev-feedback removed
- Milestone changed from Awaiting Review to Future Release
- Version set to 2.9
You can actually use
image_downsize
filter instead: