Skip to content

Instantly share code, notes, and snippets.

@everaldomatias
Created May 17, 2020 23:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save everaldomatias/a43dfd2adcb7e255e02850df150a4110 to your computer and use it in GitHub Desktop.
Save everaldomatias/a43dfd2adcb7e255e02850df150a4110 to your computer and use it in GitHub Desktop.
WordPress function for get file size in attachment
if( !function_exists( 'the_file_size' ) ) {
/**
*
* WordPress function for get file size in attachment
*
* @author Everaldo Matias
* @link https://everaldo.dev
*
* @version 1.0
* @license http://www.opensource.org/licenses/mit-license.html MIT License
*
* @see https://developer.wordpress.org/reference/functions/get_attached_file/
* @see https://www.php.net/manual/pt_BR/function.filesize.php
*
* @param string,integer $id of the attachment
*
*/
function the_file_size( $id ) {
// Get the file path
$file_path = get_attached_file( $id );
// Get the file size
$file_size = filesize( $file_path );
// Return file size in megabytes
$file_size = round( $file_size / 1024 / 1024, 1 );
echo $file_size . ' MB';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment