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!
Thread Starter
zvdh
(@zvdh)
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 );
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 🙂
Thread Starter
zvdh
(@zvdh)
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.
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?