• Resolved fuchsws

    (@fuchsws)


    I would like to define “placeholders” in the default_value field which dynamically populate field values.

    So for example a field “company name” should be filled in with the user meta “company_name”. For this I could think about using the default_value for this field like “%%usermeta:company_name%%”.

    A custom functions then hooks into “acf/render_field” and replaces the “$field[‘value’]” with “get_user_meta(get_current_user_id(), ‘company_name’, true)” and clears the default_value.

    But, this is not working. The input value just stays empty.
    I’ve increased the priority of the action to max but still empty. It seems this hook doesn’t apply with acf_get_fields() your are using.

    hmmm, what am I missing here?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter fuchsws

    (@fuchsws)

    figured it out (I used the wrong hook):

    function my_acf_load_field( $field )
    {
        if ( strpos($field['default_value'], '%%') === 0 )
        {
            $default_values = explode(':', str_replace('%%', '', $field['default_value']));
            if ( $default_values[0] == 'usermeta' )
                $field['value'] = get_user_meta(get_current_user_id(), $default_values[1], true);
        }
        return $field;
    }
    add_filter('acf/load_field', 'my_acf_load_field');
    • This reply was modified 8 years, 1 month ago by fuchsws.
    Plugin Author fabianlindfors

    (@fabianlindfors)

    Great that you managed to work it out! I’m sorry for the late reply.

    If you have any other questions or problems in the future I would love to help. Just create a support thread. ??

    Good luck!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Prepopulate fields’ is closed to new replies.