Support

Account

Forum Replies Created

  • Thanks for jumping in there @acf-support, I had the same inkling but wasn’t certain, so wanted to double-check before posting, but you beat me to it! ๐Ÿ™‚

    Jon

  • Hi @leanda

    I’m far from an expert, so feel free to check this out, but no promises from me that it is robust!

    function my_acf_save_post( $post_id ) {
    		// bail early if no ACF data
    		if( empty($_POST['acf']) ) {
    			return;
    		}
    		
    		// bail early if editing in admin
    		if( is_admin() ) {
    			return;
    		}
    		
    		if( $_POST['post_id'] != 'new' ) {
    			
    			$emailField = $_POST['acf']['field_XXXXXXXXXXXXX'];
    			$wp_user_id = str_replace("user_", "", $post_id);
    			
    			if (isset($emailField)) {
    				if (email_exists( $emailField )){
    					// Email exists, do not update value.
    					// Maybe output a warning.
    					update_field('field_XXXXXXXXXXXXX', get_the_author_meta('user_email',$wp_user_id), $post_id);
    				} else {
    					$args = array(
    						'ID'         => $wp_user_id,
    						'user_email' => esc_attr( $emailField )
    					);            
    					wp_update_user( $args );
    				}  
    			} 	
    		}
    		
    		// return the ID
    		return $post_id;
    	}
    	add_action('acf/save_post', 'my_acf_save_post', 20);

    Hope that’s of some use!

    Jon

  • Thanks James, glad I wasn’t missing something obvious!

    Jon

Viewing 4 posts - 1 through 4 (of 4 total)