getting uploaded SVG dimensions on front-end

I agree with Tom J Nowell on the use of SVG, but if the upload is an actual image, you can tap into the attachment attributes using, as you suggested, wp_get_attachment_image_src.

Those dimensions are actually already recorded and stored when using the WP media uploader, it’s likely that the plugin you’re using makes use of the WP media uploader.

There are four attributes stored (0=URL,1=Width,2=Height,3=is_intermediate) if the attachment is an image, false if it’s not an image.

So you can call and echo those attributes like in this example:

$img_atts = wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'medium');
$img_src = $img_atts[0];

I use this to echo an image’s dimensions (atts 1 & 2) to create per-post OG tags for og:image:width and og:image:height (along with other OG tags).

You can find more info here:
https://developer.wordpress.org/reference/functions/wp_get_attachment_image_src/

Leave a Comment