我想用当前用户的数据填充输入字段。 目前,我填充fields值参数,但我想保持输入为空。 因此,如果可以填充输入字段的标签或占位符参数,那就太好了。
我在文档中找不到任何内容。
这是我当前的代码:
add_filter('gform_field_value_username', create_function("", '$value = populate_usermeta(\'nickname\'); return $value;' ));
答案 0 :(得分:2)
我相信这应该与Gravity Forms配合使用:
答案 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;
}