Please provide example for custom sanitization of nested subgroup fields
Support › MB Group › Please provide example for custom sanitization of nested subgroup fieldsResolved
- This topic has 6 replies, 2 voices, and was last updated 2 years, 6 months ago by
Prabakaran Shankar.
-
AuthorPosts
-
September 27, 2022 at 9:37 PM #38436
Prabakaran Shankar
ParticipantIn 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?September 27, 2022 at 11:24 PM #38442Long Nguyen
ModeratorHi,
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
September 28, 2022 at 9:07 AM #38445Prabakaran Shankar
ParticipantHi, 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
September 28, 2022 at 10:48 PM #38463Long Nguyen
ModeratorHi,
If you want to sanitize a custom field in a subgroup, for example
sub_text1
, please change the code to this oneif( !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/September 29, 2022 at 6:16 PM #38479Prabakaran Shankar
ParticipantHi, 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.
September 30, 2022 at 10:22 PM #38494Long Nguyen
ModeratorHi,
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 ); } }
September 30, 2022 at 10:28 PM #38496Prabakaran Shankar
ParticipantThank you so much.
-
AuthorPosts
- You must be logged in to reply to this topic.