file_is_displayable_image()
Checks if the file is an image (if it is suitable for display on the page). Conditional tag.
The function is often used as a conditional tag when working with images.
When you need to determine if a file from the media library is an image, use wp_attachment_is_image().
Often, you need to include the file image.php, where the described function is defined:
require_once ABSPATH . 'wp-admin/includes/image.php';
By default, this file is included after the hook wp_loaded, when rendering content in the admin area.
Works based on the php function getimagesize().
1 time — 0.000001 sec (speed of light) | 50000 times — 0.42 sec (very fast) | PHP 7.2.16, WP 5.1.1
Hooks from the function
Returns
true|false. True - suitable for display, false - not suitable.
The function will return true for the following extensions: .gif, .jpeg, .png, .bmp, .ico.
Usage
file_is_displayable_image( $path );
- $path(string) (required)
- Path to the file being checked.
Examples
#1 Check if the files are an image
require_once ABSPATH . 'wp-admin/includes/image.php';
$path = 'F:\server\sites\wp-test.ru\wp-admin\images\align-center.png';
$is_image = file_is_displayable_image( $path ); //> true
$path = 'F:\server\sites\wp-test.ru\wp-admin\css\common.css';
$is_image = file_is_displayable_image( $path ); //> false
if ( $is_image ) {
echo 'The file is an image';
}
else {
echo 'The file is not an image';
}
Changelog
| Since 2.5.0 | Introduced. |