Make WordPress Core

Opened 14 years ago

Closed 9 years ago

Last modified 9 years ago

#11571 closed enhancement (duplicate)

Provide easy way to return url of thumbnail image

Reported by: braindrain's profile braindrain Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.9
Component: Upload 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 14 years ago.
11571.2.diff (1.0 KB) - added by scribu 13 years ago.
cleanup
11571.3.diff (983 bytes) - added by leewillis77 13 years ago.
Revised patch updated against trunk, plus few minor changes
11571.4.diff (1.6 KB) - added by leewillis77 13 years ago.
Revised patch to address (most of) nacin's comments
11571.5.diff (1.6 KB) - added by leewillis77 13 years ago.
Lower case bools
11571.6.diff (1.8 KB) - added by leewillis77 13 years ago.
Adds filters as per scribu's comments

Download all attachments as: .zip

Change History (33)

#1 @hakre
14 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?

#2 @nacin
14 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.

#3 @nacin
14 years ago

  • Milestone set to 3.1

#4 @mikeschinkel
14 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 );
}

#5 @chrisscott
14 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.

#6 @wojtek.szkutnik
14 years ago

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

#7 @nacin
13 years ago

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

#8 @jane
13 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.

@scribu
13 years ago

cleanup

#9 @scribu
13 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().

#10 @leewillis77
13 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()

@leewillis77
13 years ago

Revised patch updated against trunk, plus few minor changes

#11 @downstairsdev
13 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.

#12 @nacin
13 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.

#13 @scribu
13 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 post thumbnail functions should use get_the_post_thumbnail_src(), so that these new filters are applied consistently.

Version 0, edited 13 years ago by scribu (next)

@leewillis77
13 years ago

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

#14 follow-up: @leewillis77
13 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?

#15 @SergeyBiryukov
13 years ago

return FALSE;

According to #16302, booleans should be lowercased.

@leewillis77
13 years ago

Lower case bools

#16 in reply to: ↑ 14 @scribu
13 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.

@leewillis77
13 years ago

Adds filters as per scribu's comments

#17 @SergeyBiryukov
13 years ago

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

#18 @lancewillett
13 years ago

  • Cc lancewillett added

#19 @nacin
12 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' ).

#20 @nacin
12 years ago

  • Keywords 3.4-early added

#21 @SergeyBiryukov
12 years ago

  • Milestone changed from Future Release to 3.4

#22 @nacin
12 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.

#23 @leewillis77
10 years 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?

#24 in reply to: ↑ description @bigwhailwa
9 years ago

  • Component changed from Post Thumbnails to Upload

Replying to braindrain:

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.

#25 @downstairsdev
9 years ago

Looks like this was resolved in:
https://core.trac.wordpress.org/ticket/33070

Good to close?

#26 @leewillis77
9 years ago

  • Resolution set to duplicate
  • Status changed from new to closed

Well #33070 looks like it's still rumbling on - so I'd hesitate to say that it's resolved ... but I'm happy to close this as a duplicate since #33070 seems to have more traction ...

#27 @netweb
9 years ago

  • Milestone Future Release deleted
Note: See TracTickets for help on using tickets.