Skip to content

Instantly share code, notes, and snippets.

@eduardozulian
Created September 6, 2013 18:23
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save eduardozulian/6467854 to your computer and use it in GitHub Desktop.
Save eduardozulian/6467854 to your computer and use it in GitHub Desktop.
Get all the registered image sizes along with their dimensions
<?php
/**
* 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 = array( 'thumbnail', 'medium', 'large' );
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;
}
?>
@mikedamoiseau
Copy link

Hey, thanks for the gist.
I think you forgot to initialize your array $image_sizes before the loop. Also, I would rewrite the whole block like this:

$image_sizes = array();
$default_image_sizes = array( 'thumbnail', 'medium', 'large' );
foreach ( $default_image_sizes as $size ) {
    $image_sizes[$size] = array(
        'width'  => intval( get_option( "{$size}_size_w" ) ),
        'height' => intval( get_option( "{$size}_size_h" ) ),
        '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 );
}

Untested code, but it should work ;)

@gitgetgotgotten
Copy link

gitgetgotgotten commented Apr 25, 2022

@mikedamoiseau

Put global $_wp_additional_image_sizes at the beginning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment