我需要在表单中添加一个唯一的字符串作为客户的标识符。我创建了一些输出字符串的函数。我想用一个短代码填充一个表单字段。运气不太好。
function ABC_validUniqueStr($attr){
$length = $attr[\'length\']; // num (int)
$char = $attr[\'char\']; // num, alpha, alphanum
$case = $attr[\'case\']; // $case, true vary case, false upper case
$validStr = \'\';
do {
// $string = ABC_form_number_alpha_numeric($length,$char,$case);
$string = ABC_form_number_alpha_numeric($length=13,$char=\'num\',$case=false);
$result = ABC_test_unique_form_number($string);
if( $result[0]->count == \'0\' ) {
$validStr = $string;
}
} while( $validStr == \'\' );
return $validStr;
}
function ABC_form_number_alpha_numeric($length=13,$char=\'num\',$case=false) {
$formNumberAlphaNumericStr = \'\';
for( $i = 0; $i < $length; $i++ ){
if( $char == \'num\' ) {
$formNumberAlphaNumericStr .= rand(0,9);
} elseif ( $char == \'alpha\' ) {
$num = rand(0,25);
$formNumberAlphaNumericStr .= ABC_retunAlpha($num,$case);
} elseif ( $char == \'alphanum\' ) {
$rando = rand(0,1);
if( $rando == 0 ) {
$formNumberAlphaNumericStr .= rand(0,9);
} else {
$num = rand(0,25);
$formNumberAlphaNumericStr .= ABC_retunAlpha($num,$case);
}
} else {
$formNumberAlphaNumericStr = \'error\';
}
}
// var_dump($formNumberAlphaNumericStr);
return $formNumberAlphaNumericStr;
}
function ABC_test_unique_form_number($str){
global $wpdb;
$sqlStr = "select count(*) as count from ".$wpdb->prefix."ABC_app_invoicenumber ";
$sqlStr .= "where invoicenum = \'%s\'";
$prepSql = $wpdb->prepare($sqlStr,$str);
return $wpdb->get_results($prepSql);
}
function ABC_retunAlpha($num,$case){
if( $num >= 0 && $num < 26 ) {
$upperAlphaArr = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N",
"O","P","Q","R","S","T","U","V","W",\'X\',"Y","Z");
if( $case == 0 ) {
return $upperAlphaArr[$num];
} else {
return strtolower($upperAlphaArr[$num]);
}
} else {
return array(\'error\'=>\'num out of range\');
}
}
add_shortcode(\'ABC_form_number\',\'ABC_validUniqueStr\');
add_filter( \'gform_shortcode_ABC_form_number\', \'ABC_validUniqueStr\');
从我的隐藏输入字段(在Gravity Forms config tool中),我正在使用
[ABC_form_number length="13" char="num" case="false"]
. 在invoicenum字段中看不到任何数据。
编辑:更新了代码示例。我能够应用WP短代码,并将插件中的字符串输出到帖子中。尝试使用这种方法在重力表单中填充字段是行不通的。这里有一个指向Gravity表单中一些文档的链接,这些文档看起来是相关的。无法将此应用于修复。https://docs.gravityforms.com/gform_shortcode_form_property/