最合适的回答,由SO网友:Frank P. Walentynowicz 整理而成
将此代码放入主题函数中。php:(适用于Ninja Forms 2.9.x)
function unique_code_submission( $data, $field_id ) {
global $uniqueCode;
if ( "string" !== gettype( $uniqueCode ) )
$uniqueCode = uniqid();
/* here goes your code to populate a field */
return $data;
}
add_filter( \'ninja_forms_field\', \'unique_code_submission\', 10, 2 );
在上述函数中,您将在
$uniqueCode
全局变量。当过滤器第一次触发时,此代码将只生成一次。有关的更多信息
ninja_forms_field
过滤器:
here.
UPDATE: 对于Ninja Forms 3.0及以上版本:
function unique_code_submission( $fields ) {
$uniqueCode = uniqid();
/* here goes your code to populate a field. For example:
let\'s populate hidden field, which has key value of
\'hidden_1492812363939\' */
$index = 0;
while ( 0 <= $index ) {
++$index;
if ( \'hidden_1492812363939\' == $fields[ $index-1 ][ \'key\' ] ) {
$fields[ $index-1 ][ \'value\' ] = $uniqueCode;
$index = -1;
}
}
return $fields;
}
add_filter( \'ninja_forms_display_fields\', \'unique_code_submission\', 10, 1 );