Support » Plugin: Advanced Forms » Pre-fill field with username

  • Resolved zvdh

    (@zvdh)


    Hiya,

    I’m using Advanced Forms and ACF Pro to build a registration tool for my colleagues. It would be a real time-saver if I could pre-fill a certain field with the name of the current user in Advanced Forms.

    Is this in some way possible?

    I’ve been able to get it working in the dashboard (using a ‘user’-field which loads the current user, code below) but this breaks the Advanced Form in the front-end.

    function my_prepare_field( $field ) {
    $user = wp_get_current_user();
    $user = $user -> ID;
    
    $field ['value']= $user; //set user to current user
    return $field;
    }
    
    add_filter('acf/prepare_field/name=user', 'my_prepare_field');

    The page I need help with: [log in to see the link]

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

    (@fabianlindfors)

    Hi!

    There exist a filter you can use a to pre-fill fields. It’s called “af/field/prefill_value” and you can find some documentation here: https://advancedforms.github.io/filters/af-field-prefill_value/

    Hope this helps!

    Hi Fabian!

    Thank you for the reply! I managed to get it to work, but I still have some feedback for you.

    • Slight error in the code: it should be function prefill_form_field (...) instead of function filter_args(...) 🙂
    • The field ‘user’ doesn’t seem to work that good in Advanced Forms. I can’t see any user. It is however possible to prefill the field with the current user, so that’s all good!

    The code I used, should someone else find it usefull:

    function prefill_form_field( $value, $field, $form, $args ) {
    	$user = wp_get_current_user();
    	$user = $user -> ID;
    	$field ['value']= $user; //set user to current user
    	return $field;
    }
    add_filter( 'af/field/prefill_value', 'prefill_form_field', 10, 4 );
    add_filter( 'af/field/prefill_value/name=user', 'prefill_form_field', 10, 4 );
    add_filter( 'af/field/prefill_value/key=field_5d0fd06363803', 'prefill_form_field', 10, 4 );
    Plugin Author fabianlindfors

    (@fabianlindfors)

    Thanks for the heads up on the incorrect function name, fixed now!

    Could you elaborate more on the issues you are running into with the user field? It should work 🙂

    Hi

    Sorry for the late reply. The issue I have with the user field is that you can’t select any of the users. With the function you provided it’s possible to pre-fill the field, but I can’t select any other users (they just don’t show up).

    I haven’t dug any further to a possible solution or problem in the configuration, since it’s not necessary for us: this is the exact functionality we needed.

    Plugin Author fabianlindfors

    (@fabianlindfors)

    I’m glad it works but I’d be happy to help you with the user field problem if you’d like. Normally this is caused by a Javascript error on the page which blocks other Javascript from executing, such as the fetching of users for the field.

    Could you maybe check you browser console to see if any Javascript errors appear?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Pre-fill field with username’ is closed to new replies.