Created
October 21, 2020 20:11
-
-
Save kodie/19d027d2c0e2297d1e09b7b71505e774 to your computer and use it in GitHub Desktop.
A function to get with, height, and if crop is available for all image sizes in WordPress regardless if they are built-in sizes or custom set ones
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Get information about available image sizes | |
function get_image_sizes($size = null) { | |
$sizes = array(); | |
$size_data = wp_get_additional_image_sizes(); | |
$size_names = get_intermediate_image_sizes(); | |
$default_sizes = array_diff($size_names, array_keys($size_data)); | |
if ($size) $size_names = array($size); | |
foreach($size_names as $size_name) { | |
if (in_array($size_name, $default_sizes)) { | |
$sizes[$size_name] = array( | |
'width' => get_option($size_name . '_size_w'), | |
'height' => get_option($size_name . '_size_h'), | |
'crop' => (bool) get_option($size_name . '_crop') | |
); | |
} elseif (isset($size_data[$size_name])) { | |
$sizes[$size_name] = array( | |
'width' => $size_data[$size_name]['width'], | |
'height' => $size_data[$size_name]['height'], | |
'crop' => $size_data[$size_name]['crop'] | |
); | |
} | |
} | |
if ($size) { | |
if (isset($sizes[$size])) { | |
return $sizes[$size]; | |
} else { | |
return false; | |
} | |
} | |
return $sizes; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment