Make WordPress Core

Opened 10 years ago

Closed 9 years ago

Last modified 8 years ago

#28512 closed enhancement (fixed)

Make the post thumbnail size filterable for the Featured Image meta box

Reported by: drewapicture's profile DrewAPicture Owned by: drewapicture's profile DrewAPicture
Milestone: 4.4 Priority: normal
Severity: normal Version: 2.9
Component: Post Thumbnails Keywords: has-patch
Focuses: administration Cc:

Description (last modified by DrewAPicture)

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)

28512.diff (1.5 KB) - added by DrewAPicture 10 years ago.
28512-2.diff (1.6 KB) - added by dboulet 9 years ago.
28512-3.patch (1.6 KB) - added by dboulet 9 years ago.

Download all attachments as: .zip

Change History (17)

@DrewAPicture
10 years ago

#1 @DrewAPicture
10 years ago

  • Description modified (diff)

This ticket was mentioned in IRC in #wordpress-dev by DrewAPicture. View the logs.


10 years ago

#3 @SergeyBiryukov
10 years ago

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.

You can actually use image_downsize filter instead:

function change_featured_image_size_in_admin_28512( $downsize, $id, $size ) {
	if ( ! is_admin() || ! get_current_screen() || 'edit' !== get_current_screen()->parent_base ) {
		return $downsize;
	}

	remove_filter( 'image_downsize', __FUNCTION__, 10, 3 );

	$image = image_downsize( $id, 'medium' );

	add_filter( 'image_downsize', __FUNCTION__, 10, 3 );

	return $image;
}
add_filter( 'image_downsize', 'change_featured_image_size_in_admin_28512', 10, 3 );

#4 @DrewAPicture
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: @dboulet
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: @DrewAPicture
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?

@dboulet
9 years ago

#7 in reply to: ↑ 6 @dboulet
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 @johnbillion
9 years ago

  • Focuses administration added
  • Keywords dev-feedback removed
  • Milestone changed from Awaiting Review to Future Release
  • Version set to 2.9

@dboulet
9 years ago

#9 @dboulet
9 years ago

Rerolled patch.

#10 @DrewAPicture
9 years ago

  • Milestone changed from Future Release to 4.4
  • Owner set to DrewAPicture
  • Status changed from new to accepted

#11 @DrewAPicture
9 years ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 35022:

Media: Introduce the admin_post_thumbnail_size filter, which enables manipulation of the image size used to display the post thumbnail in the 'Featured Image' meta box.

This filter has no effect on the image size used to display post thumbnails on the front-end.

Props dboulet, DrewAPicture.
Fixes #28512.

#12 @DrewAPicture
9 years ago

In 35023:

Media: Remove logic for unnecessarily overloading the $content_width global when passing default dimensions used to display the post thumbnail image in the 'Featured Image' meta box.

Updates documentation for the $size parameter in admin_post_thumbnail_size filter doc.

See #28512.

#13 @DrewAPicture
9 years ago

In 35060:

Docs: Adjust documentation for the $size parameter in the admin_post_thumbnail_size hook doc to clarify the required order of width and height values when an array is passed.

See #34257. See #28512.

#14 @ocean90
8 years ago

In 38136:

Media: Remove global import for $content_width in _wp_post_thumbnail_html().

$content_width is unused since [35023].

See #28512.

Note: See TracTickets for help on using tickets.