I need to add a unique string to a form as an identifier for customers. I have created some functions that will output a string of characters. I want to populate a form field with that using a shortcode. Not having much luck.
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');
And from my hidden input field (within Gravity Forms config tool) I am using [ABC_form_number length="13" char="num" case="false"]
. Not seeing any data in the invoicenum field.
EDIT: Updated the code sample. I was able to apply the WP shortcode and output a string from my plugin to a post. Trying to use the approach to populate a field in a Gravity Forms form has not worked. Here's a link to some documentation at Gravity Forms for something that looks related. No luck applying this for a fix. https://docs.gravityforms.com/gform_shortcode_form_property/