Support Forum
Support › Meta Box Group › Please provide example for custom sanitization of nested subgroup fieldsResolved
In the custom sanitization callback, how to get the data from nested subgroups. Because when I use wp_kses to sanitize the nested subgroup type: textarea, it results "PHP Fatal error: Uncaught TypeError: preg_match(): Argument #2 ($subject) must be of type string".
Do I need to unserialize the nested subgroup data and serialize it again before returning $value?
Hi,
Please follow this documentation to know how to create a sanitization callback for a subfield in a group, the sub-subfield in a subgroup will work like that https://docs.metabox.io/sanitization/#sanitizing-subfields-in-a-group
Hi, thank you for the reference.
add_filter( 'rwmb_meta_boxes', function ( $meta_boxes ) {
$meta_boxes[] = [
'title' => 'Multi-level nested groups',
'fields' => [
[
'id' => 'group',
'type' => 'group',
'sanitize_callback' => 'my_subfield_validation',
'fields' => [
[
'name' => 'Text',
'id' => 'text',
],
[
'name' => 'Sub group',
'id' => 'sub_group',
'type' => 'group',
'fields' => [
// Normal field (cloned)
[
'name' => 'Sub text1',
'id' => 'sub_text1',
'type'=> 'text',
],
[
'name' => 'Sub text2',
'id' => 'sub_text2',
'type'=> 'textarea',
],
],
],
],
],
],
];
return $meta_boxes;
} );
function my_subfield_validation( $value ) {
// this is for fields
if ( empty( $value['text'] ) ) {
$value['text'] = 'Hello World!';
}
// this is for sub-group
$sub_group = isset($value['sub_group'])? $value['sub_group']:[];
$tags = array();
if(!empty($sub_group)){
$value['sub_group'] = wp_kses($value['sub_group'], $tags);
}
//above condition results "PHP Fatal error: Uncaught TypeError: preg_match(): Argument #2 ($subject) must be of type string"
return $value;
}
how to pass sub-group value to sanitize
Hi,
If you want to sanitize a custom field in a subgroup, for example sub_text1
, please change the code to this one
if( !empty( $sub_group ) ){
$value['sub_group']['sub_text1'] = wp_kses_post( $sub_group['sub_text1'] );
}
$value['sub_group']
is not a string to sanitize so the error message will appear. Please read more on the documentation https://developer.wordpress.org/reference/functions/wp_kses/
https://developer.wordpress.org/reference/functions/wp_kses_post/
Hi, thank you for the above example. Could you please provide example for cloneable? For example, consider 'sub_text1' is the id of cloneable textarea field in the subgroup.
Hi,
If the field sub_text1
is a cloneable field, so the value is an array of values. You can sanitize a specific element of the array via the key. For example
$sub_group = isset( $value['sub_group'] ) ? $value['sub_group'] : [];
$sub_text1 = $sub_group['sub_text1'];
if( !empty( $sub_text1 ) && is_array( $sub_text1 ) ){
$value['sub_group']['sub_text1'][0] = wp_kses_post( $sub_text1[0] );
}
or this code if you want to sanitize all elements in the array
if( !empty( $sub_text1 ) && is_array( $sub_text1 ) ) {
foreach ($sub_text1 as $key => $text_value) {
$value['sub_group']['sub_text1'][$key] = wp_kses_post( $text_value );
}
}
Thank you so much.