The ‘pre_wp_filesize’ filter allows you to modify the result of the wp_filesize function before the PHP function is executed.
You can use this filter to bypass the filesize call by returning an integer value.
Usage
To use this filter, add the following code to your theme’s functions.php file or a custom plugin:
add_filter( 'pre_wp_filesize', 'your_function_name', 10, 2 );
function your_custom_function( $size, $path ) {
// Your custom code here
}
Parameters
- $size (null|int): The unfiltered value. Returning an int from the callback bypasses the
filesizecall. - $path (string): Path to the file.
Examples
Set a custom file size limit
Scenario: Set a custom file size limit for uploaded images to 1MB.
add_filter( 'pre_wp_filesize', 'custom_filesize_limit', 10, 2 );
function custom_filesize_limit( $size, $path ) {
// Define the maximum allowed file size in bytes (1MB)
$max_size = 1024 * 1024;
// Check if the file is an image
if ( strpos( mime_content_type( $path ), 'image' ) === 0 ) {
$filesize = filesize( $path );
// If the image size is larger than the allowed size, set the size to the maximum allowed size
if ( $filesize > $max_size ) {
return $max_size;
}
}
// Return the unfiltered value if the file is not an image or is within the allowed size
return $size;
}
What does the code do?
This code sets a custom file size limit for uploaded images to 1MB. If the image size is larger than the allowed size, it sets the size to the maximum allowed size. If the file is not an image or is within the allowed size, it returns the unfiltered value.
Bypass the filesize call for specific file types
Scenario: Bypass the filesize call for .txt files.
add_filter( 'pre_wp_filesize', 'bypass_filesize_for_txt_files', 10, 2 );
function bypass_filesize_for_txt_files( $size, $path ) {
// Check if the file is a .txt file
if ( pathinfo( $path, PATHINFO_EXTENSION ) === 'txt' ) {
// Bypass the filesize call by returning an arbitrary value
return 12345;
}
// Return the unfiltered value for other file types
return $size;
}
What does the code do?
This code checks if the file is a .txt file. If it is, it bypasses the filesize call by returning an arbitrary value. If the file is not a .txt file, it returns the unfiltered value.
Set a default file size for missing files
Scenario: Set a default file size for files that do not exist on the server.
add_filter( 'pre_wp_filesize', 'default_filesize_for_missing_files', 10, 2 );
function default_filesize_for_missing_files( $size, $path ) {
// Check if the file does not exist
if ( !file_exists( $path ) ) {
// Set a default file size of 0 for missing files
return 0;
}
// Return the unfiltered value for existing files
return $size;
}
Multiply file sizes by a factor
Scenario: Multiply the file sizes of all .pdf files by a factor of 2.
add_filter( 'pre_wp_filesize', 'multiply_pdf_filesize', 10, 2 );
function multiply_pdf_filesize( $size, $path ) {
// Check if the file is a .pdf file
if ( pathinfo( $path, PATHINFO_EXTENSION ) === 'pdf' ) {
// Get the actual file size
$actual_size = filesize( $path );
// Multiply the file size by 2
return $actual_size * 2;
}
// Return the unfiltered value for other file types
return $size;
}
What does the code do?
This code checks if the file is a .pdf file. If it is, it multiplies the file size by a factor of 2. If the file is not a .pdf file, it returns the unfiltered value.
Limit file size for audio files
Scenario: Limit the file size of audio files to 5MB.
add_filter( 'pre_wp_filesize', 'limit_audio_filesize', 10, 2 );
function limit_audio_filesize( $size, $path ) {
// Define the maximum allowed file size in bytes (5MB)
$max_size = 5 * 1024 * 1024;
// Check if the file is an audio file
if ( strpos( mime_content_type( $path ), 'audio' ) === 0 ) {
$filesize = filesize( $path );
// If the audio file size is larger than the allowed size, set the size to the maximum allowed size
if ( $filesize > $max_size ) {
return $max_size;
}
}
// Return the unfiltered value if the file is not an audio file or is within the allowed size
return $size;
}
What does the code do?
This code limits the file size of audio files to 5MB. If the audio file size is larger than the allowed size, it sets the size to the maximum allowed size. If the file is not an audio file or is within the allowed size, it returns the unfiltered value.