Using WordPress ‘image_make_intermediate_size’ PHP filter

The image_make_intermediate_size WordPress PHP Filter allows you to modify the name of the saved image file when WordPress generates intermediate sizes.

Usage

add_filter('image_make_intermediate_size', 'your_custom_function', 10, 1);
function your_custom_function($filename) {
    // your custom code here
    return $filename;
}

Parameters

  • $filename (string) – The name of the saved image file.

More information

See WordPress Developer Resources: image_make_intermediate_size

Examples

Add custom prefix to image filename

Add a custom prefix to the image filename when it’s saved.

add_filter('image_make_intermediate_size', 'add_custom_prefix', 10, 1);
function add_custom_prefix($filename) {
    $custom_prefix = 'custom_';
    $new_filename = $custom_prefix . $filename;
    return $new_filename;
}

Add current date to image filename

Add the current date to the image filename when it’s saved.

add_filter('image_make_intermediate_size', 'add_current_date', 10, 1);
function add_current_date($filename) {
    $date = date('Ymd');
    $new_filename = $date . '_' . $filename;
    return $new_filename;
}

Convert image filename to lowercase

Convert the image filename to lowercase when it’s saved.

add_filter('image_make_intermediate_size', 'convert_filename_to_lowercase', 10, 1);
function convert_filename_to_lowercase($filename) {
    $new_filename = strtolower($filename);
    return $new_filename;
}

Replace spaces with underscores in image filename

Replace spaces with underscores in the image filename when it’s saved.

add_filter('image_make_intermediate_size', 'replace_spaces_with_underscores', 10, 1);
function replace_spaces_with_underscores($filename) {
    $new_filename = str_replace(' ', '_', $filename);
    return $new_filename;
}

Remove special characters from image filename

Remove special characters from the image filename when it’s saved.

add_filter('image_make_intermediate_size', 'remove_special_characters', 10, 1);
function remove_special_characters($filename) {
    $new_filename = preg_replace('/[^A-Za-z0-9\_\-\.]/', '', $filename);
    return $new_filename;
}