Plugin Planet

Premium WordPress Plugins

By default, USP Pro appends a numerical suffix to the names of custom fields for attached files. This tutorial explains how to disable the suffix and/or customize however is desired.

How it works

By default, the URL of each uploaded file is attached to the submitted post as a custom field. For example, if your form enables users to submit three files, each submitted post will include the following custom fields:

usp-file-1
usp-file-2
usp-file-3

Notice the auto-added suffixes for each file, -1, -2, -3. By default, USP Pro adds the suffix to any files uploaded via [usp_files] (primary field) or field#input_file (custom field). The suffixes are merely for naming purposes and are not required. This tutorial shows you how to remove or customize them.

Step 1

To disable the auto-suffix, add the following code to your theme’s functions.php file (or add via simple plugin):

// usp pro remove file suffix
function usp_pro_remove_file_suffix($field, $key) {
    
    return str_replace('-'. $key, '', $field);
    
}
add_filter('usp_file_name_primary_multiple', 'usp_pro_remove_file_suffix', 10, 2);

Here we are connecting our function via the plugin’s usp_file_name_primary_multiple hook. So it will apply to any files uploaded via the [usp_files] when multiple is set to true, for example:

[usp_files multiple="true" required="true"]

So with the above custom function in place, submitting three files via that files field will result in the following custom fields attached to the submitted post:

usp-file
usp-file
usp-file

To customize the suffix (instead of removing it), you can edit the value of str_replace() to suit your needs.

Step 2

If you’re removing the suffix from files uploaded via the [usp_files] field, nothing else needs to be done. You just add the previous code snippet and call it a day.

If, on the other hand, you are using a custom field for your files field, you need to make one small change to the previous code example. Specifically, change the USP hook that is used, from this:

add_filter('usp_file_name_primary_multiple', 'usp_pro_remove_file_suffix', 10, 2);

..to this:

add_filter('usp_file_name_custom_field', 'usp_pro_remove_file_suffix', 10, 2);

This tells the plugin to remove the suffix from any files uploaded via a custom files field, for example something like this:

field#input_file|method#select|multiple#true|data-required#true|files_min#1|files_max#3

You can learn more about custom fields and multiple files fields via the “Resources” section at the end of this tutorial.

Remove the suffix from other types of files fields

So far, we have seen how to remove the suffix from files uploaded via primary fields and custom fields. While this covers most cases, there are other types of files fields that may be used. So to customize the auto-added suffix for other types of files fields, you can change the hook (as described in Step 2) to target whichever field you are using. Here is a list of the hooks that can be used to customize or remove the default added suffix:

usp_file_name_primary_multiple  =  e.g., [usp_files multiple="true"]
usp_file_name_custom_prefix     =  e.g., field#input_file|multiple#true|name#myprefix-myfieldname
usp_file_name_custom_field      =  e.g., field#input_file|multiple#true
usp_file_name_custom_custom     =  e.g., field#input_file|name#my-custom-custom-field

If you are unsure as to which type of field your form is using, you can hook into all of them to remove or customize all auto-added suffixes:

// usp pro remove file suffix
function usp_pro_remove_file_suffix($field, $key) {
    
    return str_replace('-'. $key, '', $field);
    
}
add_filter('usp_file_name_primary_multiple', 'usp_pro_remove_file_suffix', 10, 2);
add_filter('usp_file_name_custom_prefix', 'usp_pro_remove_file_suffix', 10, 2);
add_filter('usp_file_name_custom_field', 'usp_pro_remove_file_suffix', 10, 2);
add_filter('usp_file_name_custom_custom', 'usp_pro_remove_file_suffix', 10, 2);

As before, you can customize the suffix (instead of removing it) by tweaking the str_replace() function.

Customizing other types of files fields

In addition to the four types of files fields that auto-add a suffix, there are two types of files fields that do NOT add any suffix:

usp_file_name_primary_single  =  e.g., [usp_files multiple="false"]
usp_file_name_primary_key     =  e.g., [usp_files multiple="false" key="1"]

Using the same function as provided above, it also is possible to customize the custom-field name for these two types of files fields. Simply change the hook used by the previously provided function. For example, to customize the name of files uploaded via the [usp_files multiple="false"] field:

// usp pro remove file suffix
function usp_pro_remove_file_suffix($field, $key) {
    
    return str_replace('-'. $key, '', $field);
    
}
add_filter('usp_file_name_primary_single', 'usp_pro_remove_file_suffix', 10, 2);

To instead customize the custom-field name for files uploaded via the [usp_files multiple="false" key="1"] field, simply change the hook like so:

add_filter('usp_file_name_primary_key', 'usp_pro_remove_file_suffix', 10, 2);

It’s also possible to remove the auto-added prefix, as explained next..

Removing or customize the auto-added prefix

Notice in the example above that some types of fields auto-add a prefix of usp- to the custom-field names of uploaded files, for example:

usp-file-1
usp-file-2
usp-file-3

To remove or customize the default usp- prefix, you can use custom custom fields.

Resources