[wp-hackers] get_user_meta cached
Matthias Breuer
mail at matthiasbreuer.com
Wed Mar 27 18:51:29 UTC 2013
Hi,
upon calling update_user_meta, which is calling the underlying update_metadata function, the cache for that item gets deleted (wp_cache_delete($object_id, $meta_type . '_meta'); L. 156 in wp-includes/meta.php).
Could it be that you are referencing the user meta in your code before the POST request is handled, this would explain how you get an outdated (not yet updated) value. If this is not the case, check try the clean_user_cache function, you can pass a user ID, or an WP_User object.
Normally what i do in a situation like this, and what i think is good practice, is to redirect the user after the POST request is handled to the same page with wp_redirect. This is good, because it clears all POST data and doesn't give an annoying message when the user accidentally or intentionally hits reload (also see http://en.wikipedia.org/wiki/Post/Redirect/Get).
I don't know where exactly you handle the POST request, but doing it on init or a different appropriate action can reduce the load time and handles the redirect early enough to be sent with the headers.
I hope this helps! Feel free to ask further questions if anything is unclear or you need more direct pointers.
Best,
Matthias
Am 27.03.2013 um 19:11 schrieb Joaquin Rodriguez Montero <yojoaquin at gmail.com>:
> He guys, quick one: I'm updating / adding a user meta field via a form
> through POST. It adds / update the data. But when the page refreshes I get
> an old value. How can I avoid that? I guess is because the value was
> cached.. but when I refresh the page again I get the new value.. could
> anyone help me please? I'd like to see the new value immediately after the
> form is sent and the page has refreshed because of this.
>
> On my functions I've got:
>
> function chocks_adds_mark($userid, $workid) {
>
> $meta_key = "work_id_".$workid;
>
> if(!empty($_POST['myMark'])) {
>
> $mark = $_POST['myMark'];
>
> update_user_meta("$userid","$meta_key", "$mark");
>
> }
>
> }
>
> And my form on single-attachment is:
>
> <form name="marksform" method="post" action="<?php calificar($userid,
> $workid); ?>" >
>
> <label>Calificación: </label>
>
> <input type="text" name="myMark" value="<?php if(!empty($calificacion_nota))
> : echo $calificacion_nota; endif; ?>" /><br />
>
> <?php wp_nonce_field('marksform'); ?>
>
> <input type="submit" name="enviar" value="Calificar"/>
>
> </form>
>
> Thanks!
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
More information about the wp-hackers
mailing list