Support » Plugin: Participants Database » Previous values on pdb-after_submit_update?

  • Resolved mairabay

    (@mairabay)


    When using the pdb-after_submit_update filter, is there any (simple) way to access the previous values of the participant before it was updated? (similar to how WordPress provides the $old_user_data in the profile_update)

    I am trying to keep the email addresses from PDB and WP in sync. I was able to use the profile_update mentioned above to update PDB when user changes their email in WP. But to update WP when PDB changes doesn’t seem to be that simple.

    I can’t use the tutorial explained here because in my site the users sign up for PDB before signing up for WP.

    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.

    I thought of creating a user meta field in the WP user when they sign up to save the PDB_ID (and use it to retrieve the WP user when the PDB user is updated), but I wanted to ask here just in case there is a simpler solution. Is there? 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author xnau webdesign

    (@xnau)

    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.

    Thread Starter mairabay

    (@mairabay)

    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

    Plugin Author xnau webdesign

    (@xnau)

    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.

    Thread Starter mairabay

    (@mairabay)

    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 2 years, 1 month ago by mairabay. Reason: added link to closure and fixed typo
    Plugin Author xnau webdesign

    (@xnau)

    Nicely done…it’s a great technique, I use it all the time.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Previous values on pdb-after_submit_update?’ is closed to new replies.