Saving fields
Sanitization and meta field updates are handled automatically by Index. You can also define a custom edit field type to manually save data for individual fields, using an additional resource.
Meta types
All meta field types are saved as strings. For almost all component types, this is the expected behavior (WordPress always stores post meta as strings, even integers). However, the gallery component type is an array of attachment IDs. To handle this, gallery values are joined together and saved as a comma delimited list. If you do not want this behavior (for example, each item should be stored separately), you can set the field type to custom
and use the index_update_field
action to manually save your updates.
Custom types
Some data doesn’t fit neatly into a meta field. For example, WooCommerce provides many methods to safely update product features that require using WooCommerce methods. To handle this, Index provides a custom
type for edit fields, which require manually saving values using the following action:
add_action('index_update_field', function($field, $value, $post_id) {
if($field === 'your-field') {
// manually save your values here
}
});
Sanitization
Index handles all sanitization for both arrays and strings. Before values are even allowed to be looped over, they are passed through the WordPress sanitize_text_field
function. Both arrays and strings are checked. This means that your values have already been sanitized when using the index_update_field
action. You can perform additional validation here, but please note that if you can’t save a field there is no notification provided to the viewer.
Validation
One way to handle validation that the viewer can see is to use the validate
parameter. This is only handled on text fields as all other fields send specific types of data back. The current validation options are:
- price
- number
- url
Note: price validation is inclusive of international formatting so using commas in place of decimals is allowed.