重力形式:动态填充字段标签或占位符

时间:2020-04-09 12:00:41

标签: php wordpress gravity-forms-plugin

我想用当前用户的数据填充输入字段。 目前,我填充fields值参数,但我想保持输入为空。 因此,如果可以填充输入字段的标签或占位符参数,那就太好了。

我在文档中找不到任何内容。

这是我当前的代码:

add_filter('gform_field_value_username', create_function("", '$value = populate_usermeta(\'nickname\'); return $value;' ));

2 个答案:

答案 0 :(得分:2)

我相信这应该与Gravity Forms配合使用:

Screenshot of field settings with the user merge tag in the placeholder setting

答案 1 :(得分:1)

在Gravity Forms支持的帮助下以及一些自己的测试下,我想出了一个解决方案:

function populate_usermeta($meta_key){
    global $current_user;
    return $current_user->__get($meta_key);
}


add_filter( 'gform_pre_render', 'populate_text' );

//Note: when changing choice values, we also need to use the gform_pre_validation so that the new values are available when validating the field.
add_filter( 'gform_pre_validation', 'populate_text' );

//Note: this will allow for the labels to be used during the submission process in case values are enabled
add_filter( 'gform_pre_submission_filter', 'populate_text' );

function populate_text( $form ) {

    $items = array();

    $fields = $form['fields'];
    foreach( $form['fields'] as &$field ) {

        if ( $field->type != 'text' || strpos( $field->cssClass, 'populate-placeholder' ) === false ) {
            continue;
        }

        $field->placeholder = populate_usermeta($field->inputName);

    }

    return $form;
}
相关问题