• coffeesleep

    (@coffeesleep)


    I have a dropdown list to select which country the user is located in. When I use update_user_meta() and update the meta data with a dropdown list, it updates the record with the index number (e.g. 12, meaning I have selected the 13th option from the dropdown) instead of the actual string value (e.g. ‘United States’). How can I fix this with the string value actually be saved?
    (I am using Ultimate Member plugin)

    wp_usermeta table in MySQL record is originally like this,

    
    +----------+---------+----------+-----------------------+
    | umeta_id | user_id | meta_key | meta_value            |
    +----------+---------+----------+-----------------------+
    |      519 |      11 | country  | Canada                |
    +----------+---------+----------+-----------------------+
    

    but when I go to the account.php page and update my field, using the dropdown and select ‘United States’, my meta_value is updated based on the index (int value 12, representing the 13th option), not the selected text.

    
    +----------+---------+----------+-----------------------+
    | umeta_id | user_id | meta_key | meta_value            |
    +----------+---------+----------+-----------------------+
    |      519 |      11 | country  | 12                    |
    +----------+---------+----------+-----------------------+
    

    Here is the code I have on my functions.php

    
    add_action('um_after_account_general', 'showUMExtraFields', 100);
    
    function showUMExtraFields() {
      $id = um_user('ID');
      $output = '';
      $names = array(
        "country",
        "office"
      );
    
      $fields = array(); 
      foreach( $names as $name )
        $fields[ $name ] = UM()->builtin()->get_specific_field( $name );
      $fields = apply_filters('um_account_secure_fields', $fields, $id);
      foreach( $fields as $key => $data )
        $output .= UM()->fields()->edit_field( $key, $data );
    
      echo $output;
    }
    
    add_action('um_account_pre_update_profile', 'getUMFormData', 100);
    
    function getUMFormData(){
      $id = um_user('ID');
      $names = array(
        "country",
        "office"
      );
    
      foreach( $names as $name )
        update_user_meta( $id, $name, $_POST[ $name ] );
    }
    

    This also relates to this topic:
    https://gist.github.com/champsupertramp/c1f6d83406e9e0425e9e98aaa36fed7d#gistcomment-3520506

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    The UM fields are not constructed in a way that’s meaningful for your need. Instead of directly using the return from UM()->fields()->edit_field( $key, $data ), you’d need to construct your own select/option group where each option’s name attribute is the country’s proper name.

    Thread Starter coffeesleep

    (@coffeesleep)

    @bcworkz thank you, would you be able to share a sample or two where I can refer to in order to construct my own select/option group? Something like a source code sample or an explanatory article would be greatly appreciated. Sorry I am still new to coding / WordPress and did some googling but was not able to find a good reference. Thanks again.

    Moderator bcworkz

    (@bcworkz)

    The HTML form field should look like the example at https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select

    Except in your case the field would look more like

    <select name="country" id="country-select">
        <option value="">--Please choose a country--</option>
        <option value="United States">United States</option>
      more countries...
    </select>

    In PHP, the selection would be available as $_REQUEST['country']. To work better with existing code your select tag’s name should match that of the select tag it’s replacing. But if other code is expecting an index number instead of “United States” as a passed value, that code might need modification as well. This gets you a field that sends a more meaningful value to the server, but how that meshes with existing code I couldn’t say.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘update_user_meta() POSTs the index number of an array, not the actual value’ is closed to new replies.