has_image_size()
Checks whether the specified image size exists.
When calling the function, specify the name of the size that was used when registering it with the add_image_size() function. Conditional tag — returns TRUE or FALSE.
Works based on the global variable $_wp_additional_image_sizes. New image sizes are written into this variable.
No Hooks.
Returns
true|false.
true, if the specified size exists.false, if the specified size is not found.
Usage
has_image_size( $name );
- $name(string) (required)
- The name of the registered image size whose existence needs to be checked.
Examples
#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() has image size code WP 6.9.1
function has_image_size( $name ) {
$sizes = wp_get_additional_image_sizes();
return isset( $sizes[ $name ] );
}