Get radio buttons consistant values inside function to update Wordpress userdata

52 views Asked by At

I have the following function with an array of variables that handles a profile form. All but one is a typical input, with the exception of 'member-comptency[]'. It's a select field with several possible values. Right now, the form saves, all the data comes through as expected with the exception of that field which simply returns a number which I assume is the HTML value. In looking at it a bit more, the "value" of the element is simply a number also, which might make matters worse.

I've tried in vain several times to modify this in different ways, but only produces a 'no-save' situation, or further non-descrip errors. I understand this field is an array itself, but not sure how to even ask a better question to get myself on track.

The function

function getUMFormData(){
    $id = um_user('ID');
        $names = array(
            'member-company',
            'member-phone',
            'member-website',
            'member-competency[]',
            'member-contact-billing-name'
        );

        foreach( $names as $name )
        update_user_meta( $id, $name, $_POST[$name] );
}

The HTML element

One of several choices

<label class="um-field-radio  um-field-half  right ">
  <input type="radio" name="member-competency[]" value="1">

  <span class="um-field-radio-state">
    <i class="um-icon-android-radio-button-off"></i>
  </span>

  <span class="um-field-radio-option">Manufacturer</span>
</label>

The desired outcome

I need to get the word from the last <span> element (Manufacturer in the example) to save into the getUMFormData() function.

2

There are 2 answers

4
LoicTheAztec On BEST ANSWER

From your HTML example, note that the "Manufacturer" radio button has "1" as value.

Now, in your function, the meta key to be used is 'member-competency' without [].

Then note that $_POST['member-competency'] will give an array like array('1') because of [] in member-comptency[] attribute name from the input radio button HTML.

If you get array('1') from $_POST['member-competency'] is that the selected radio button is "Manufacturer", and you can set "Manufacturer" as meta value.

So your function will be:

function getUMFormData(){
    $user_id = um_user('ID');

    $fields  = array(
        'member-company',
        'member-phone',
        'member-website',
        'member-competency', // <== Removed "[]"
        'member-contact-billing-name'
    );

    foreach( $fields as $meta_key ) {
        if ( $user_id > 0 && isset($_POST[$meta_key]) ) {
            $meta_value = $_POST[$meta_key];
            // Targeting 'member-competency' field with "Manufacturer" label
            if ( $meta_key === 'member-competency' && is_array($meta_value) && current($meta_value) == '1' ) {
                update_user_meta( $user_id, $meta_key, __("Manufacturer") );
            } 
            // Other fields
            else {
                update_user_meta( $user_id, $meta_key, esc_attr($meta_value) );
            }
        }
    }      
}

It should work.


Addition:

In your radio buttons HTML, to copy the label name to the input attribute "value" you can use the following:

add_action('wp_footer', 'wp_footer_custom_script', 10);
function wp_footer_custom_script() {
    ?><script>
    jQuery(function($){
        $('[name^=member-competency]').each(function(){
            $(this).attr('value', $(this).parent().find('span.um-field-radio-option').text());
        });
    });
    </script><?php
}

Then your function will change slightly:

function getUMFormData(){
    $user_id = um_user('ID');

    $fields  = array(
        'member-company',
        'member-phone',
        'member-website',
        'member-competency', // <== Removed "[]"
        'member-contact-billing-name'
    );

    foreach( $fields as $meta_key ) {
        if ( $user_id > 0 && isset($_POST[$meta_key]) ) {
            $meta_value = $_POST[$meta_key];
            // Targeting 'member-competency' field with "Manufacturer" label
            if ( $meta_key === 'member-competency' && is_array($meta_value) ) {
                update_user_meta( $user_id, $meta_key, esc_attr( current($meta_value) ) );
            } 
            // Other fields
            else {
                update_user_meta( $user_id, $meta_key, esc_attr($meta_value) );
            }
        }
    }      
}

It should work and solve your worry.

0
Roy Croezen On

I suppose 'member-competency[]' is not the right way to access an array of values within the post array. You could try to var_dump() or print_r() the $_POST['member-competency']; See if the array is displayed properly.