Support

Account

Home Forums General Issues Update_field not working for front end user creation

Solved

Update_field not working for front end user creation

  • I have a front end form for a custom post type to create users from the frontend. I am trying to update a custom field ‘incidents’ from the post to the user on user creation from the frontend. Unfortunately no matter what I do it just won’t update the post object from the custom post.

    I have tried using the field name, the field key as well as changing the output for both fields between Post Object and Post ID.

    Am I missing something here?

    function my_acf_save_multiple_posts( $post_id ) {
        
    
        $first_name = get_field('first_name', $post_id);
        $last_name = get_field('last_name', $post_id);
    	$username = get_field('email', $post_id);
    	$password = get_field('password', $post_id);
    	$incident = get_field('related_incident', $post_id);
    	$role = get_field ('role', $post_id);
    	
        
    	$userdata= array (
    		'user_login' => $username,
    		'user_email' => $username,
    		'first_name' => $first_name,
    		'last_name' => $last_name,
    		'user_pass' => $password,
    		'role' => $role,
    	);
        
        wp_insert_user( $userdata );
    
    	update_field('field_602ce62b11720', $incident, $post_id);
    }
    
    add_action('acf/save_post', 'my_acf_save_multiple_posts', 20);
  • 
    // ensure you get the ID value here
    $incident = get_field('related_incident', $post_id, false);
    
    ...
    
    $user_id = wp_insert_user($userdata);
    update_field('field_602ce62b11720', $incident, 'user_'.$user_id);
    
  • Thanks @hube2 it works! Not sure I understand why but it does 🙂

  • You were updating a value for the user, not the post. You need to set the ID for ACF the same way you would to get values from the user https://www.advancedcustomfields.com/resources/how-to-get-values-from-a-user/

  • Ahhh makes sense thank you! So I’m setting the ID for a user not a post.

  • @hube2 I am now getting an issue if I try to update an existing user post. Do I need to add some code for update as this update_field seems to be causing an issue for updates?

    Uncaught Error: Object of class WP_Error could not be converted to string in

  • You should be testing at the start of your function that it’s only running for your post type

    
    if (get_post_type($post_id) != 'your post type') {
      return;
    }
    
  • @hube2 I don’t think that is the issue as this error occurs when I update the custom post. I understand why there may be an error if I try to edit from the core WordPress user tab.

    I actually tried to add this if but it actually caused the whole function to not run at all.

    function create_user_frontend( $post_id ) {
    
    	if (get_post_type($post_id) != 'users') {
    
        $first_name = get_field('first_name', $post_id);
        $last_name = get_field('last_name', $post_id);
    	$username = get_field('email', $post_id);
    	$password = get_field('password', $post_id);
    	$incident = get_field('related_incident', $post_id, false);
    	$role = get_field ('role', $post_id);
        
    	$userdata= array (
    		'user_login' => $username,
    		'user_email' => $username,
    		'first_name' => $first_name,
    		'last_name' => $last_name,
    		'user_pass' => $password,
    		'role' => $role,
    	);
    
    	$user_id = wp_insert_user($userdata);
    	update_field('field_602ce62b11720', $incident, 'user_'.$user_id);
    	
    	}
    
    }
    
    add_action('acf/save_post', 'create_user_frontend', 20);
  • @hube2 I now have it working with it checking the post type before running the function and it still has this unable to convert to string error when I update the custom post from both WP admin and the frontend. I think the probably is related to the 'user_'.$user_id);

  • @hube2 I’ve narrowed it down to the fact that on update/edit, it is expecting a number as opposed to the ‘user_number’ string. If I hard code the id ’60’ and then update that user it works.

    Do you know how I can differentiate between user creation and user update so I can pass just the user id for updating and use the ‘user_’. when creating a user?

  • I am now confused what it is that you’re doing. You are using $user_id = wp_insert_user($userdata); so I assume that you are adding a user and not a post in your “users” CPT.

  • @hube2 yes correct and it works for creating a user. But when I try to make a change to a user this function runs and causes an error because the user id already exists. So I suspect it is outputting something other than just user Id which is causing the above string error.

    Basically updating the custom post type users results in the error due to line

    update_field('field_602ce62b11720', $incident, 'user_'.$user_id)

  • @hube2 when I put the user Id directly e.g. 16 it updates the user fine.

    I.e.

    ‘16’

    Instead of

    ‘user_’.$user_id)

  • @hube2 when it is a case of updating an existing user I just need to be able to get the id of that user rather than using the wp_insert_user like I do for creating the user.

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

You must be logged in to reply to this topic.