The file_is_displayable_image WordPress PHP filter determines if an image is displayable in the browser.
Usage
add_filter('file_is_displayable_image', 'your_custom_function', 10, 2); function your_custom_function($result, $path) { // your custom code here return $result; }
Parameters
$result
(bool) – Whether the image can be displayed. Default is true.$path
(string) – Path to the image.
More information
See WordPress Developer Resources: file_is_displayable_image
Examples
Restrict displayable image types
Allow only JPEG and PNG images to be displayed in the browser.
add_filter('file_is_displayable_image', 'restrict_displayable_image_types', 10, 2); function restrict_displayable_image_types($result, $path) { // Check file extension $ext = strtolower(pathinfo($path, PATHINFO_EXTENSION)); // Allow only JPEG and PNG images if ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'png') { return true; } return false; }
Force display of SVG images
Force SVG images to be displayable in the browser.
add_filter('file_is_displayable_image', 'force_svg_display', 10, 2); function force_svg_display($result, $path) { // Check file extension $ext = strtolower(pathinfo($path, PATHINFO_EXTENSION)); // If the file is an SVG, allow display if ($ext == 'svg') { return true; } return $result; }
Block all images
Prevent all images from being displayed in the browser.
add_filter('file_is_displayable_image', 'block_all_images', 10, 2); function block_all_images($result, $path) { // Block all images return false; }
Allow only images in a specific folder
Allow images to be displayed only if they are in the “approved” folder.
add_filter('file_is_displayable_image', 'allow_specific_folder_images', 10, 2); function allow_specific_folder_images($result, $path) { // Check if image is in the "approved" folder if (strpos($path, '/approved/') !== false) { return true; } return false; }
Display only square images
Allow only square images to be displayed in the browser.
add_filter('file_is_displayable_image', 'display_only_square_images', 10, 2); function display_only_square_images($result, $path) { // Get image dimensions list($width, $height) = getimagesize($path); // Check if image is square if ($width == $height) { return true; } return false; }