Home › Forums › ACF PRO › Get Filesize through GridBuilder Custom Block › Reply To: Get Filesize through GridBuilder Custom Block
Thanks for the reply, John!
You are correct, calling the post was the missing piece.
Here’s the code that worked for me if anyone else is in need:
$blocks['file_size_block'] = [
'name' => __( 'File Size Block', 'text-domain' ),
'render_callback' => function() {
// Object can be a post, term or user.
$object = wpgb_get_object();
// If this is not a post (you may change this condition for user or term).
if ( ! isset( $object->post_type ) ) {
return;
}
$attachment_id = get_post_meta( $object->ID, 'report_attachment', true );
$file_path = get_attached_file( $attachment_id );
if ( empty( $attachment_id ) ) {
return;
}
// Get the file size
$file_size = filesize( $file_path );
// Return file size in megabytes
$file_size = round( $file_size / 1024, 0 );
echo $file_size . ' KB';
Cheers!
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.