Make WordPress Core

Opened 11 years ago

Closed 3 years ago

Last modified 3 years ago

#23983 closed enhancement (fixed)

Add filter to get_post_thumbnail_id to override default thumbnail use

Reported by: jesper800's profile Jesper800 Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 5.9 Priority: normal
Severity: normal Version:
Component: Media Keywords: has-patch has-dev-note
Focuses: Cc:

Description

The current function for getting the post thumbnail ID, used in among others get_the_post_thumbnail, is as follows:

function get_post_thumbnail_id( $post_id = null ) {
	$post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
	return get_post_meta( $post_id, '_thumbnail_id', true );
}

In my opinion, this needs a filter, so the user can override this by the attachment of his choosing, such as an Advanced Custom Fields image attached to the post.

Something like:

function get_post_thumbnail_id( $post_id = null ) {
	$post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
	return apply_filters( 'post_thumbnail_id', get_post_meta( $post_id, '_thumbnail_id', true ), $post_id );
}

I know you can hook into the get_{$meta_type}_metadata filter, but getting the post thumbnail ID should still be possible by using get_post_meta, the other thumbnail should just be used for displaying.

Attachments (3)

23983.patch (548 bytes) - added by gilbitron 9 years ago.
23983.2.patch (814 bytes) - added by sebastian.pisula 8 years ago.
23983.2.diff (825 bytes) - added by audrasjb 3 years ago.
Media: Add a hook to filter get_post_thumbnail_id() result

Download all attachments as: .zip

Change History (29)

#1 @alexvorn2
11 years ago

this needs a filter, so the user can override this by the attachment of his choosing, such as an Advanced Custom Fields image attached to the post

This can confuse people.

Last edited 11 years ago by alexvorn2 (previous) (diff)

#2 @engelen
10 years ago

  • Keywords has-patch added

(formerly Jesper800)

Why would this confuse people? Hooking directly into get_post_meta would confuse people, but allowing a function return value to be filterable shouldn't. We're requesting the featured image for a post, not a raw database value. Allowing get_the_title to be filterable doesn't confuse people either, does it?

In my opinion this should be filterable, as it provides flexibility in a currently quite rigid thumbnail system. For example, one might want to use the thumbnail of a parent post if the post itself doesn't have one. Adding a filter adds more flexibility, there are more than enough realistic use cases, and it doesn't add any confusion.

In dire need of a second opinion :-).

#3 @wonderboymusic
10 years ago

  • Keywords needs-patch added; has-patch removed

@gilbitron
9 years ago

#4 @gilbitron
9 years ago

  • Keywords needs-testing added; needs-patch removed

#5 @sebastian.pisula
8 years ago

Second example: Via filter we can set the default thumbnail if post not set featured image

#6 @sebastian.pisula
8 years ago

  • Keywords has-patch added

#7 @SergeyBiryukov
7 years ago

Related: #40547 (suggests a filter for get_the_post_thumbnail_url()).

#8 @sebastian.pisula
7 years ago

I haven't control to overwrite meta value via get_post_metadata filter. Why? Because I can't check that post has _thumbnail_id.

In my filter I can check:

<?php
if (empty ($ post_thumbnail_id)) {
$post_thumbnail_id = 5;
}

return $post_thumbnail_id;

Additional solution is compatibility with has_post_thumbnail function :-)

#9 follow-up: @leogermani
7 years ago

+1 for this

hooking in get_%type%_metadata is messy, because you can not easily check wether the post has a post thumbnail or not inside your filter, you would end up in an endless loop if you call has_post_thumbnail()

In that case you have to do something like inside your callback:

$wpdb->get_var("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = '_thumbnail_id' AND post_id = $object_id");

not really beautiful.

#10 @SergeyBiryukov
6 years ago

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

#11 @rzen
6 years ago

Adding my voice as a +1 to this. There have been so many cases over the years where I've desired to override the post thumbnail behavior by specifying an alternative post image.

Being able to filter this value as well as the has_post_thumbnail() are a necessary part of that process since different plugins or themes may (properly) be checking for has_post_thumbnail() before utilizing get_the_post_thumbnail() or get_the_post_thumbnail_url().

I don't see a ticket for the latter, so I'm going to go ahead and create that one now.

#12 in reply to: ↑ 9 @SergeyBiryukov
6 years ago

Replying to leogermani:

hooking in get_%type%_metadata is messy, because you can not easily check wether the post has a post thumbnail or not inside your filter, you would end up in an endless loop if you call has_post_thumbnail()

You can avoid an endless loop by removing and re-adding the filter:

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

	if ( is_admin() ) {
		return $null;
	}

	remove_filter( 'get_post_metadata', __FUNCTION__, 10, 4 );

	$featured_image_id = get_post_thumbnail_id( $object_id );

	add_filter( 'get_post_metadata', __FUNCTION__, 10, 4 );

	// The post has a featured image.
	if ( $featured_image_id ) {
		return $featured_image_id;
	}

	// Do something else
	...

	return $featured_image_id;
}
add_filter( 'get_post_metadata', 'wp23983_get_featured_image_id', 10, 4 );

I agree it's not pretty though, and a filter would be helpful.

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


6 years ago

#14 @joemcgill
6 years ago

A filter makes sense to me, although I'm unsure if filtering the ID is the correct approach, since someone might also want to replace the thumbnail with an outside source that doesn't have an ID. However, adding a filter to get_post_thumbnail_id() before the post meta lookup seems like a fine enhancement.

#15 @peterwilsoncc
6 years ago

  • Milestone changed from 5.0 to 5.1

Moving to the 5.1 milestone due to the WordPress 5.0 focus on the new
editor (Gutenberg).

#16 @pento
6 years ago

  • Milestone changed from 5.1 to Future Release

Bumping, as this needs further consideration as to whether it's the correct solution.

#17 @audrasjb
3 years ago

  • Keywords needs-dev-note added; 2nd-opinion needs-testing removed
  • Milestone changed from Future Release to 5.9

New patch to filter get_post_thumbnail_id().
Please note that this patch should be committed alongside the patch I'll add to #40547.

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

@audrasjb
3 years ago

Media: Add a hook to filter get_post_thumbnail_id() result

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


3 years ago

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


3 years ago

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


3 years ago

#22 @antpb
3 years ago

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

In 52028:

Media: Add filter for post thumbnail id.

Introduces new filter post_thumbnail_id which allows overriding the default id returned from get_post_thumbnail_id().

Props engelen, alexvorn2, gilbitron, sebastianpisula, SergeyBiryukov, leogermani, rzen, joemcgill, audrasjb.
Fixes #23983.

#23 @antpb
3 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

Reopening this to keep an eye on it for dev notes.

#24 @audrasjb
3 years ago

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

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 :)

Note: See TracTickets for help on using tickets.