The other day I got an interesting support thread for Easy Digital Downloads, asking for the list of files, and their sizes be displayed when viewing a product. Being that I only use it to sell WordPress plugins, it seemed pretty minor, but if you were dealing with larger Audio/Video files, or your consumers used their mobile devices frequently, I could see how this would be useful. So I put together a quick, single file, plugin that is now available in the EDD Library Repo on Github.
First, here’s the primary function that is run:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function edd_ck_show_file_sizes( $post_id ) { $files = edd_get_download_files( $post_id, null ); $decimals = 2; $sz = 'BKMGTP'; $header = _n( 'File Size', 'File Sizes', count( $files ), 'edd' ); echo '<h5>' . $header . '</h5>'; echo '<ul>'; foreach( $files as $file ) { $bytes = filesize( get_attached_file( $file['attachment_id'] ) ); $factor = floor((strlen($bytes) - 1) / 3); echo '<li>' . $file['name'] . ' - ' . sprintf( "%.{$decimals}f", $bytes / pow( 1024, $factor) ) . @$sz[$factor] . '</li>'; } echo '</ul>'; } add_action( 'edd_after_download_content', 'edd_ck_show_file_sizes', 10, 1 ); |
Now, a quick overview. Basically what we’re doing is looking for all the uploaded media items attached as ‘Downloads’ to this product. Once we get them, we’re iterating through them. You see this in the line
1 |
$files = edd_get_download_files( $post_id, null ); |
After that we do some setup, only wanting 2 decimal places, setting up the title of the list (following the numeric localization method), and you’ll notice that odd variable BKMGTP
. What is this? Well it’s used to make the output of the size calculation human readable. It’s for “Bytes, Kilobytes, Megabytes, Gigabytes, Terabytes, and Petabytes”. I’ve never seen a use case of someone selling Tera/Petabyte files on EDD, but who am I to judge.
After that, a little loop to run each calculation by powers of 1024, and we’re good to go!
You can go grab the full plugin-ready file here.
Image credit(CC): John Trainor
[…] EDD? Here’s a very handy snippet to show file sizes for downloads on the product pages. Sumobi also released a free extension for add-to-cart redirects on […]
Awesome! Using size_format() might be useful too: https://codex.wordpress.org/Function_Reference/size_format
This is a great suggestion, thanks!
Hi Chris, thanks for the code! I’ve added this to functions.php and then on my product page I’ve added;
$post = get_post();
edd_ck_show_file_sizes( $post->ID );
which is almost working. Strangely, if I upload a file with the fes submission form it shows the size as 0.00, but if I upload a file from the WordPress dashboard it seems to show the size correctly. Any idea why this might be? Screenshot; https://i.imgur.com/E67VUal.png
Thanks again.
Tom
Hi, I am facing the same problem. As I found out, this is all from the fact that the plugin FES does not add parameter ‘attachment_id’ to the table ‘wp_postmeta’ in line ‘edd_download_files’.
I found the file plugins/edd-fes/classes/fields/multiple_pricing.php
and in the lines 513 and 523 added: ‘attachment_id’ => fes_get_attachment_id_from_url( $url, $user_id )
And it all worked)
But I don’t know if all this is right and there will be no problems in the future …) Thanks
hi, how to show
file name with extension ?
ex:
name : test size : 39 kb type: zip
tnx