Support » Plugin: Advanced Custom Fields: Extended » How to preload a field with user data

  • Resolved mirefoot

    (@mirefoot)


    In my Field Group I have a field (Rider) which is a user field so I can store the user in my custom fields for my CPT (Score).

    When a competitor (Rider) wants to enter a competition I want to save a new Score (custom post) with their user details. (They have to be logged in to do this)

    How do I preload the field in question with the User details when the form is created on the frontend by a shortcode?

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

    (@hwk-fr)

    Hello,

    Thanks for the feedback!

    You can prefill any field value with a custom data using the native acf/load_value filter. It will work both for the front-end and back-end forms.

    An another solution would be to simply hide the field from the front-end (if it is not a field that should be changed by the user), and programmatically update the field during the Post Action submission, using the acfe/form/submit/post hook with update_field(). See documentation.

    Usage example:

    add_action('acfe/form/submit/post/form=my-form', 'my_form_submit', 10, 5);
    function my_form_submit($post_id, $type, $args, $form, $action){
        
        // Retrieve the current user ID
        $current_user_id = get_current_user_id();
        
        // Update 'my_user_field' on the newly created/updated post
        update_field('my_user_field', $current_user_id, $post_id);
        
    }
    

    Hope it helps!

    Have a nice day!

    Regards.

    Thread Starter mirefoot

    (@mirefoot)

    Can you tell me where this code goes. Does it go into my functions file or into a code box in the forms UI?

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    This code is a PHP code that has to be included in the functions.php file of the theme. You can read more about Theme Functions here and PHP hooks here.

    Have a nice day!

    Regards.

    Thread Starter mirefoot

    (@mirefoot)

    Have worked out this goes into my functions file, thank you.

    • This reply was modified 1 month, 3 weeks ago by mirefoot.
    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    I’m glad to hear it! You’re welcome 🙂

    Have a nice day.

    Regards.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.