• Resolved jeneara

    (@jeneara)


    Hello,

    I’m trying to create a form that when it creates a term it also assigns the current user to that term.

    I want to do this so that only the assigned user and the admins can then edit it in future.

    Right now I have the form setting up the term correctly but I can’t seem to assign the user.

    Current field set up

    Hook

    add_action('acfe/form/submit/term/form=developer-submission-form-testing', 'my_form_term_save', 10, 5);
    function my_form_term_save($term_id, $type, $args, $form, $action){
    
        // Retrieve the current user ID
        $current_user_id = get_current_user_id();
    
        $user = get_field('field_60f4ec940975e');
    
         // Update 'user' on the newly created term
         update_field($user, $current_user_id, $term_id);
    
        }

    Form Link

    It’s quite possible that I’m simply not calling the hook properly as I am new to web dev.

    Any help around how I can get this to work would be greatly appreciated.

    Thank you

Viewing 1 replies (of 1 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback!

    It looks like the problem come from the fact that you wrongly use the update_field() function. As explained in the ACF documentation, the first parameter of the update_field() has to be the targeted field name or key. In your example you’re using the value of an another field, which seems weird.

    Here is a working usage example:

    update_field('my_field', 'my_value', 52);
    

    Here is an example which should work in your case:

    add_action('acfe/form/submit/term/form=developer-submission-form-testing', 'my_form_term_save', 10, 5);
    function my_form_term_save($term_id, $type, $args, $form, $action){
    
        // Retrieve the current user ID
        $current_user_id = get_current_user_id();
        
        // Convert Term ID to use ACF Term ID format: 'term_4' instead of '4'
        // See: https://www.advancedcustomfields.com/resources/update_field/
        // And: https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/
        $acf_term_id = 'term_' . $term_id;
    
        // Update 'my_user_field' with the current user ID on the newly created term
        update_field('my_user_field', $current_user_id, $acf_term_id);
    
    }
    

    Please note that the Term ID that is passed to the update_field() function has to be formatted as term_4 instead of 4, so ACF correctly understands it has to update the a Term, and not a Post. You’ll find more info about that logic here:

    Hope it helps!

    Have a nice day!

    Regards.

Viewing 1 replies (of 1 total)
  • The topic ‘Assign Current User to the created term’ is closed to new replies.