Support

Account

Home Forums Front-end Issues Don't update email if it exists. Show error on same page in

Solved

Don't update email if it exists. Show error on same page in

  • My issue is as follows.

    I am creating a shortcode which outputs the ACF form with a custom field group. Some of those fields will update the corresponding user fields. I want to have users update their profiles using ACF and custom fields like apartment number and phone(s)

    1. My problem is that I want the email to not update if it already exists. So if the user enters an email already taken by another user I’d like it to show an error at the top of the page and not submit any values.

    Actually it would be even better if it showed as an error. See Screenshot G.

    But what happens is it posts and then the error is just on a page with nothing else. See screenshot D. The email is also updated anyway – see screenshot E.

    Although the ACF custom field is updated but not the WordPress user which makes sense because it must be unique. See screenshot F.

    So basically whatever condition I have outputs an error on a page after posting all by itself. And even though I say do nothing if the email exists, it still updates the email.

    Screenshots

    ACF form generated from field group. Email, first name, lastname, meant to update the corresponding user fields.
    https://zorahosting.com/user form.png

    Here is the container showing a custom class ‘form-update-container’. This is the container of the shortcode
    A. https://zorahosting.com/form-update-container.png

    This is the shortcode in the WYSIWYG WordPress editor
    B. https://zorahosting.com/userupdate-shortcode.png

    Shortcode is not showing up in the container but outside of it
    C. https://zorahosting.com/outside-of-container.png

    Error: ‘the email already exists’ shows up on a new page. I’d like the error to show up somewhere else. either at top or bottom of form. In the #message part above the form.
    D. https://zorahosting.com/email-exists-in-window.png

    Email is updated even though it exists. Although the ACF email is allowed to update. It has not updated the user because WordPress won’t allow to of the same email.
    E. https://zorahosting.com/email-updated-anyway.png

    ACF email updated not user from email that existed on form submit
    F. https://zorahosting.com/acf-updated-not-user.png

    Red error box generated by regular expression ( this is not an email )
    G. http://zorahosting.com/red-error-box.png

    Here below is my code

    The shortcode outputting the form:

    
    
    function zUpdate( $atts, $content = null ) {
    	
    	$current_id = get_current_user_id();
    	$user_id = 'user_' . $current_id;	
    	
    	$options = array(
    	    'post_id' => $user_id,
    	    /* my field group grabs unit, home phone, and cell phone acf */
    	    'field_groups' => array('3307', ),
    	    'submit_value' => 'Update' 
    	);
    	$form = acf_form( $options );
    	return $form;	
    
    }
    
    

    The shortcode updating the form with ACF data updating the WP user data

    
    
    function my_acf_save_post( $post_id ) {
    		if( empty($_POST['acf']) ) {
    			return;
    		}
    		
    		if( is_admin() ) {
    			return;
    		}
    		
    		//get current email before change
    		$current_email = get_the_author_meta('user_email', $wp_user_id);
    		
    		if( $_POST['post_id'] != 'new' ) {
    			
    			$emailField = $_POST['acf']['field_5ae7a967009de'];
    			$wp_user_id = str_replace("user_", "", $post_id);
    			
    			if (isset($emailField)) {
    				if (email_exists( $emailField )) {
    					echo "Email already exists
    ";
    					update_field('field_5ae7a967009de', $current_email, $post_id);
    				} else {
    					$args = array(
    						'ID'         => $wp_user_id,
    						'user_email' => esc_attr( $emailField )
    					);            
    					wp_update_user( $args );
    				}  
    			}
    			$first_name = $_POST['acf']['field_5ae7a4b8e5948'];
    			$last_name = $_POST['acf']['field_5ae7a8cf8e3e0'];
    			$user_data = wp_update_user( array( 'ID' => $wp_user_id, 'first_name' => $first_name, 'last_name' => $last_name ) );	
    		}
    		
    		return $post_id;
    }
    
    

    both are called at the end of course:

    
    
    add_action('acf/save_post', 'my_acf_save_post', 1);
    
    add_shortcode('userupdate', 'zUpdate');
    
    
  • My issue is as follows. I am creating a shortcode which outputs the ACF form with a custom field group. Some of those fields will update the corresponding user fields. I want to have users update their profiles using ACF and custom fields like apartment number and phone(s)

    1. First problem is that the ACF form outputted by the shortcode is going outside of the WYSIWYG editor and the container. You can see that in Screenshot A. B. and C.

    2. My other problem is that I want the email to not update if it already exists. So if the user enters an email already taken by another user I’d like it to show an error at the top of the page and not submit any values. Actually it would be even better if it showed as an error. See Screenshot G. But what happens is it posts and then the error is just on a page with nothing else. See screenshot D. The email is also updated anyway – see screenshot E. Although the ACF is updated but not the WordPress user which makes sense because it must be unique. See screenshot F.

    Screenshots

    ACF form generated from field group. Email, first name, lastname, meant to update the corresponding user fields.
    https://zorahosting.com/user form.png

    Here is the container showing a custom class ‘form-update-container’. This is the container of the shortcode
    A. https://zorahosting.com/form-update-container.png

    This is the shortcode in the WYSIWYG WordPress editor
    B. https://zorahosting.com/userupdate-shortcode.png

    Shortcode is not showing up in the container but outside of it
    C. https://zorahosting.com/outside-of-container.png

    Error: ‘the email already exists’ shows up on a new page. I’d like the error to show up somewhere else. either at top or bottom of form. In the #message part above the form.
    D. https://zorahosting.com/email-exists-in-window.png

    Email is updated even though it exists. Although the ACF email is allowed to update. It has not updated the user because WordPress won’t allow to of the same email.
    E. https://zorahosting.com/email-updated-anyway.png

    ACF email updated not user from email that existed on form submit
    F. https://zorahosting.com/acf-updated-not-user.png

    Red error box generated by regular expression ( this is not an email )
    G. http://zorahosting.com/red-error-box.png

    Here below is my code

    The shortcode outputting the form:

    – – – – – – – – – – – – – – – – – – – – – – –

    
    
    function zUpdate( $atts, $content = null ) {
    	
    	$current_id = get_current_user_id();
    	$user_id = 'user_' . $current_id;	
    	
    	$options = array(
    	    'post_id' => $user_id,
    	    /* my field group grabs unit, home phone, and cell phone acf */
    	    'field_groups' => array('3307', ),
    	    'submit_value' => 'Update' 
    	);
    	$form = acf_form( $options );
    	return $form;	
    
    }
    
    

    The shortcode updating the form with ACF data updating the WP user data

    
    
    function my_acf_save_post( $post_id ) {
    		if( empty($_POST['acf']) ) {
    			return;
    		}
    		
    		if( is_admin() ) {
    			return;
    		}
    		
    		//get current email before change
    		$current_email = get_the_author_meta('user_email', $wp_user_id);
    		
    		if( $_POST['post_id'] != 'new' ) {
    			
    			$emailField = $_POST['acf']['field_5ae7a967009de'];
    			$wp_user_id = str_replace("user_", "", $post_id);
    			
    			if (isset($emailField)) {
    				if (email_exists( $emailField )) {
    					echo "<p>Email already exists</p>";
    					update_field('field_5ae7a967009de', $current_email, $post_id);
    				} else {
    					$args = array(
    						'ID'         => $wp_user_id,
    						'user_email' => esc_attr( $emailField )
    					);            
    					wp_update_user( $args );
    				}  
    			}
    			$first_name = $_POST['acf']['field_5ae7a4b8e5948'];
    			$last_name = $_POST['acf']['field_5ae7a8cf8e3e0'];
    			$user_data = wp_update_user( array( 'ID' => $wp_user_id, 'first_name' => $first_name, 'last_name' => $last_name ) );	
    		}
    		
    		return $post_id;
    }
    
    

    both are called at the end of course:

    – – – – – – – – – – – – – – – – – – – – – – –

    
    
    add_action('acf/save_post', 'my_acf_save_post', 1);
    
    add_shortcode('userupdate', 'zUpdate');
    
    
  • The filter to use is acf/validate_value https://www.advancedcustomfields.com/resources/acf-validate_value/. In your filter test to see if the value for the email already exists and set the error if it does.

  • Here is what I came up with:

    add_filter('acf/validate_value/name=my_user_email', 'my_acf_validate_value', 10, 4);
    
    function my_acf_validate_value( $valid, $value, $field, $input ){
    	
    	if($value == "" || $value == " ") {
    		$valid = "Email field cannot be empty";
    	} elseif( email_exists($value) ) {
    		$valid = "This email already exists. Use a unique email";
    	}
    	return $valid;
    	
    }

    If a users email is info@website.com and they try another email that already exists or nothing then they get the respective errors. So far so good. If they try to change their email back to info@website.com they can’t because of the email_exists($value). Even though that is the what we want because they’re actual email is info@website.com. That was never changed. Can we do a check as follows

    If email exists then show error “This email exists already” unless the user’s actual email (wordpress user email) is the ACF field value being entered. Then that is OK and that is the exception.

    I can see users trying to change their email to something else. seeing the error. then saying “I’m going to change it back to my old email”. and if they do it exists because the WordPress email was never changed.

    So close

    If user

  • I figured it out:

    function my_acf_validate_value( $valid, $value, $field, $input ) {
    	
    	$current_id = get_current_user_id();
    	$wp_user_id = 'user_' . $current_id;
    	$current_email = get_the_author_meta('user_email', $current_id);
    			
    	if($value == "" || $value == " ") {
    		$valid = "Email field cannot be empty";
    	} elseif( email_exists($value) && $value != $current_email ) {
    		$valid = "This email already exists. Use a unique email";
    	}
    	return $valid;
    	
    }

    I figured this out. I’d like this validation to only be on the front end of the site not in the admin area. I’ll start another forum thread for that.

  • You need to check that it’ not the admin

    
    function my_acf_validate_value( $valid, $value, $field, $input ) {
    
    // add to top of funciton
    if (is_admin()) {
      return $valid;
    }
    
  • I’m doing this one instead. I added

    function my_acf_validate_value( $valid, $value, $field, $input ) {
    	
    	$current_id = get_current_user_id();
    	$wp_user_id = 'user_' . $current_id;
    	$current_email = get_the_author_meta('user_email', $current_id);
    	
    	if( $_POST['acf']['register'] == 'true' ) {
    		if($value == "" || $value == " ") {
    			$valid = "Email field cannot be empty";
    		} elseif( email_exists($value) && $value != $current_email ) {
    			$valid = "This email already exists. Use a unique email";
    		}
    	}
    	return $valid;
    	
    }

    in the form that was generated I added a hidden field for register

    	$options = array(
    	    'post_id' => $user_id,
    	    /* my field group grabs unit, home phone, and cell phone acf */
    	    'field_groups' => array('3307', ),
    	    'submit_value' => 'Update',
    	    'html_after_fields' => '<input type="hidden" name="acf[register]" value="true"/>',
    	);
    	acf_form( $options );
Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.