I have seen that CMB2 offers the possibility to create a custom escaping function with which the values are checked before they’re displayed. So I set the escape_cb
parameter in the metafield this way:
$cmb->add_field( array(
'name' => esc_html__( 'Text Area for Code', 'cmb2' ),
'id' => $prefix . 'textarea_code',
'type' => 'textarea_code',
'sanitization_cb' => 'my_sanitize_text_callback',
'escape_cb' => 'my_escape_text_callback'
) );
Then I created the callback function for escaping, in order to convert HTML entities to their corresponding characters:
function my_escape_text_callback( $value, $field_args, $field ) {
$escaped_value = html_entity_decode( $value, ENT_QUOTES );
return $escaped_value;
}
Now it works great and in the edit screen in the admin panel I visualize the readable text.