Make WordPress Core

Opened 5 years ago

Closed 3 months ago

Last modified 3 weeks ago

#40547 closed enhancement (fixed)

Add a filter to get_the_post_thumbnail_url

Reported by: ibenic Owned by: SergeyBiryukov
Milestone: 5.9 Priority: normal
Severity: normal Version: 4.8
Component: Post Thumbnails Keywords: has-patch has-dev-note
Focuses: Cc:

Description

Since we have a filter for the function get_the_post_thumbnail:

return apply_filters( 'post_thumbnail_html', $html, $post->ID, $post_thumbnail_id, $size, $attr )

should we also have a filter to the function get_the_post_thumbnail_url?

I have a situation where I have made a plugin that allows setting a post_thumbnail image as an external url or similar so I am using the mentioned filter for displaying a custom post thumbnail.

If a theme uses the function get_the_post_thumbnail_url, there is no way to return the custom url for the post thumbnail.

Something like this could be helpful then:

function get_the_post_thumbnail_url( $post = null, $size = 'post-thumbnail' ) {
        $post_thumbnail_id = get_post_thumbnail_id( $post );
        if ( ! $post_thumbnail_id ) {
                return false;
        }
        return apply_filters( 'get_the_post_thumbnail_url', wp_get_attachment_image_url( $post_thumbnail_id, $size ), $post->ID );
}

Attachments (2)

40547.patch (1.1 KB) - added by ibenic 5 years ago.
40547 - Patch with filter
40547.2.diff (1.1 KB) - added by audrasjb 4 months ago.
Media: Add a hook to filter the result of get_post_thumbnail_url()

Download all attachments as: .zip

Change History (19)

@ibenic
5 years ago

40547 - Patch with filter

#1 @SergeyBiryukov
5 years ago

Hi @ibenic, welcome to WordPress Trac! Thanks for the ticket.

Related: #23983 (suggests a filter for get_post_thumbnail_id()).

If a theme uses the function get_the_post_thumbnail_url, there is no way to return the custom url for the post thumbnail.

This should be possible using get_post_metadata and wp_get_attachment_image_src as a workaround:

function wp40547_filter_post_thumbnail_id( $null, $post_id, $meta_key, $single ) {
	if ( '_thumbnail_id' !== $meta_key ) {
		return $null;
	}

	// Set a non-empty $attachment_id for the passed $post_id. Doesn't have to be an existing 
	// attachment, just a value you would check later in wp40547_filter_post_thumbnail_src().
	// $attachment_id = ...

	return $attachment_id;
}
add_filter( 'get_post_metadata', 'wp40547_filter_post_thumbnail_id', 10, 4 );

function wp40547_filter_post_thumbnail_src( $image, $attachment_id, $size, $icon ) {
	if ( 'post-thumbnail' !== $size ) {
		return $image;
	}

	// Set the image URL for the passed $attachment_id.
	// $image[0] = ...

	return $image;
}
add_filter( 'wp_get_attachment_image_src', 'wp40547_filter_post_thumbnail_src', 10, 4 );

#2 @ibenic
5 years ago

Hi @SergeyBiryukov, thank you for this suggestion.

It did not occur to me to hook into metadata for making such modifications. I'll see to implement this approach and check if it all works well or breaks other functions which require the thumbnail_id.

#3 @jackreichert
4 years ago

@SergeyBiryukov Since the get_{$meta_type}_metadata filter happens before wp_cache_get() in get_metadata you can't actually filter the data that exists, only return an alterative. Personally if I had a choice I'd opt for the filter in #23983, but why not both?

#4 @SergeyBiryukov
3 years ago

  • Milestone changed from Awaiting Review to 5.0
  • Owner set to SergeyBiryukov
  • Status changed from new to reviewing

#5 @pento
3 years ago

  • Milestone changed from 5.0 to 5.1

#6 @pento
3 years ago

  • Milestone changed from 5.1 to Future Release

Bumping this in association with #23983.

@audrasjb
4 months ago

Media: Add a hook to filter the result of get_post_thumbnail_url()

#7 @audrasjb
4 months ago

  • Keywords has-patch needs-dev-note added
  • Milestone changed from Future Release to 5.9

New patch to filter get_post_thumbnail_url().
Please note that this patch should be committed alongside the patch I previously added to #23983.

Moving for 5.9 consideration.
Adding needs-dev-note.

This ticket was mentioned in Slack in #core-media by joedolson. View the logs.


3 months ago

This ticket was mentioned in Slack in #core-media by antpb. View the logs.


3 months ago

#11 @antpb
3 months ago

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

In 52027:

Media: Add filter for post thumbnail url.

Introduces new filter post_thumbnail_url which allows overriding the default url returned from wp_get_attachement_image_url().

Props ibenic, SergeyBiryukov, jackreichert, audrasjb.
Fixes #40547.

#12 @antpb
3 months ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

Reopening to keep an eye on needing a dev note for this one. :)

This ticket was mentioned in Slack in #core by audrasjb. View the logs.


3 months ago

#14 @audrasjb
3 months ago

Closing this ticket as fixed for now, because we're going to punt unfixed enhancements from 5.9 and we don't want this one to move to Future release :D
I think the needs-dev-note workflow keyword is enough to make sure this ticket is tracked by the Docs release leads :)

#15 @hellofromTonya
3 months ago

  • Resolution set to fixed
  • Status changed from reopened to closed
Note: See TracTickets for help on using tickets.