Skip to content

Commit 2ecfe2c

Browse files
Improve image sub size handling
1 parent 214812e commit 2ecfe2c

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

php/class-media.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3272,23 +3272,33 @@ public function upgrade_settings( $previous_version, $new_version ) {
32723272
/**
32733273
* Get the size dimensions based on the size slug.
32743274
*
3275+
* Uses WordPress core API to retrieve registered image subsizes, which handles both
3276+
* built-in sizes (thumbnail, medium, large, etc.) and custom registered sizes.
3277+
*
32753278
* @param string $size_slug The WordPress size slug (e.g., 'thumbnail', 'medium', 'large').
32763279
*
32773280
* @return array|null An array with width and height, or null if not found.
32783281
*/
32793282
public function get_size_from_slug( $size_slug ) {
3280-
// Get the dimensions of the WordPress size from options.
3281-
$size_width = get_option( $size_slug . '_size_w' );
3282-
$size_height = get_option( $size_slug . '_size_h' );
3283+
// Use WordPress core API to get all registered image subsizes.
3284+
$image_subsizes = wp_get_registered_image_subsizes();
32833285

3284-
// Check if we have valid dimensions. If not, return null to indicate no specific size.
3285-
if ( empty( $size_width ) || empty( $size_height ) ) {
3286+
// Check if the requested size exists in registered subsizes.
3287+
if ( ! isset( $image_subsizes[ $size_slug ] ) ) {
32863288
return null;
32873289
}
32883290

3289-
return array( (int) $size_width, (int) $size_height );
3291+
$size_data = $image_subsizes[ $size_slug ];
3292+
3293+
// Return width and height if both are present and valid.
3294+
if ( ! empty( $size_data['width'] ) && ! empty( $size_data['height'] ) ) {
3295+
return array( (int) $size_data['width'], (int) $size_data['height'] );
3296+
}
3297+
3298+
return null;
32903299
}
32913300

3301+
32923302
/**
32933303
* Extract WordPress image size from parent figure element.
32943304
*

0 commit comments

Comments
 (0)