Announcing a new addon/extension for USP Pro: Custom File Names! This extension automatically names uploaded files with a unique custom prefix, which may be customized directly in the source code. No configuration required, simply install, activate, and enjoy custom/unique file names for all submitted images and files.
How it works
By default, USP Pro uploads files and names them using WordPress default protocols. This works fine for most use-case scenarios, but in some cases you may need to ensure that all files are named in a specific way. That’s where the Custom File Name extension comes into play: it enables you to customize the file name with a unique/custom prefix.
Ways to customize the file name
By default with USP Pro, there are two ways to configure names for submitted files:
- Default file names: keeps the file name as-is, using WP core functionality
- Prefix names with a date-based + random string: this can be enabled via the “USP Uploads” setting, “Unique File Names”
So if one of those options works for you, then you’re good to go without the Custom File Names extension. But if you need more control over how the files are named, the extension will enable you to customize the names however is required.
Technical details
When the “Unique File Names” option is enabled in the USP Uploads settings, the following code is used to prefix all submitted file names:
$usp_files['name'][$i] = date('Y-m-d') . '_' . uniqid() . '_' . $usp_files['name'][$i];
As you can see, this prepends the file name with the current date, plus a unique string. So if you need more control over the file names, the Custom File Names extension makes it possible. By default, the extension simply prefixes the file name with a unique ID:
$custom = uniqid();
But this can be changed to whatever is required, directly in the plugin source code. So the extension gives you much control, and enables you to set file names using any criteria that is available to WordPress. If you decide to purchase the extension, we will be glad to help you modify the code to suit your needs. Contact us for more information.
Customizing
The Custom File Names plugin provides a filter hook that can be used to customize the string that is prepended to the file name. By default, the custom string is set to a random number using PHP’s uniqid()
function. So if you want to change that to, say, the current date, you would add the following code to your theme’s (or child theme’s) functions.php
file:
function usp_custom_file_names_custom($custom) {
return date();
}
add_filter('usp_custom_file_names_custom', 'usp_custom_file_names_custom');
This technique uses the usp_custom_file_names_custom
filter hook to set the value of the custom string that is prepended to the file name. Of course, you can replace date()
with whatever string/function you want to use.