Created
May 17, 2020 23:52
-
-
Save everaldomatias/a43dfd2adcb7e255e02850df150a4110 to your computer and use it in GitHub Desktop.
WordPress function for get file size in attachment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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