1

Assuming I have a custom thumbnail size, as defined by the following...

add_image_size( $name, $dim_x, $dim_y, true );

...how can I get the defined image dimensions programatically, using only $name ?

1

If you take the look at the source of the function is simply stores the arguments in $_wp_additional_image_sizes global, with key by name.

As far as I remember there are no dedicated API functions for it (get_intermediate_image_sizes() will return list of names, but not data). Just access the global and look for data you need by key.

1

Thanks to the answer given by Rarst, I was able to find this in the Wordpress documentation...

function get_image_sizes( $name ) {
    global $_wp_additional_image_sizes;
    $sizes = array();
    $get_intermediate_image_sizes = get_intermediate_image_sizes();
    // Create the full array with sizes and crop info
    foreach( $get_intermediate_image_sizes as $_size ) {
            if ( in_array( $_size, array( 'thumbnail', 'medium', 'large' ) ) ) {
                    $sizes[ $_size ]['width'] = get_option( $_size . '_size_w' );
                    $sizes[ $_size ]['height'] = get_option( $_size . '_size_h' );
                    $sizes[ $_size ]['crop'] = (bool) get_option( $_size . '_crop' );
            } elseif ( isset( $_wp_additional_image_sizes[ $_size ] ) ) {
                    $sizes[ $_size ] = array( 
                            'width' => $_wp_additional_image_sizes[ $_size ]['width'],
                            'height' => $_wp_additional_image_sizes[ $_size ]['height'],
                            'crop' =>  $_wp_additional_image_sizes[ $_size ]['crop']
                    );
            }
    }
    // Get only 1 size if found
    if ( $size ) {
            if( isset( $sizes[ $size ] ) ) {
                    return $sizes[ $size ];
            } else {
                    return false;
            }
    }
    return $sizes;
}

<?php 
    var_dump(get_image_sizes('custom-size'));
?>

array(3) {
    ["width"] => int(250)
    ["height"] => int(250)
    ["crop"] =>  bool(true)
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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