• Resolved noorakhtar

    (@noorakhtar)


    I have created multicheck custom fields in WordPress user profile page. But by selecting checkboxes values are not storing in database. Rather keys are storing in json format in meta_value column. Why is this happening? How can I store the values of the checkboxes in database?

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

    (@tw2113)

    The BenchPresser

    Can you provide your CMB2 configuration code so that we can review what’s being used and what may be going on?

    Thread Starter noorakhtar

    (@noorakhtar)

    add_action( 'cmb2_admin_init', 'tprofile_user_fighting_meta');
            function tprofile_user_fighting_meta(){
        $tprofile_user_fight_info = new_cmb2_box(
            array(
                'id'            => 'fighting-info',
                'object_types'  => array( 'user' ),
                'title'         => __( 'Fighting Profile', 'tprofile' )
            )
        );
        $tprofile_user_fight_info->add_field(
            array(
                'id'            => '_user-fighting-style',
                'name'          => __( 'Your fighting styles', 'tprofile' ),
                'type'          => 'multicheck',
                'options'       => array(
                    'MMA'               => __('MMA', 'tprofile' ),
                    'Kikboxing'         => __( 'Kikboxing', 'tprofile' ),
                    'Boxing'            => __( 'Boxing', 'tprofile' ),
                    'Judo'              => __( 'Judo', 'tprofile' ),
                    'Taekwondo'         => __( 'Taekwondo', 'tprofile'),
                    'Karate'            => __( 'Karate', 'tprofile' ),
                    'Brazilian'         => __( 'Brazilian Jiu-jitsu', 'tprofile' ),
                    'Wrestling'         => __( 'Wrestling', 'tprofile' ),
                    'Capoeira'          => __( 'Capoeira', 'tprofile' ),
                    'Muay Thai'         => __( 'Muay Thai', 'tprofile' ),
                    'Karate Kyokushinkai'=> __('Karate Kyokushinkai', 'tprofile' ),
                    'Kudo Daido Juku'   => __('Kudo Daido Juku', 'tprofile' ),
                )
            )
        );
    }
    

    Values are saving in database in this way: a:3:{i:0;s:3:”MMA”;i:1;s:4:”Judo”;i:2;s:6:”Karate”

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I’m pretty sure that this is the intended default behavior. The value parts from the options array are just meant for the checkbox labels themselves.

    That said, you can provide the keys however which ways you like from what I’m seeing. For example:

    'options' => array(
    	'Mixed Martial Arts'  => __( 'MMA', 'tprofile' ),
    )
    

    var_dump output below:

    array (size=1)
      0 => string 'Mixed Martial Arts' (length=18)
    
    Thread Starter noorakhtar

    (@noorakhtar)

    Thank you Michael. I understood that. But things I can’t do is that I want to display the checked values in wp_list_table. When I am writing this code to retrieve the value

    $tprofile_users = get_users( array( 'role__in' => array( 'subscriber' ) ) );
    foreach( $tprofile_users as $tprofile_user ){
    	global $current_user;
    	$user_id = $current_user->ID;
    	$tprofile_user_cmeta = get_the_author_meta( $user_id );
    	$data[] = [
    		'fighting_style' => esc_html( $tprofile_user->$tprofile_user_cmeta['_user-fighting-style'][0] ),
    }

    Result is showing in this way:a:5:{i:0;s:3:”MMA”;i:1;s:9:”Kikboxing”;i:2;s:6:”Boxing”;i:3;s:4:”Judo”;i:4;s:9:”Taekwondo”;}
    Could you please show me how can I display the result in this way : MMA, Kikboxing, Boxing, Judo, Taekwondo
    I couldn’t find any solution anywhere. Thank you for taking care my issue.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Looks like your current attempt is not unserializing data, thus why you’re getting that JSON-like result.

    Give this a try instead, I simplified a bit but also tested it out.

    $data = [];
    $tprofile_users = get_users( array( 'role__in' => array( 'subscriber' ) ) );
    foreach ( $tprofile_users as $tprofile_user ) {
    	$tprofile_user_cmeta = get_user_meta( $tprofile_user->ID, '_user-fighting-style', true );
    	$data[] = [ 'fighting_style' => esc_html( implode( ', ', $tprofile_user_cmeta ) ) ];
    }
    
    Thread Starter noorakhtar

    (@noorakhtar)

    THANK YOU SO MUCH, MICHAEL. Thank You So Much. Great support you have provided!! The problem is solved!!!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Multicheck fields sending keys instead of values in database.’ is closed to new replies.