0

I have a custom user registration form and am trying to store multiple values in the usermeta. This is a sample code I am using, however it just stores the last option selected (i.e. if I select from Volvo to Opel, it will only store Opel in the database), whereas what I am wanting is all the values selected to be stored. Im not sure what Im doing wrong.

Any help would be appreciated.

<select name="test" id="test" multiple>
      <option value="volvo">Volvo</option>
      <option value="saab">Saab</option>
      <option value="opel">Opel</option>
      <option value="audi">Audi</option>
    </select>

    update_usermeta( $new_user, 'test', $_POST['test'] );
1
  • See this answer for how to store array data. It is using update_option(), but that works the same way as update_user_meta().
    – fuxia
    May 22, 2013 at 19:22

1 Answer 1

1

Subtle difference in your name declaration, which needs to be name="test[]".

<select name="test[]" id="test" multiple>
      <option value="volvo">Volvo</option>
      <option value="saab">Saab</option>
      <option value="opel">Opel</option>
      <option value="audi">Audi</option>
</select>

    update_usermeta( $new_user, 'test', $_POST['test'] );

Edit re: your comment,

$meta = get_user_meta($user_id, 'test'); 
foreach ($meta as $key => $value) { 
    echo $value; 
}
4
  • This was correct, however how do I print the array so it shows the values. At present it displays like this in the user profile Array ( [test] => Array ( [0] => volvo [1] => saab [2] => opel [3] => audi ) ) May 22, 2013 at 17:24
  • Thank you - that didn't work but I tried this '$meta = get_the_author_meta( 'test', $user->ID, true ); foreach($meta['test'] as $meta) { echo $meta['test'], '<br>'; }' and it now shows up the values, but only the first letter? May 22, 2013 at 17:34
  • $user_id is a placeholder for $user->ID, you need to make sure you pass the actual ID of the user to the function. Hard to tell without seeing your full code. Your foreach function is also incorrect.
    – Adam
    May 22, 2013 at 17:38
  • 1
    update_usermeta is deprecated as of Version 3.0. Please use update_user_meta instead May 22, 2013 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.