最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

populating a form field with output from a custom plugin function

matteradmin9PV0评论

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. /

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/gform_shortcode_form_property/

Share Improve this question edited Dec 31, 2018 at 6:51 b_dubb asked Dec 28, 2018 at 6:00 b_dubbb_dubb 516 bronze badges 2
  • 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 Commented Dec 31, 2018 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/gform_shortcode_form_property – b_dubb Commented Dec 31, 2018 at 6:51
Add a comment  | 

1 Answer 1

Reset to default 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)

Post a comment

comment list (0)

  1. No comments so far