Get the size of a file with wp_filesize

WordPress 6.0 introduced the wp_filesize method.

It is a wrapper of the core PHP function filesize() and returns the size as an integer.

It also has filters for filtering the size before and after executing the filesize() method.

$size = filesize(‘/path/image.png’); // before 6.0

$size = wp_filesize(‘/path/image.png’); // after 6.0

If you encounter the following error, Uncaught Error: Call to undefined function “wp_filesize”, then you have to update WordPress core to 6.0 or higher.

VS Code extension, Intelephense might throw a warning, Undefined function ‘wp_filesize’.intelephense(1010) 

Even with the latest version of Intelephense, I still get this warning. I will update this post once I find a fix for this.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *