WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 5 months ago

#11571 new enhancement

Provide easy way to return url of thumbnail image

Reported by: braindrain Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 2.9
Component: Post Thumbnails Keywords: has-patch needs-testing gsoc
Focuses: Cc:

Description

I'm sure that there is probably a hack for doing this, but for those who aren't interested in custom functions, etc., I would like a simple built-in function for referencing the thumbnail image url (or a size version of it) without bringing in the predefined image tag. I tried applying a workaround, but it collided with a plug-in I was trying to use, and hacking a pre-2.9 plug-in to use the post plugin has not gone so well because of the image tag that the get_post_thumbnail function returns.

Attachments (6)

11571.diff (1.1 KB) - added by wojtek.szkutnik 4 years ago.
11571.2.diff (1.0 KB) - added by scribu 3 years ago.
cleanup
11571.3.diff (983 bytes) - added by leewillis77 3 years ago.
Revised patch updated against trunk, plus few minor changes
11571.4.diff (1.6 KB) - added by leewillis77 3 years ago.
Revised patch to address (most of) nacin's comments
11571.5.diff (1.6 KB) - added by leewillis77 3 years ago.
Lower case bools
11571.6.diff (1.8 KB) - added by leewillis77 3 years ago.
Adds filters as per scribu's comments

Download all attachments as: .zip

Change History (29)

comment:1 hakre4 years ago

  • Keywords reporter-feedback added
  • Milestone Unassigned deleted
  • Version set to 2.9

Can you please add the code of your try and provide some more information about what you tried to achieve?

comment:2 nacin4 years ago

I think the issue is that while there is a get_the_post_thumbnail() function, which corresponds to wp_get_attachment_image(), there is no get_the_post_thumbnail_src(). There is however a wp_get_attachment_image_src() function.

There probably should be a get_the_post_thumbnail_src() -- then again, there is no direct echo for wp_get_attachment_image(), while there is the_post_thumbnail().

As an alternative, you can use wp_get_attachment_image_src() with the right parameters -- check out how get_the_post_thumbnail() calls wp_get_attachment_image() for that.

comment:3 nacin4 years ago

  • Milestone set to 3.1

comment:4 mikeschinkel4 years ago

  • Cc mikeschinkel@… added

Would this work?

function get_post_thumbnail_src( $post_id, $size = 'thumbnail', $icon = false) {
	if (!has_post_thumbnail( $post_id )) {
	 	$src = false;
	} else {
	 	$size = apply_filters( 'post_thumbnail_size', $size, $post_id, $icon );
		$post_thumbnail_id = get_post_thumbnail_id( $post_id );
		list($src) = wp_get_attachment_image_src($post_thumbnail_id, $size, $icon);
	}
	return apply_filters( 'post_thumbnail_src', $src, $post_id, $post_thumbnail_id, $size, $icon );
}

comment:5 chrisscott4 years ago

Here's what I ended up doing to do this based on get_the_post_thumbnail():

function get_post_thumbnail_url( $post_id = NULL ) {
	global $id;
	$post_id = ( NULL === $post_id ) ? $id : $post_id;
	
	$post_thumbnail_id = get_post_thumbnail_id( $post_id );
	$url = false;
	
	if ( $post_thumbnail_id ) {
		$size = apply_filters( 'post_thumbnail_size', 'post-thumbnail' );
		$image = wp_get_attachment_image_src( $post_thumbnail_id, $size, false );
		
		if ( $image ) {
			list( $src, $width, $height ) = $image; 
			$url = $src;
		}
	}
	
	return $url;
}

If this looks good to add to post-thumbnail-template.php let me know and I'll submit a patch.

comment:6 wojtek.szkutnik4 years ago

  • Cc wojtek.szkutnik@… added
  • Keywords has-patch needs-testing gsoc added; reporter-feedback removed

wojtek.szkutnik4 years ago

comment:7 nacin3 years ago

  • Milestone changed from Awaiting Triage to 3.1
  • Type changed from feature request to enhancement

comment:8 jane3 years ago

  • Keywords 3.2-early added
  • Milestone changed from 3.1 to Future Release

We are past freeze, punting to be reconsidered for 3.2. Marking 3.2-early.

scribu3 years ago

cleanup

comment:9 scribu3 years ago

I cleaned up the patch.

Not sure about the 'post_thumbnail_src' filter.

If it's introduced, it should be applied everywhere, not just when calling get_the_post_thumbnail_src().

comment:10 leewillis773 years ago

