0

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/

  • have you done any debugging to determine where the point of failure is? does the shortcode work in other contexts, like a page or post? – Milo Dec 31 '18 at 1:55
  • 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. docs.gravityforms.com/gform_shortcode_form_property – b_dubb Dec 31 '18 at 6:51
2
+50

You can pre-populate a field with gform_field_value_$parameter_name hook, where $parameter_name is the name of the field/parameter. So, for example if you added that to your code:

add_filter('gform_field_value_invoice_numb', 'my_func');
function my_func( $value ) {
    return do_shortcode('[ABC_form_number length="13" char="num" case="false"]');
}

(Note: Allow field to be populated dynamically option needs to be enabled)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.