• Resolved ysnz

    (@ysnz)


    Hello, I’ve got a stupid question but can’t find any solutions on that.
    I’m able to set key-value-pairs for multiselects and dropdowns with the options callback.
    But how can I add key-value-pairs for checkboxes?
    There is noch options callback available.. is it possible to do this right in the textfield with something like?
    key: value

    I tried different separators, but can’t make it work.

    Anyone got a solution on that?

    Thanks for your help!

    • This topic was modified 5 years, 3 months ago by ysnz.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @ysnz

    It’s a bit complicated but here’s the code snippet to achieve the Key-Pair values for checkbox:

    
    function um_021821_checkbox_array(){
        return [
            'apple' => 'Apple',
            'banana' => 'Banana',
            'carrot' => 'Carrot'
        ];
        
    }
    add_filter("um_field_checkbox_item_title", function( $um_field_checkbox_item_title, $key, $v, $data ){
        
        if( $key == 'key-pair' ){
            $arr = um_021821_checkbox_array();
            $um_field_checkbox_item_title = $arr[ $v ];
        }
    
        return $um_field_checkbox_item_title;
     }, 10, 4);
    
     add_filter("um_edit_key-pair_field_value", function( $value, $key ){
        $arr = um_021821_checkbox_array();
        return $arr[ $value ];
     }, 10, 2);
    
    add_filter("um_profile_field_filter_hook__key-pair",function( $value, $data ){
    
        $arr = explode(", ", $value );
        $arr_reference = um_021821_checkbox_array();
        $arr_new_format = [];
        foreach( $arr as $a ){
            $arr_new_format[ ] = $arr_reference[ $a ];
        }
    
        return implode(", ", $arr_new_format);
    
    },10, 2);

    key-pair is the name of the checkbox field so you will have to change it in the code snippet above to match your field’s meta key.

    um_021821_checkbox_array is function where you add the key-pair. You also need to add the values to the checkbox options via Form Builder. Use the slugs( e.g. apple, banana etc. ) as option values.

    Regards,

    • This reply was modified 5 years, 2 months ago by Champ Camba.
    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @ysnz

    Please feel free to re-open this thread by changing the Topic Status to ‘Not Resolved’ if any other questions come up and we’d be happy to help. 🙂

    Regards,

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Register Form: Key-Value-Pair for Checkbox’ is closed to new replies.