• selim13

    (@selim13)


    I often use safe svg in conjunction with ACF image fields and one thing that often bugged me was the lack of image dimensions (width and height) in returned array for the field and there is no much info in the google for it.

    After messing around with the code I’ve found a pretty simple solution of using acf’s filter with svg_dimensions from safe_svg class.

    
    add_filter('acf/format_value/type=image', function ($value, $post_id, $field) {
        if (is_array($value) && $value['mime_type'] === 'image/svg+xml') {
            $dimensions = svg_dimensions(get_attached_file($value['id']));
    
            if ($dimensions) {
                $value['width'] = $dimensions['width'];
                $value['height'] = $dimensions['height'];
            }
        }
    
        return $value;
    }, 20, 3);
    

    The only problem is a need to copy/pasting of svg_dimensions into theme since it is protected.
    Since it doesn’t rely on anything inside the class, maybe you can make it public static?

    Thanks.

  • The topic ‘Add dimensions to ACF images fields (or make svg_dimensions public static)’ is closed to new replies.