36

What function can I use in a plugin to get the dimensions of every image size (in an array preferably) that is defined in a child theme?

Just for clarification I am not asking how to create a new image size.

1
  • Searching for something like this, but with an interface, came across the plugin Additional image sizes (zui). It has 500 days without updates but passed the first batch of tests.
    – brasofilo
    Jul 6, 2012 at 1:51

7 Answers 7

59

Found it here. The answer is:

global $_wp_additional_image_sizes; 
print '<pre>'; 
print_r( $_wp_additional_image_sizes ); 
print '</pre>';
4
  • Adding it to the top of the functions.php file won't work, but stick it in any of your functions in that file and it should work.
    – kevnk
    Aug 15, 2017 at 20:40
  • 4
    This lists only the additional sizes, not sizes like "large"
    – Fanky
    Jan 19, 2021 at 9:37
  • Wrong answer, should not be accepted as such.
    – user75665
    Nov 8, 2022 at 5:02
  • As of 2023 this is the correct answer: wordpress.stackexchange.com/questions/33532/… Aug 24, 2023 at 8:38
35

WordPress core doesn't have a native method for getting intermediate image sizes (i.e. width and height), but the following helper function will get all registered image sizes along with their dimensions:

/**
 * Get all the registered image sizes along with their dimensions
 *
 * @global array $_wp_additional_image_sizes
 *
 * @link http://core.trac.wordpress.org/ticket/18947 Reference ticket
 *
 * @return array $image_sizes The image sizes
 */
function _get_all_image_sizes() {
    global $_wp_additional_image_sizes;

    $default_image_sizes = get_intermediate_image_sizes();

    foreach ( $default_image_sizes as $size ) {
        $image_sizes[ $size ][ 'width' ] = intval( get_option( "{$size}_size_w" ) );
        $image_sizes[ $size ][ 'height' ] = intval( get_option( "{$size}_size_h" ) );
        $image_sizes[ $size ][ 'crop' ] = get_option( "{$size}_crop" ) ? get_option( "{$size}_crop" ) : false;
    }

    if ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) ) {
        $image_sizes = array_merge( $image_sizes, $_wp_additional_image_sizes );
    }

    return $image_sizes;
}

Which will output results similar to:

Array
(
    [thumbnail] => Array
        (
            [width] => 150
            [height] => 150
            [crop] => 1
        )

    [medium] => Array
        (
            [width] => 300
            [height] => 300
            [crop] => 
        )

    [medium_large] => Array
        (
            [width] => 768
            [height] => 0
            [crop] => 
        )

    [large] => Array
        (
            [width] => 1024
            [height] => 1024
            [crop] => 
        )

)
7
  • 5
    +1 for a more complete answer. The others return additional sizes but not default ones. Jan 8, 2017 at 22:30
  • 2
    Even better if you use $default_image_sizes = get_intermediate_image_sizes(); as there's a medium_large size since WP 4.4.
    – Nate Beaty
    Sep 21, 2017 at 22:23
  • Good suggestion; I've updated the answer with your recommendation.
    – rjb
    Sep 23, 2017 at 19:24
  • 8
    Since 5.3, there is wp_get_registered_image_subsizes()
    – Marc
    Sep 3, 2020 at 16:40
  • 2
    This answer works like a charm, but is superseded by the last comment, for Wordpress 5.3 and up: developer.wordpress.org/reference/functions/…
    – nabrown
    Jun 8, 2021 at 16:11
18

If you only need the names of all image sizes, you can use get_intermediate_image_sizes:

<pre>
<?php print_r(get_intermediate_image_sizes()); ?>
</pre>
15

Since WP 5.3 it is enough to use this function:

wp_get_registered_image_subsizes();

Documentation: https://developer.wordpress.org/reference/functions/wp_get_registered_image_subsizes/

2
  • The shortest and the only correct answer here, but why so low in rankings.
    – Picard
    Jun 28, 2022 at 9:08
  • The only proper answer. Probably low ranking because it is only around since WP 5.3, which at the time of the OP question was not around. Yet, this should be the only accepted answer, as everything else is the usual "throw in some code SO trash" - upvoted to get it up.
    – user75665
    Nov 8, 2022 at 5:02
5

If the other answer did not work, use this code, so it will run after WordPress is initialized.

add_action('init', 'get_all_image_sizes');

function get_all_image_sizes(){
    global $_wp_additional_image_sizes; 
    print '<pre>'; 
    print_r( $_wp_additional_image_sizes ); 
    print '</pre>';
}
1
  • I have tested several methods and this one finally worked. (WP + WooCommerce + custom theme) Feb 19, 2018 at 11:19
4

Since WP 4.7 you can use wp_get_additional_image_sizes() to get additional image sizes registered (excluding the default image sizes added by WP).

Since WP 5.3 you can use wp_get_registered_image_subsizes() to get all the image sizes registered.

2

Use the following functions:

get_intermediate_image_sizes(); // get all the image size names.
wp_get_additional_image_sizes(); // get all the additional image size data.
wp_get_registered_image_subsizes(); // get all the image size data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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