Support

Account

Home Forums Front-end Issues ACF_form User Profile editing

Solved

ACF_form User Profile editing

  • Hello everyone

    I’m just asking some advice, to check if I’m missing something obvious.

    I’m using ACF_form to create a nice front-end user profile editing page. I have it all working quite nicely, but one thing got me stumped. I wanted the user to also be able to modify their email address – which is obviously part of the author meta.

    To allow users to edit their profile description, I was able to simply name my ACF field ‘description’ and it automatically synced up with the standard WordPress user description meta. But the same doesn’t seem to work for user_email.

    I have got a solution working by creating a different email field, and then using the save action hook to grab that email and update the user_email via the functions.php file, but it seems like a bit of a hack.

    I just wondered if I’m missing a simple way to integrate an editable user_email field in an ACF_form?

    Thanks guys!

    Jon

  • Hi @raffacake

    It’s because the description is located in wp_usermeta table (where ACF saves the custom fields for users) while the email address is located in the wp_users table. I believe what you have done is the right method for something like that.

    Hope this helps 🙂

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

    Jon

  • Hi @raffacake

    I’m trying to achieve the same thing. I was wondering if you’d mind sharing the code you used in the functions.php to save the email address?

    Many thanks

    Leanda

  • 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 @raffacake that works brilliantly!

  • Me again! So the above code worked beautifully for the email address, thanks @raffacake but I thought I might be able to do the same thing for the user_url however I don’t seem to be able to get it working.

    Any ideas where I might be going wrong?

    Thanks

    function my_acf_save_post_website( $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' ) {
    
    	$urlField = $_POST['acf']['profile_website'];
    			$wp_user_id = str_replace("user_", "", $post_id);
    
    			if (isset($urlField)) {
    				if (url_exists( $urlField )){
    					// Website exists, do not update value.
    					// Maybe output a warning.
    					update_field('profile_website', get_the_author_meta('user_url',$wp_user_id), $post_id);
    				} else {
    					$args = array(
    						'ID'         => $wp_user_id,
    						'user_url' => esc_attr( $urlField )
    					);
    					wp_update_user( $args );
    				}
    			}
    		}
    
    		// return the ID
    		return $post_id;
    	}
    	add_action('acf/save_post', 'my_acf_save_website', 20);
    
  • Hi @leanda

    That’s because you were trying to get the posted URL using the field name instead of the field key:

    $urlField = $_POST['acf']['profile_website'];

    If you need to get the posted value, you can do it like this for the unvalidated value:

    $urlField = $_POST['acf']['field_1234567890abc'];

    Or like this for the validated value:

    $urlField = get_field('profile_website', $post_id);

    Where ‘field_1234567890abc’ is the field key. Please keep in mind that the get_field() function will only work if you use it in the acf/save_post hook with priority more than 10.

    I hope this helps 🙂

  • 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 guys thanks for replying.

    Sadly I still can’t figure this one out 🙁 I’ve switched it to use the field key and changed the priority to 10 and now when I submit the form I get a white screen (500). I’ve even tried setting the field type from text to url.

    It’s got me stumped this one because is works so well for the email field.

    Here’s the updated code:

    function my_acf_save_post_url( $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' ) {
    			
    
    			$urlField = $_POST['acf']['field_57861544dfb0f'];
    			$wp_user_id = str_replace("user_", "", $post_id);
    
    			if (isset($urlField)) {
    				if (url_exists( $urlField )){
    					// Email exists, do not update value.
    					// Maybe output a warning.
    					update_field('field_57861544dfb0f', get_the_author_meta('user_url',$wp_user_id), $post_id);
    				} else {
    					$args = array(
    						'ID'         => $wp_user_id,
    						'user_url' => esc_attr( $urlField )
    					);
    					wp_update_user( $args );
    				}
    			}
    
    			}
    
    		// return the ID
    		return $post_id;
    	}
    	add_action('acf/save_post', 'my_acf_save_post_url', 10);
    

    Could I be missing something in my form options:

    
     $options = array(
     'post_id' => 'user_'.$current_user->ID, // $user_profile,
     'field_groups' => array(225),
     'submit_value' => 'Update Profile',
     'uploader' => 'basic'
     );
    

    Thanks for your help, very much appreciated.

  • Hi @raffacake

    No problem. I’m happy to help you guys. If you have something to add, just let us know 🙂


    @leanda

    Could you please check the error log on your server? Usually, the error log will tell you more details about the 500 error message.

    It’s possible that $_POST['acf']['field_57861544dfb0f'] is not available, but you called it before you check it. In this case, you need to use this code:

    if( isset($_POST['acf']['field_57861544dfb0f']) ) {
        $urlField = $_POST['acf']['field_57861544dfb0f'];
    } else {
        $urlField = false;
    }

    Or maybe there’s something wrong with your url_exists() function or how you update the field.

    Please keep in mind that ACF saves the data with the priority of 10. I suggest you change the priority to 1 or 20, depending on when you want the code to be executed. Please check this page to learn more about it: https://www.advancedcustomfields.com/resources/acfsave_post/.

    Thanks 🙂

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

The topic ‘ACF_form User Profile editing’ is closed to new replies.