Plugin Planet

Premium WordPress Plugins

USP Pro provides built-in form validation and error notifications. But sometimes you just want a little more.. like custom form validation for certain fields. This post explains how to go about grabbing the form POST data, and validating it however is required. Note that this is an advanced technique aimed at developers.

Note: This technique requires USP Pro version 2.9 or better.

Step 1: Validation

Add the following code to your theme functions.php or add via custom plugin:

function usp_set_custom_form_validation($array) {
	// grab $_POST, validate as needed
	// append any error(s) to $array
	// prefix each error with "usppro_"
	$array['errors'][] = 'usppro_example'; 
	$array['errors'][] = 'usppro_something'; 
	$array['errors'][] = 'usppro_whatever'; 
	return $array;
add_filter('usp_get_field_val', 'usp_set_custom_form_validation');

This snippet is where you can add your own custom form validation. To do so, grab the $_POST variable and validate as needed. Then you can set the error that is displayed by adding your own strings to the $array['errors'][] array.

Step 2: Error Display

With the previous code in place, we’re all set to check the error array and display any error messages to the user. To do this, we add the following code to your theme functions.php or add via custom plugin:

function usp_get_custom_form_validation($string, $key) {
	if ($key === 'usppro_example') {
		$string = '<div class="usp-error">Example error..</div>';
	} elseif ($key === 'usppro_something') {
		$string = '<div class="usp-error">Something error..</div>';
	} elseif ($key === 'usppro_whatever') {
		$string = '<div class="usp-error">Whatever error..</div>';
	return $string;
add_filter('usp_display_errors_custom', 'usp_get_custom_form_validation', 10, 2);

You can see what is happening in this function. Basically we check the passed $key variable, to see if it matches any of our custom strings (that we set up in Step 1). If any matches, we display whatever error/warning information is required.

So that’s the basic gist of the technique. By enabling developers to tap into the form-validation and error-display process, it’s possible to implement custom validation for any of your USP Form fields. Enjoy!