For something like that, you need to use the pdb-before_submit_update filter. In that case, the record ID is in the submitted data, so you can get the db values with that.
The pdb-after_submit_update action happens after the db has been updated. so the previous values are not available at that point.
I think you didn’t understand my initial comment.
I said
I also don’t want to use pdb-before_submit_update
because there might be errors in the submission and the information might not have been saved at this point.
To make it more clear: let’s say I update the user’s email in WP during the pdb-before_submit_update
with the new email submitted by the user in the PDB form. But it turns out that something else was wrong in the form (let’s say date_of_birth
) and the user decides not to make any updates after all. Then I will have already updated the WP user with the new email which the user gave up on adding to PDB. Do you understand now why I can’t use pdb-before_submit_update
? I hope it’s more clear now.
If so, can you see any solution (with your current system) to what I want to do?
Thanks
OK, sorry. You really have no choice but to use the “before” filter, so it’s a matter of figuring out how that can work. Here are a couple of options:
The pdb-before_submit_update filter happen after the form is validated, in other words, it won’t get called unless the form validates. That may be all you need.
If you need to do some other checks on the submitted data, you can use the pdb-before_submit_update filter to set an action handler on pdb-after_submit_update.
The way this works is in your handler function for the pdb-before_submit_update filter, you first do whatever checks on the old data/new data you need to do, then if it checks out, add your pdb-after_submit_update action handler in that function.
Thank you so much!
That was such a good idea. And much simpler than the route I was taking.
I’m fairly new to hooks, filters and all that (I’ve been programming for 20 years but only started with WP 2 months ago), so it took me a while to wrap my head around what you said (I even had to write it down on paper, lol).
But doing some research I figured out how to do what I wanted, using your idea and using a closure. Here is the working code in case someone else runs into the same problem:
add_filter('pdb-before_submit_update', 'myplugin_update_email_on_WP', 10, 1);
function myplugin_update_email_on_WP($pdb_form_data){
$participant_id = $pdb_form_data['id'];
if($participant_id){
if(class_exists('Participants_Db')){
$old_participant = Participants_Db::get_participant($participant_id);
if($old_participant){
$old_email = $old_participant['email'];
add_action('pdb-after_submit_update', function ($participant) use ($old_email){
$wp_user = get_user_by('email', $old_email);
if(!empty($wp_user)){
$wp_user_id = $wp_user->ID;
$new_email = $participant['email'];
wp_update_user(array( 'ID' => $wp_user_id, 'user_email' => $new_email ));
}
}
,10,1);
}
}
}
return $pdb_form_data;
}
Again, thank you so much for this tip. It worked and I really appreciate your help and appreciate you making this plugin available for free. I will write a very good review for you 🙂
-
This reply was modified 3 years ago by
mairabay. Reason: added link to closure and fixed typo
Nicely done…it’s a great technique, I use it all the time.