There are many validation options included within Redux. You can also build your own validation option if necessary.
Required Fields
To make a field required you will need to use the following setting:
'validate' => 'not_empty'
Standard Validation Options
- HTML:
'validate' => 'html'
- No HTML:
'validate' => 'no_html'
- CSS:
'validate' => 'css'
- Javascript:
'validate' => 'js'
- Hex Color Value:
'validate' => 'color'
- RGBA Color Value:
'validate' => 'colorrgba'
(for use with'type' => 'color_rgba'
) - Email:
'validate' => 'email'
- URL:
'validate' => 'url'
- Numeric:
'validate' => 'numeric'
- Comma Numeric:
'validate' => 'comma_numeric'
- Alpha Numeric:
'validate' => 'no_special_chars'
- Unique Slug:
'validate' => 'unique_slug'
w/ optionalflush_permalinks
arg that can be add to a field to flush permalinks on successful save.
Advanced Validation Customization Options and Hooks
Custom HTML Validation
You can easily set what HTML is allowed in one of your fields by using the code below. This utilizes wp_kses from core. You can find more information about this here: http://codex.wordpress.org/Function_Reference/wp_kses
'validate' => 'html_custom', 'allowed_html' => array( 'a' => array( 'href' => array(), 'title' => array() ), 'br' => array(), 'em' => array(), 'strong' => array() )
Replacement Validation
You can use validation to change what the user enters in a text field to follow whatever pattern or design you need. There are currently 2 options for replacement validation:
String Replace Hook
'validate' => 'str_replace', 'str' => array( 'search' => ' ', 'replacement' => 'thisisaspace' )
Preg Replace Hook
'validate' => 'preg_replace', 'preg' => array( 'pattern' => '/[^a-zA-Z_ -]/s', 'replacement' => 'no numbers' )
Custom Validation
You can also create your own custom validation if Redux doesn’t have what you need. Here’s how…
-
Enter your field validation
'validate_callback' => 'your_custom_validate_callback_function'
-
Create your callback function
/** * Custom function for the callback validation referenced above **/ if (!function_exists('your_custom_validate_callback_function')): function your_custom_validate_callback_function($field, $value, $existing_value) { $error = false; $value = 'just testing'; /* do your validation if(something) { $value = $value; } elseif(something else) { $error = true; $value = $existing_value; $field['msg'] = 'your custom error message'; } */ $return['value'] = $value; if ($error == true) { $return['error'] = $field; } return $return; } endif;
Fields that Don’t Accept Validation
There are a few fields that do not accept validation currently. Those fields are listed below.
- Checkboxes
- Multi-Checkboxes
- Radio Buttons
- Select Fields
- Muli-Select Fields
- Image Select Fields
- WordPress Select Fields (categories, pages, posts, users, tags, menus, post types, roles, capabilities)
- Icon Font Select Fields
- Date Fields
- Button Sets