How can I filter the contents of a metafield before it’s displayed in the admin?

With the cmb2 plugin I created a metabox with a textarea metafield this way:

function myprefix_register_metabox() {

$prefix = ‘myprefix_’;

$cmb = new_cmb2_box( array(
‘id’ => $prefix . ‘metabox’,
‘title’ => esc_html__( ‘My Metabox’, ‘cmb2’ ),
‘object_types’ => array( ‘page’ ),
) );

$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’,
) );

}
add_action( ‘cmb2_admin_init’, ‘myprefix_register_metabox’ );

I use this callback function to sanitize the metafield content:

function my_sanitize_text_callback( $value, $field_args, $field ) {

$value = htmlentities( html_entity_decode( $value, ENT_QUOTES ), ENT_QUOTES );

return $value;
}

Everything works fine, but how can I view the content of the textarea stored in the db, with the converted entities html_entity_decode() in the edit screen in the admin panel, to get a more human readable text?

Read more here:: How can I filter the contents of a metafield before it’s displayed in the admin?

Leave a Reply

Your email address will not be published. Required fields are marked *