has_image_size()WP 3.9.0

Check if an image size exists.

No Hooks.

Return

true|false. True if the image size exists, false if not.

Usage

has_image_size( $name );
$name(string) (required)
The image size to check.

Examples

0

#1 Deleting a image size

Suppose we don't need the size of the image and we need to remove this size, so that unnecessary thumbnails are not created. How to do this I wrote in this article, but this point is not there, because in practice we can rarely know the size name.

This example shows how to delete a registered size by knowing its name:

add_action( 'after_setup_theme', 'remove_registered_image_size' );

function remove_registered_image_size() {

	if( has_image_size('image-name') ) {
		remove_image_size('image-name');
	}
}

This function only checks for image sizes that are registered via the add_image_size() function, core image sizes, namely small, medium, medium_large and large, are not considered. Hence, checking for a core image size using has_image_size() will always return FALSE.

Changelog

Since 3.9.0 Introduced.

has_image_size() code WP 6.5.2

function has_image_size( $name ) {
	$sizes = wp_get_additional_image_sizes();
	return isset( $sizes[ $name ] );
}