# Shorten Very Long Upload File Names

> Trims uploaded file names longer than 60 characters, such as camera exports or copied web titles, to a tidy length before saving, while keeping the extension.

- Type: PHP
- Runs: Run everywhere
- Category: [Media](https://wpide.com/library/category/media/)
- Tags: uploads, file-names, cleanup
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/media-shorten-long-file-names/

## Code

```php
// Longest allowed name, without the extension.
$wpide_max_file_name_length = 60;

add_filter( 'sanitize_file_name', function ( $filename ) use ( $wpide_max_file_name_length ) {
    $info = pathinfo( $filename );
    $name = $info['filename'] ?? $filename;

    if ( mb_strlen( $name ) <= $wpide_max_file_name_length ) {
        return $filename;
    }

    $name = rtrim( mb_substr( $name, 0, $wpide_max_file_name_length ), '-_.' );

    return $name . ( ! empty( $info['extension'] ) ? '.' . $info['extension'] : '' );
}, 20 );
```

## Notes

Change $wpide_max_file_name_length; WordPress still adds -1, -2 when a name is already taken.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Shorten Very Long Upload File Names" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
