I'm trying to use update_user_meta() to create a trace of activity by placing calls in the code like:

update_user_meta(1, 'dbg'.time(), "R1:Setting href to: " . $href);

Then I go into phpMyAdmin and look at the metadata for user 1 and see all the debug points that were posted:

enter image description here

This works pretty well. But I notice that if I place two trace calls in the code, one immediately after the other, like the two update_user_meta lines below, only the second one gets posted to the database:

$href = '?add-to-cart=' . $atts['id']; // e.g., ?add-to-cart=

update_user_meta( 1, 'dbg'.time() "R1:Settings href to: " . $href );
update_user_meta( 1, 'dbg'.time() "R1b:Settings href to: " . $href );

Why is this? It's annoying with this little trace system, but it seems like it could be a much bigger problem for WordPress. What if someone is actually writing two pieces of metadata to the wp_usermeta table, one immediately after the other. Will the first fail, with no warning? Does anyone understand what's going on here?

  • That's because they have the same unique ID and PHP runs in no time that time() is the same. Use microtime(1) instead? or _1 _2 ... ? – Samuel Elh Aug 28 '16 at 23:21
  • Yes! You're right. 'dbg'.time() is the meta_key and because the second line is executed before time() has rolled over, both lines have the same meta_key. Thank you! – Steve Aug 29 '16 at 0:08
  • You're welcome. Happy debugging :) – Samuel Elh Aug 29 '16 at 1:11
  • @Steve Posted your answer below, be sure to click the green checkmark to indicate that your question is resolved. – Ethan Jinks O'Sullivan Aug 29 '16 at 1:14

Your Answer

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

Browse other questions tagged or ask your own question.