update_user_meta() POSTs the index number of an array, not the actual value
-
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
- The topic ‘update_user_meta() POSTs the index number of an array, not the actual value’ is closed to new replies.