Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I know how to set the size of an image type:

set_post_thumbnail_size( 150, 150 )

..or using the media settings, but how do I get the image with and image height that have been set for an image size?

I'd like something like:

get_post_thumbnail_size(); // return array(150, 150)

how should this be done? Is it by using get_option(), and what would the options keys be for thumbnail width and height?

share|improve this question

7 Answers 7

up vote 7 down vote accepted

When an image size is added either by WordPress(add_image_size), or by a plugin or your own custom code it gets added into the $_wp_additional_image_sizes global, i can't find a similar function for pulling data from that global, but you could certainly just look inside the global to determine what width and height a registered image size has.

Example:

global $_wp_additional_image_sizes;
// Output width
echo $_wp_additional_image_sizes['post_thumbnail']['width'];
// Output height
echo $_wp_additional_image_sizes['post_thumbnail']['height'];

Of course just be sure to reference those values at a point after you've set the size. So if for example you're setting the thumbnail size during init, you'll need to make sure to do whatever you need to after that point(else you'll get back the sizes as they were prior to setting them).

Hope that helps.

share|improve this answer
    
It appears to be post-thumbnail rather than post_thubmnail (WP3.6) –  user2019515 Sep 23 '13 at 9:38

There is however another way:

get_option( 'thumbnail_size_w' );
get_option( 'thumbnail_size_h' );

Note: thumbnail is one of the sizes set inside $_wp_additional_image_sizes. So the call to get_option() will work for every other image size too.

share|improve this answer
3  
This is actually the best option because plugin can run their hooks on it. –  toscho Feb 10 '12 at 13:55
1  
That's why I am surprised it hasn't been mentioned yet. –  Mark Feb 10 '12 at 14:00

Keep in mind that this global $_wp_additional_image_sizes; might not work as expected. For example when using is in plugin it will not return the additional sizes that a Themes have registered.

share|improve this answer
    
It will if you make the call later enough in the action sequence, ie. if the theme registers the sizes on the init action, your call must be made after that action has occured. –  t31os Apr 11 '13 at 19:08

As an addition to why it's such a pain to retrieve these numbers, a core enchancement that contains a finished patch. Sadly it's still not in core.

Just follow the progress of this ticket: Wordpress Trac: Enchance get_intermediate_image_sizes() to contain not only the names, but the sizes too.

share|improve this answer

The only thing still missing is the thumbnail's ID:

$post_thumbnail_id = get_post_thumbnail_id( $post_id );
$attachment = wp_get_attachment_image_src( $post_thumbnail_id );
$width=$attachment[1];
$height=$attachment[2];
share|improve this answer

What you need is this function: wp_get_attachment_image_src

Codex: http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src

It will return an array containing attachment's url, width and height.

share|improve this answer
1  
Yes but it expects an ID for a given image, the user doesn't have an ID to pass, he wants to find out the registered size by name, eg. thumbnail(post_thumbnail). –  t31os Dec 19 '11 at 13:12

In addition to the post above:

$attachment = wp_get_attachment_image_src( $attachment_id ); // where $attachment_id is the ID of the attachment you want to get the thumbnail from

Then simply use the next code to display the url, width and height:

echo $attachment[0]; // url
echo $attachment[1]; // width
echo $attachment[2]; // height
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.