Support

Account

Home Forums ACF PRO Acf_form simultaneous tasks Reply To: Acf_form simultaneous tasks

  • This is the form I’m using:

                @php acf_form_head(); @endphp
    
                @php
                  acf_form( array(
                    'id'            => 'register_new_speaker',
                    'fields' => array('field_5b4c1662fde34','field_5b4c1931aa152')
                  ));
                @endphp
    

    This is the function I’m using to create a user:

    function generate_new_user_id( $post_id, $form ) {
    
        // Check that we are targeting the right form. I do this by checking the acf_form ID.
        if ( ! isset( $form['id'] ) || 'register_new_speaker' != $form['id'] ) {
            return $post_id;
        }
    
        // Create an empty array to add user field data into
        $user_fields = array();
    
        // Check for the fields we need in our postdata, and add them to the $user_fields array if they exist
        if ( isset( $_POST['acf']['field_5b4c1662fde34'] ) ) {
            $user_fields['first_name'] = sanitize_text_field( $_POST['acf']['field_5b4c1662fde34'] );
        }
    
        if ( isset( $_POST['acf']['field_5b4c1662fde34'] ) ) {
            $user_fields['last_name'] = sanitize_text_field( $_POST['acf']['field_5b4c1662fde34'] );
        }
    
        if ( isset( $_POST['acf']['field_5b4c1662fde34'] ) ) {
            $user_fields['user_login'] = sanitize_user( $_POST['acf']['field_5b4c1662fde34'] );
        }
    
        if ( isset( $_POST['acf']['field_5b4c1931aa152'] ) ) {
            $user_fields['user_email'] = sanitize_email( $_POST['acf']['field_5b4c1931aa152'] );
        }
    
        if ( isset( $_POST['acf']['field_5b4c1931aa152'] ) ) {
            $user_fields['user_pass'] = $_POST['acf']['field_5b4c1931aa152'];
        }
    
        if ( isset( $_POST['acf']['field_5a83681ec4d01'], $_POST['acf']['field_5b4c1931aa152'] ) ) {
            $user_fields['display_name'] = sanitize_text_field( $_POST['acf']['field_5b4c1931aa152'] . ' ' . $_POST['acf']['field_5a83681ec4d15'] );
        }
    
        $user_fields['role'] = 'ilustrador';
    
        $user_id = wp_insert_user( $user_fields );
    
        if ( is_wp_error( $user_id ) ) {
            wp_die( $user_id->get_error_message() );
        } else {
            // Set the 'post_id' to the newly created user_id, including the 'user_' ACF uses to target a user
            return 'user_' . $user_id;
        }
    }
    add_action( 'acf/pre_save_post', __NAMESPACE__ .'\generate_new_user_id', 10, 2 );
    

    This is the function I’m using to set the term on the custom post type that has the acf_form:

    
    function my_pre_save_post($post_id) {
    
        // check for numerical post_id and check post_type
        if (!is_numeric($post_id) || get_post_type($post_id) != 'post') {
            return $post_id;
        }
    
        // update the post
        wp_set_object_terms($post_id, array(29), 'estado_hist_ext', true);
    
        if ( is_wp_error( $post_id ) ) {
            // If adding this user failed, deliver an error message. I also do custom JS field validaiton before submit to check for proper email addresses, and to check for duplicate emails/existing usernames. But that code is beyond the scope of this thread
            wp_die( $post_id->get_error_message() );
        } else {
            // Set the 'post_id' to the newly created user_id, including the 'user_' ACF uses to target a user
            return $post_id;
        }
    }
    
    add_filter('acf/pre_save_post' , __NAMESPACE__ .'\my_pre_save_post', 10);
    

    I’ve found both functions seem to work if I comment out the other but when running both, only the user gets created, the one updating the term fails to run. My bet is the $post_id value is being set by the first one when creating the user and thus the next function has a wrong $post_id value (more like $user_id at that point) and it does not run.

    I’ve tried changing the priority on both as well as hooking on pre_save_post and save_post with no avail.

    I’m happy to provide further details if needed and hoping I’ve explained myself well enough for someone to kindly point my mistakes.