menu

Questions & Answers

Gravity Forms should pull some data from mysql

I am trying to make some sort of password recovery function (don't ask why).

I have a WordPress site and use Gravity Forms to ask for the client's mail address. Then I want to use that address to find the corresponding woo-commerce product, that has the same email address added as a custom attribute (pa_e-mail). Then I want to use another attribute, that is holding a password (no need for super strong security, md5, hashes, etc) as a custom attribute (pa_pw) and send that password back to Gravity Forms to send it via mail to the user. Also, I want to send the link to that product, using the permalink.

The code I have so far is inside the functions.php and in Gravity Forms, I have two textfields that can receive dynamic fill. (edit_pw & edit_link)

function get_pw_by_email( $email ) {
  // Get all products
  $products = wc_get_products( array(
      'limit' => -1,
      'status' => 'publish', 
  ) );

  // Loop through the products
  foreach ( $products as $product ) {
      // Get the value of the "pa_e-mail" attribute
      $pa_email = $product->get_attribute('pa_e-mail');
      // Check if the "pa_e-mail" attribute matches the email
      if ( $pa_email == $email ) {
          // Get the value of the "pa_pw" attribute
          $pa_pw = $product->get_attribute('pa_pw');
          // Return the value of the "pa_pw" attribute
          return $pa_pw;
          // Break the loop
          break;
      }
  }
}

function get_product_permalink_by_email( $email ) {
  // Get all products
  $products = wc_get_products( array(
      'limit' => -1,
      'status' => 'publish', 
  ) );

  // Loop through the products
  foreach ( $products as $product ) {
      // Get the value of the "pa_e-mail" attribute
      $pa_email = $product->get_attribute('pa_e-mail');
      // Check if the "pa_e-mail" attribute matches the email
      if ( $pa_email == $email ) {
          $permalink = $product->get_permalink();
          // Return the permalink
          return $permalink;
          // Break the loop
          break;
      }
  }
}

add_filter( 'gform_field_value_edit_link', 'my_custom_population_function1' );
function my_custom_population_function1($value) {
  if ( rgpost( 'is_submit_6') ) {
    // Form has been submitted, so retrieve the values from the database
    
    // get mail address from gf field
    $email = rgar($entry, '4');
    
    // Permalink
    $link = get_product_permalink_by_email($email);
    
    // Output the value of $link to the PHP error log
    error_log( 'edit_link: ' . $link );
    
    return $link;
  } else {
    // Form has not been submitted, so return an empty value
    return '';
  }
}

add_filter( 'gform_field_value_edit_pw', 'my_custom_population_function2' );
function my_custom_population_function2($value) {
  if ( rgpost( 'is_submit_6') ) {
    // Form has been submitted, so retrieve the values from the database
    
    // get mail address from gf field
    $email = rgar($entry, '4');
    
    // Password
    $password = get_pw_by_email($email);
    
    // Output the value of $password to the PHP error log
    error_log( 'edit_pw: ' . $password );
    
    return $password;
  } else {
    // Form has not been submitted, so return an empty value
    return '';
  }
}

But the mail I receive just has empty values, where I want {Link:6} & {Passwort:7} to appear.

Comments:
2023-01-07 20:29:23
I'm sorry, I know you say it doesn't matter, but "no need for super strong security" plus the word "password" don't go together. You don't care, which is your right, but I (the user of your site) care. The vast majority of users have poor passwords that they use everywhere. By you storing the password insecurely, you've just potentially leaked someone else's bank or email password. Shame on the user for doing that, true. But the fix is trivial on your side. The only exception might be if you are randomly generating the password and not letting users change it.
2023-01-07 20:29:23
Yes, the password is generated and pre-set
Answers(1) :

I have re-written the code.

function get_product_permalink_by_email($email) {

  $products = get_posts(array('post_type' = >'product', 'tax_query' = >array(array('taxonomy' = >'pa_e-mail', 'field' = >'name', 'terms' = >$email))));

  if (empty($products)) {
    return '';
  }

  return get_permalink($products[0] - >ID);
}

function get_pw_by_email($email) {

  $products = get_posts(array('post_type' = >'product', 'tax_query' = >array(array('taxonomy' = >'pa_e-mail', 'field' = >'name', 'terms' = >$email))));

  $terms = wp_get_post_terms($products[0] - >ID, 'pa_pw', true);
  if (empty($terms)) {
    return '';
  }

  return $terms[0] - >name;
}

add_filter('gform_field_value_edit_link', 'my_custom_population_function1');
function my_custom_population_function1($value, $entry) {
  // Form has been submitted, so retrieve the values from the database

  // get mail address from gf field
  $email = rgar($entry, '4');

  // Permalink
  $link = get_product_permalink_by_email($email);

  // Output the value of $link to the PHP error log
  error_log('edit_link: '.$link);

  return $link;
}

add_filter('gform_field_value_edit_pw', 'my_custom_population_function2');
function my_custom_population_function2($value, $entry) {
  // Form has been submitted, so retrieve the values from the database

  // get mail address from gf field
  $email = rgar($entry, '4');

  // Password
  $password = get_pw_by_email($email);

  // Output the value of $password to the PHP error log
  error_log('edit_pw: '.$password);

  return $password;

}

and I am most certainly sure, that its this line that causes the "error":

$email = rgar($entry, '4');

If I enter a mail address manually as $email, then it works as expected. So there must be something about Gravity Forms not passing the value of the e-mail input textfield (ID 4).

Any hint someone?