Hi - I just came to add this myself as a fresh bug, then found this. I've added a .3 patch which is mostly the same as .2, but with the following changes:

  • Removed the filter as I agree with scribu that it doesn't make sense just being added here.
  • This also removes the reference to $icon which was undefined
  • Updated to call update_post_thumbnail_cache() as per get_the_post_thumbnail()

leewillis773 years ago

Revised patch updated against trunk, plus few minor changes

comment:11 downstairsdev3 years ago

+1 For @leewillis77's patch. Tested in the loop with image size and without image size.

I am wondering if we should also have the companion function for output in the loop, or if that's overkill:

/**
 * Display The Post Thumbnail Source
 *
 *
 * @param int $size Optional. Image size.  Defaults to 'post-thumbnail', which theme sets using set_post_thumbnail_src( $width, $height, $crop_flag );.
 * @param string|array $attr Optional. Query string or array of attributes.
 */
function the_post_thumbnail_src( $size = 'post-thumbnail', $attr = '' ) {
	echo get_the_post_thumbnail_src( null, $size, $attr );
}

BTW: My use case is for a responsive portfolio layout. If images have height/width attributes they scale badly with min-width:100% in CSS.

comment:12 nacin3 years ago

A few things to tweak for commit:

No need for the logic on line 112. get_post_thumbnail_id() can take the default null. Likewise we can remove that code from get_the_post_thumbnail().

Docblock needs @return string|bool Image src, or false if the post does not have a thumbnail.

We can bail with false, rather than the if/else, then the code can become un-indented a bit.

I question the lack of a need for filters. Seems to me like we may even want to duplicate the begin/end_fetch_post_thumbnail_html hooks.

comment:13 scribu3 years ago

I question the lack of a need for filters. Seems to me like we may even want to duplicate the begin/end_fetch_post_thumbnail_html hooks.

In that case, all higher post thumbnail functions should use get_the_post_thumbnail_src(), so that these new filters are applied consistently.

Last edited 3 years ago by scribu (previous) (diff)

leewillis773 years ago

Revised patch to address (most of) nacin's comments

comment:14 follow-up: leewillis773 years ago

Revised patch (11571.4.diff) which addresses most of nacin's comments. Also fixes a comment in get_the_post_thumbnail.

Happy to add the begin/end_fetch_post_thumbnail_src hooks, but not sure what the use case would be?

Not sure I know enough about the core code to pick up scribu's comments - but happy to have a bash over the next few days if that's helpful?

comment:15 SergeyBiryukov3 years ago

return FALSE;

According to #16302, booleans should be lowercased.

leewillis773 years ago

Lower case bools

comment:16 in reply to: ↑ 14 scribu3 years ago

Replying to leewillis77:

Happy to add the begin/end_fetch_post_thumbnail_src hooks, but not sure what the use case would be?

Not sure I know enough about the core code to pick up scribu's comments - but happy to have a bash over the next few days if that's helpful?

After talking with nacin on IRC, the idea is to use the '{begin|end}_fetch_post_thumbnail_html' hooks again.

That way, no other changes are necessary, either on our part or on plugin authors' part.

leewillis773 years ago

Adds filters as per scribu's comments

comment:17 SergeyBiryukov3 years ago

  • Keywords 3.2-early removed
  • Milestone changed from Future Release to 3.3

comment:18 lancewillett3 years ago

  • Cc lancewillett added

comment:19 nacin2 years ago

  • Milestone changed from 3.3 to Future Release

Too late for new APIs at this point, especially when there's a basic simple workaround: wp_get_attachment_image_src( get_post_thumbnail_id(), 'post-thumbnail' ).

comment:20 nacin2 years ago

  • Keywords 3.4-early added

comment:21 SergeyBiryukov2 years ago

  • Milestone changed from Future Release to 3.4

comment:22 nacin2 years ago

  • Keywords 3.4-early removed
  • Milestone changed from 3.4 to Future Release

Let's punt this once more. Again, simple workaround exists.

I noticed that get_the_post_thumbnail_src() would return the source, while the corresponding function wp_get_attachment_image_src() returns an array. This isn't very intuitive.

comment:23 leewillis775 months ago

get_the_post_thumbnail_src() is a convenience helper where you just want the image URL. I'd agree it's inconsistent with wp_get_attachment_image_src() - however making it consistent would lose its simplicity.

The workaround suggested is actually:

$attachment_src = wp_get_attachment_image_src( get_post_thumbnail_id(), 'post-thumbnail' );
$src = $attachment_src[0];

compared to:

$attachment_src = get_the_post_thumbnail_src();

I think there's value in the smaller convenience function over the inconsistency.

This ticket has been around for a while. Can it get in 3.9 early - if not, and there's no desire to add it - can we / should we close?

Note: See TracTickets for help on using tickets.