Support

Account

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!