Advertisement
palsushobhan

wcfm-custom-field-image-type-upload

Jun 24th, 2025
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.03 KB | None | 0 0
  1. function isImage($url) {
  2.     $headers = get_headers($url, 1);
  3.     if (isset($headers['Content-Type'])) {
  4.         $contentType = $headers['Content-Type'];
  5.         if (strpos($contentType, 'image/') === 0) {
  6.             return true;
  7.         }
  8.     }
  9.     $pathInfo = pathinfo($url);
  10.     if (isset($pathInfo['extension'])) {
  11.         $extension = strtolower($pathInfo['extension']);
  12.         $imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'ico'];
  13.         if (in_array($extension, $imageExtensions)) {
  14.             return true;
  15.         }
  16.     }
  17.     return false;
  18. }
  19.  
  20. add_filter('wcfm_custom_field_value', function($val, $name, $product_id, $field_type, $field) {
  21.     if($field_type == 'upload') {
  22.         $field_value = wcfm_get_attachment_url(get_post_meta($product_id, $name, true));
  23.         if(!$field_value || !isImage($field_value)) {
  24.             return $val;
  25.         }
  26.         return '<img src="'.esc_url($field_value).'" alt="'. wcfm_removeslashes(__($field['label'], 'WCfM')) .'"/>';
  27.     }
  28.     return $val;
  29. }, 10, 5);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement