Advertisement
palsushobhan

wcfm-custom-field-image-type-upload

Jun 24th, 2025 (edited)
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.07 KB | None | 0 0
  1. function is_attachment_image($attachment_id) {
  2.     if (!is_numeric($attachment_id) || $attachment_id <= 0) {
  3.         return false;
  4.     }
  5.  
  6.     $mime_type = get_post_mime_type($attachment_id);
  7.  
  8.     if ($mime_type && strpos($mime_type, 'image/') === 0) {
  9.         return true;
  10.     }
  11.  
  12.     return false;
  13. }
  14.  
  15. add_filter('wcfm_custom_field_value', function($val, $name, $product_id, $field_type, $field) {
  16.     if ($field_type == 'upload') {
  17.         $attachment_id = get_post_meta($product_id, $name, true);
  18.  
  19.         // Ensure we have a valid attachment ID and it's an image
  20.         if (is_numeric($attachment_id) && $attachment_id > 0 && is_attachment_image($attachment_id)) {
  21.             $image_url = wp_get_attachment_url($attachment_id);
  22.             if ($image_url) {
  23.                 $alt_text = isset($field['label']) ? esc_attr(wcfm_removeslashes(__($field['label'], 'WCfM'))) : '';
  24.                 return '<img src="' . esc_url($image_url) . '" alt="' . $alt_text . '" style="max-width:250px; height:auto;"/>';
  25.             }
  26.         }
  27.     }
  28.     return $val;
  29. }, 10, 5);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement