By default, blank spaces are replaced with underscores _ in the value attribute of custom checkbox, radio, and select fields. This post explains how to reverse this behavior and replace the underscores with blank spaces.

Solution

The following function hooks into the select-field value and replaces any underscores with blank spaces:

// usp pro replace underscores with spaces
function usp_pro_modify_select_value($value) {
	
	return strtolower(trim(str_replace('_', ' ', $value)));
}
add_filter('usp_custom_fields_select_value', 'usp_pro_modify_select_value');

Add that to your theme’s functions.php file and done.

To filter the values of other fields, simply replace the usp_custom_fields_select_value hook with any of the following:

usp_custom_fields_checkbox_value
usp_custom_fields_radio_value
usp_custom_fields_select_value

So to filter all three types of fields, you would do something like this:

// usp pro replace underscores with spaces
function usp_pro_modify_field_value($value) {
	
	return strtolower(trim(str_replace('_', ' ', $value)));
	
}
add_filter('usp_custom_fields_checkbox_value', 'usp_pro_modify_field_value');
add_filter('usp_custom_fields_radio_value',    'usp_pro_modify_field_value');
add_filter('usp_custom_fields_select_value',   'usp_pro_modify_field_value');

Here the name of the function is changed for clarity. This example will replace underscores with spaces for all types of fields: checkboxes, radio, and select.

Note: other custom fields, such as text inputs and textareas, do not auto-replace spaces with underscores, so only the three fields mentioned above may require it.
Update!

To capitalize the first letter of each word, in addition to replacing underscores, change the return line to this:

return ucwords(trim(str_replace('_', ' ', $value)));

Basically it’s the strtolower() function that converts the string to lowercase, so by replacing it with ucwords(), we ensure that each word is capitalized. Likewise, further customization of the field string may be achieved by modifying the return line with other PHP functions, etc.

Related