0

Is this is possible to update the user meta without lose the old data it have ?

For example :

i have the following data in my user meta field name user_documents

a:4:{i:0;s:36:"817fb3ec8e2412b141bcc40c1e16d4c9.jpg";i:1;i:91;i:2;s:27:"8769106-alone-wallpaper.jpg";i:3;i:92;}

this is about the attached files with the user_documents meta, if suppose a user upload more files i want to store it into the user_documents again but without lose the old meta which is above.

  • By default add_user_meta do not delete the old values. Check the last $unique parameter. codex.wordpress.org/Function_Reference/add_user_meta – M-R Mar 25 '15 at 14:46
  • @M-R thanks for the comment, but the $unique parameter defines that it doesn't repeat the same meta_key, i want to update the meta_value – jothikannan Mar 26 '15 at 6:14
1

If I understand problem correctly, you want to store name of multiple files in user meta field. add_user_meta function has fourth argument $unique which is false by default that means for each call to add_user_meta a new key-value pair is added, even if key already exists.

add_user_meta has less clarification then the add_post_meta that does exactly same thing for posts. Here it says, how it works

Note that if the given key already exists among custom fields of the specified post, another custom field with the same key is added unless the $unique argument is set to true, in which case, no changes are made. If you want to update the value of an existing key, use the update_post_meta() function instead.

So, you while uploading a file you will call add_user_meta with a new filename.

add_user_meta($user_id, 'user_documents','filename.txt');

Each time it will add a new pair, that can be fetched using get_user_meta function

get_user_meta($user_id, 'user_documents'); // set $single to false to fetch all values against 'user_documents' key.

It will return an array of all values stored against 'user_documents'.

To update a filename, you will need to provide filename against which you want to update.

update_user_meta($user_id, 'user_documents', 'filename.txt','new_filename.txt');

or you can delete a file by specifying the particular file name.

delete_user_meta($user_id, 'user_documents', 'filename.txt')

I hope, it clarifies how you would go for it.

  • let me try this, on before it if suppose there is more then one files on the meta value which stored as the json string, it will work or ? – jothikannan Mar 26 '15 at 13:51
  • Why do you want more than one file, json encoded, in one instance? Separating them would make them more managable and you dont need to json encode them. – M-R Mar 26 '15 at 15:15
  • So if we add new files by add_user_meta it will add new meta with the same key, and we can get everything together by get_user_meta ? is it ? – jothikannan Mar 27 '15 at 4:05
  • One more question about the delete, if i delete a meta with the specific file it doesn't work, because my user_document meta have the value like a:2:{i:0;s:10:"sample.doc";i:1;i:105;}, so it's deleting value, any guess ? – jothikannan Mar 27 '15 at 7:20
  • Sorry, I dont get your question if i delete a meta with the specific file it doesn't work, ... so it's deleting value.Btw, Why do you have to store it as a serialize array? I would recommend to convert it to a string. If you have to store "105", store it differently, may be, with separation of a special character. – M-R Mar 27 '15 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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