Support

Account

Home Forums General Issues Pre-populate form with user fields in functions.php

Solved

Pre-populate form with user fields in functions.php

  • Hello,

    I’am trying to pre-populate a front-end ACF form field with current user custom field.

    add_filter('acf/load_value/name=USER_FIELD', 'load_fill_user_field', 20, 5);
    function load_fill_user_field($value, $post_id, $field) {
      // check for empty value in order to populate
      if ($value == NULL) {
        $curent_user = wp_get_current_user(); // Get current user
        $curent_user_id = $curent_user->ID; // Get current user ID
    
        if (function_exists('get_field')) {
            // Get user's USER_FIELD
            $value = get_field('USER_FIELD', 'user_'. $curent_user_id, false);
        }
        return $value;
      }
      return $value;
    }

    This code result in a 503 error on page..

  • You are creating an infinite loop by calling get field on the same field that you are trying to preload. If this is really necessary then you must remove your filter before calling get_field() and add it again after the call.

  • Thank you! I was doing a big stupidity and I didn’t notice it.

    Actually, I wanted to get a USER_ACCOUNT_FIELD while USER_FIELD was loading.

  • Heya @rtm would you mind sharing the filter / function that finally worked for you on this? I’m trying to do the same thing but somehow am running into a wall. Or I’ve been up too long! Thanks.

    In case it’s helpful, to be specific, I’m using acf_form() to allow people to fill in a frontend ACF form, and I want to log the user / id / name of the person who filled in the form. Can be a hidden function or a pre-filled form field that the person filling in the form can’t edit. So they can see that their name is logged or we can just do it in the background, I don’t care. When the acf_form() is saved, I’d like to write their user / id / name to an ACF field along with the rest of their form entry so we have a record of who filled it out.

    Thanks!

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

The topic ‘Pre-populate form with user fields in functions.php’ is closed to new replies.