Support

Account

Home Forums General Issues User profile form: Update password?

Solved

User profile form: Update password?

  • I have a form that lets users update an ‘advanced profile’.

    Is there a way to let the user update their password too?

  • You would need to add fields to your form for them to update the password.

    You’d also need to do any validation of these fields yourself, you can use the acf/validate_value hook for this, but if you supply two fields I’m not sure at the moment how you’d compare two fields.

    Then you’d need to create an acf/pre_save_post filter http://www.advancedcustomfields.com/resources/acf-pre_save_post/ that would call the WP function wp_set_password() https://codex.wordpress.org/Function_Reference/wp_set_password

  • Thanks John

    So in my case, it’d be

    my_pre_save_post('user_5') // 5 being the ID of the user

    and then whatever the user typed in would be set as their password with wp_set_password('pword', 5)

    Sound about right?

  • I think something like that

    
    add_filter('acf/pre_save_post', 'my_update_password');
    function my_update_password($post_id) {
      if (substr($post_id, 0 , 5) != 'user_') {
        // not a user
        return $post_id;
      }
      // get value from input and update password
    }
    
  • Sorry for the delay in getting back to this…

    One question I have, perhaps because I’m not understanding _quite_ how this works, is how to I actually fetch the saved value of that custom field.

    IE I have a profile form where the user will type in a password.

    Then I have the above function you suggested in my functions.php and I’m using a wp_mail() function to test what its doing/returning…

    wp_mail( '[email protected]', 'test', $post_id.' / '.get_field('user_password', $post_id));

    IE this is where I’d have the wp_set_password() function.

    What I’m getting from the above is

    user_1 /

    get_field('user_password', $post_id) isn’t returning anything.

    So how do I actually get the value of that field?

  • You’re saving the value before ACF has saved the value. You need to look in $_POST['acf'] for the value.

    For testing I would suggest that you add this to your pre_save_post function to get an idea of what you need to do. This is what I generally do anyway.

    echo '<pre>'; print_r($_POST); die; // exit so you can see what is submitted

  • Aaaah, you biscuit 🙂

    Sorted, thanks very much! Had to add a couple lines to update the user’s cookie so the password change doesn’t log them out immediately.

    Here’s the final function that’s working for me:

    add_filter('acf/pre_save_post', 'my_update_password');
    function my_update_password($post_id) {
    	if (substr($post_id, 0 , 5) != 'user_') {
    		// not a user
    		return $post_id;
    	}
    
    	$new_password = $_POST['acf']['field_55c0cfff1d979']; // update this
    	$who = str_replace('user_', '', $post_id);
    
    	wp_set_password( $new_password, $who );
    	wp_clear_auth_cookie();
    	wp_set_current_user ( $who );
    	wp_set_auth_cookie  ( $who );
    }
  • I have my hook and it works fine (updates others fields of user such as display_name and pass) but now it is not updating my acf fields, am I missing something here? does this hook has to return a value or something?

  • Got it! Had to return the post_id! Sorry I’m a WP and ACF newbie 🙂

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

The topic ‘User profile form: Update password?’ is closed to new replies.