• Resolved bigant841

    (@bigant841)


    Hello,

    I am having a little issue trying to figure out to resolve how meta boxes handle special characters. It seems every time I add a back slash “\” to a field and I publish the page it removes it altogether. How can I go about making my text metabox display the backslash on a post?

    Thank you

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I believe this is going to boil down to the default escape/sanization callbacks that are applied to fields and field types. It’s something that you can provide your own for. See https://github.com/CMB2/CMB2/wiki/Field-Parameters#sanitization_cb and https://github.com/CMB2/CMB2/wiki/Field-Parameters#escape_cb for examples.

    It does note, but DOES NOT recommend, that you can bypass completely by passing in a value of false for either of these.

    Thread Starter bigant841

    (@bigant841)

    Thank you for getting back to me. Is there a recommended way for me to achieve what I am trying to do? Also, what are the repercussions of altering the escape/sanization?

    Mainly what I am trying to do is add a backlash to my text when needed within my text box. For example, I am trying to achieve this ab\cd but when I publish the page it ends up as abcd.

    Plugin Author Justin Sternberg

    (@jtsternberg)

    In order to provide accurate help/advice, it’s best if you can provide your box/field configuration. But generally speaking, if you want to avoid CMB2’s default call to stripslashes_deep, but still have the benefits of the sanitization, you can add 'sanitization_cb' => 'sanitize_text_field', to your field configuration. The reason we need more info is because if you’re using a wysiwyg or non-standard text field, that may be the wrong sanitization callback to use.

    Thread Starter bigant841

    (@bigant841)

    I am mainly focusing on my repeatable group type= text with the id company_name.

    Attached is the code I aim using

    add_action( 'cmb2_admin_init', 'lia_jury_metabox' );
    function lia_jury_metabox() {
    	$prefix = 'jury_';
    	$cmb_jury = new_cmb2_box( array(
    		'id'            => $prefix . 'metabox',
    		'title'         => __( 'Jury President Panel' ),
    		'object_types'  => array( 'jury', ), // Post type
    	) );
    $group_field_id = $cmb_jury->add_field( array(
    			'id'          => 'jury_team',
    			'type'        => 'group',
    			'options'     => array(
    				'group_title'   => __( 'Team Member {#}' ),
    				'add_button'    => __( 'Add Another Member'),
    				'remove_button' => __( 'Remove Member'),
    				'sortable'      => true, // beta
    			),
    		) );
    			$cmb_jury->add_group_field( $group_field_id, array(
    				'name' => 'Name',
    				'id'   => 'jury_name',
    				'type' => 'text',
    			) );
    			$cmb_jury->add_group_field( $group_field_id, array(
    				'name' => 'Job Title',
    				'id'   => 'job_title',
    				'type' => 'text',
    			) );
    			$cmb_jury->add_group_field( $group_field_id, array(
    				'name' => 'Company',
    				'id'   => 'company_name',
    				'type' => 'text',
    			) );
    Plugin Author Justin Sternberg

    (@jtsternberg)

    Thank you for providing that. So it turns out, WordPress itself removes the slashes, so if you explicitly WANT the slashes, then you need to extra-slash it before it saves. You can, again, do that with the sanitization_cb parameter:

    
    $cmb_jury->add_group_field( $group_field_id, array(
    	'name' => 'Company',
    	'id'   => 'company_name',
    	'type' => 'text',
    	'sanitization_cb' => function( $value ) {
    		return wp_slash( sanitize_text_field( $value ) );
    	},
    ) );
    
    Thread Starter bigant841

    (@bigant841)

    Perfect! Thank you so much for all your help.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Special Character gets deleted from metabox once I publish the page’ is closed to new replies.