[wp-trac] [WordPress Trac] #28512: Make the post thumbnail size filterable for the Featured Image meta box

WordPress Trac noreply at wordpress.org
Thu Jun 12 16:34:43 UTC 2014


#28512: Make the post thumbnail size filterable for the Featured Image meta box
-----------------------------+------------------------------
 Reporter:  DrewAPicture     |       Owner:
     Type:  enhancement      |      Status:  new
 Priority:  normal           |   Milestone:  Awaiting Review
Component:  Post Thumbnails  |     Version:
 Severity:  normal           |  Resolution:
 Keywords:  has-patch        |     Focuses:
-----------------------------+------------------------------
Description changed by DrewAPicture:

Old description:

> I'd like to propose making the Featured Image meta box thumbnail image
> size filterable.
>
> Currently, the only way to change the size is to basically duplicate the
> code in `_wp_post_thumbnail_html` on the `admin_post_thumbnail_html`
> filter hook. This approach is not ideal:
>
> {{{
> #!php
> <?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
> <?php
> $thumbnail_html = wp_get_attachment_image( $thumbnail_id );
> }}}
>
> I'm relying on the default image size ('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 had previously looked at adding a filter in `image_downsize()`, but I
> think the context would be far too muddled and tough to target that far
> down the call stack.
>
> I also looked at #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.

New description:

 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
 <?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
 <?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.

--

--
Ticket URL: <https://core.trac.wordpress.org/ticket/28512#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list