I was wondering if I can show my featured image Dimensions (W x H in px) and the filesize in mb. In my Wordpress post with a shortcode.

thank you in advance :)

1 Answer 1


Yes, that is possible. Building upon the answer Nicolai Grossherr gave to Get the size (size in Kb or MB) of an featured image? you could do something like this,

// e.g. in functions.php
add_shortcode( 'my_image_metadata', 'my_image_metadata_callback' );
function my_image_metadata_callback($atts) {
  $img_meta = wp_get_attachment_metadata( get_post_thumbnail_id() );
  if ( ! $img_meta ) {

  $upload_dir = wp_upload_dir();
  $bytes = filesize(
    $upload_dir[ 'basedir' ] . DIRECTORY_SEPARATOR . $img_meta[ 'file' ]
  if ( ! $bytes ) {

  $file_size = size_format( $bytes );

  return sprintf(
      <span class="dimensions">%d x %d px</span>
      <span class="filesize">%s</span>
    $file_size ?: ''

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.