Skip Navigation

[Resolved] Including File Size

This support ticket is created 3 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 5 replies, has 2 voices.

Last updated by donaldM-2 3 years, 8 months ago.

Assigned support staff: Minesh.

Author
Posts
#1193475

I have this page on my site. hidden link

I made a CPT called Files and then I have categories so I can separate them. I have the files showing and it all works great. Is there a way to pull in the file size automatically and display it on the front end so the person downloading knows how big it is? preferably in MB.

#1193481

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - there is no native feature available to display the file size but you can create a custom shortcode to display the size of PDF file.

1) Create a custom shortcode [display-pdf-size], add below codes into your theme file functions.php:

add_shortcode('display-pdf-size', function($atts){
   global $post;
    $atts = shortcode_atts( array(
        'pdf_url' =>'' ,
        'meta_name' => 'size',
    ), $atts );
     
    $res = '';
    $file = get_attached_file( $atts['pdf_url'] );
     
    switch($atts['meta_name']){
          case 'size':
            $res = size_format(filesize( $file ));
            break;
    }
    return $res;
});

2) Use the custom shortcode as given under:

 [display-pdf-size meta_name="size" pdf_url="pass-here-PDF-URL"]
#1193484

So I inserted the shortcode from step two, and add the field like so:

[display-pdf-size meta_name="size" pdf_url="[types field='document' output='raw'][/types]"]

Then add the step 1 code to the functions file. Do I need to register the shortcode somewhere? Im not seeing the size on the page.

#1193486

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - I do not think you need to register this shortcode anywhere.

Can I have access details so I can check what's going wrong there?

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#1193491

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I've adjust the code as given under:

add_shortcode('display-pdf-size', function($atts){
   global $post;
    $atts = shortcode_atts( array(
        'pdf_url' =>'' ,
        'meta_name' => 'size',
    ), $atts );
      
    $res = '';
   
   $attachment_id = attachment_url_to_postid($atts['pdf_url']);
   
    $file = get_attached_file($attachment_id);
      
    switch($atts['meta_name']){
          case 'size':
            $res = size_format(filesize( $file ));
            break;
    }
    return $res;
});

In addition to that, I've removed the above code from your functions.php file and instead I've added to Toolset's Custom Code section.
=> hidden link

More info:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

#1193497

My issue is resolved now. Thank you!