Sanitising multiple inputs in one field. (multidimensional array)
-
Hi!
I’m creating a custom field so I can specify allergies in a dish. What I want to create is something like this:Allergy 1 (e.g. Gluten)
— Checkbox (on/off) — Text input (Specify type of gluten e.g. kind of flour ect.) —Allergy 2 (e.g. Nuts)
— Checkbox (on/off) — Text input (Type of nuts) —— And so on.. —
Currently my code looks like this:
function cmb2_render_dish_allergies( $field, $value, $object_id, $object_type, $field_type ) { // make sure we specify each part of the value we need. $value = wp_parse_args( $value, array( 'gluten' => '', 'nuts' => '', ) ); ?> <ul> <li> <label for="<?php echo $field_type->_id( '_gluten_bool' ); ?>"><?php _e('Gluten', 'textdomain'); ?></label> <?php echo $field_type->checkbox( array( 'name' => $field_type->_name( '[gluten][bool]' ), 'id' => $field_type->_id( '_gluten_bool' ), 'value' => 'on', 'desc' => '', ) ); ?> <?php echo $field_type->input( array( 'name' => $field_type->_name( '[gluten][type]' ), 'id' => $field_type->_id( '_gluten_type' ), 'value' => $value['gluten']['type'], 'desc' => '', ) ); ?> </li> <li> <label for="<?php echo $field_type->_id( '_nuts_bool' ); ?>"><?php _e('nuts', 'textdomain'); ?></label> <?php echo $field_type->checkbox( array( 'name' => $field_type->_name( '[nuts][bool]' ), 'id' => $field_type->_id( '_nuts_bool' ), 'value' => 'on', 'desc' => '', ) ); ?> <?php echo $field_type->input( array( 'name' => $field_type->_name( '[nuts][type]' ), 'id' => $field_type->_id( '_nuts_type' ), 'value' => $value['nuts']['type'], 'desc' => '', ) ); ?> </li> </ul> <br class="clear"> <?php echo $field_type->_desc( true ); } add_filter( 'cmb2_render_dish_allergies', 'cmb2_render_dish_allergies', 10, 5 );When saving I get the error message: “Notice: Array to string conversion in /www.example.dev/wordpress/wp-includes/formatting.php on line 1025”. I think this might be a problem because the array I’m trying to save is not sanitized?
I’m quite new to using custom fields in CMB2 and sanitising/escaping ect. and was hoping some of you guys could explain me how to solve this?
The topic ‘Sanitising multiple inputs in one field. (multidimensional array)’ is closed to new replies.