WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 3 years ago

#39030 closed enhancement (fixed)

Pass post object or post ID to post_thumbnail_size filter

Reported by: wido Owned by: NathanAtmoz
Milestone: 4.9 Priority: normal
Severity: normal Version:
Component: Post Thumbnails Keywords: good-first-bug has-patch has-unit-tests
Focuses: template Cc:

Description

As per title, an edit to the filter post_thumbnail_size as follow:

        /**
	 * Filters the post thumbnail size.
	 *
	 * @since 2.9.0
	 *
	 * @param string|array $size The post thumbnail size. Image size or array of width and height
	 *                           values (in that order). Default 'post-thumbnail'.
         * @param WP_Post $post The post object
	 */
	$size = apply_filters( 'post_thumbnail_size', $size, $post );

In this way we can retrieve additional info by the post to filter the size of the post outside of a loop.

Yes, there is a filter named post_thumbnail_html but in that case the purpose of the filter is to filter the html string.

Attachments (2)

39030.1.diff (833 bytes) - added by NathanAtmoz 3 years ago.
first pass at a patch
39030.diff (4.1 KB) - added by flixos90 3 years ago.

Download all attachments as: .zip

Change History (7)

#1 @SergeyBiryukov
3 years ago

  • Keywords needs-patch good-first-bug added
  • Milestone changed from Awaiting Review to 4.9

@NathanAtmoz
3 years ago

first pass at a patch

#2 @NathanAtmoz
3 years ago

  • Keywords has-patch added; needs-patch removed

#3 @flixos90
3 years ago

  • Keywords needs-unit-tests added
  • Owner set to NathanAtmoz
  • Status changed from new to assigned

Thanks for the patch @NathanAtmoz, looks good!

Please note that the parameter description as well as the since annotation in the doc block need to end with a dot. However, you don't necessarily need to update the patch, since this can be done pre-commit. Just something to be aware of. :)

@flixos90
3 years ago

#4 @flixos90
3 years ago

  • Keywords has-unit-tests added; needs-unit-tests removed
  • Version 4.7 deleted

39030.diff passes the post ID instead of post object, to have consistency with the other filters in the function, and also in order to prevent the filter from changing object properties on the original instance.

Tests for the filter usage including the new parameter have been added too.

#5 @flixos90
3 years ago

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

In 41267:

Post Thumbnails: Pass post ID to post_thumbnail_size filter.

In addition to the enhancement, tests for the filter usage including the new parameter have been added.

Props NathanAtmoz.
Fixes #39030.

Note: See TracTickets for help on using tickets.