catchresponsive_sanitize_image

Image sanitization callback example.

Description

(string) catchresponsive_sanitize_image( (string) $image, (WP_Customize_Setting) $setting ); 

Checks the image's file extension and mime type against a whitelist. If they're allowed, send back the filename, otherwise, return the setting default.

- Sanitization: image file extension - Control: text, WP_Customize_Image_Control

Returns (string)

The image filename if the extension is allowed; otherwise, the setting default.

Parameters (2)

0. $image (string)
Image filename.
1. $setting (WP_Customize_Setting)
Setting instance.

Usage

  1. if ( !function_exists( 'catchresponsive_sanitize_image' ) ) { 
  2. require_once '/inc/customizer-includes/catchresponsive-customizer-sanitize-functions.php'; 
  3.  
  4. // Image filename. 
  5. $image = ''; 
  6.  
  7. // Setting instance. 
  8. $setting = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = catchresponsive_sanitize_image($image, $setting); 
  12.  

Defined (1)

The function is defined in the following location(s).

/inc/customizer-includes/catchresponsive-customizer-sanitize-functions.php  
  1. function catchresponsive_sanitize_image( $image, $setting ) { 
  2. /** 
  3. * Array of valid image file types. 
  4. * The array includes image mime types that are included in wp_get_mime_types() 
  5. */ 
  6. $mimes = array( 
  7. 'jpg|jpeg|jpe' => 'image/jpeg',  
  8. 'gif' => 'image/gif',  
  9. 'png' => 'image/png',  
  10. 'bmp' => 'image/bmp',  
  11. 'tif|tiff' => 'image/tiff',  
  12. 'ico' => 'image/x-icon' 
  13. ); 
  14. // Return an array with file extension and mime_type. 
  15. $file = wp_check_filetype( $image, $mimes ); 
  16. // If $image has a valid mime_type, return it; otherwise, return the default. 
  17. return ( $file['ext'] ? $image : $setting->default );