搜尋

首頁  >  問答  >  主體

重力形式應該從 mysql 中提取數據

我正在嘗試製作某種密碼恢復功能(不要問為什麼)。

我有一個 WordPress 網站,並使用重力表單來詢問客戶的郵件地址。 然後我想使用該位址來尋找對應的 woo-commerce 產品,該產品具有新增為自訂屬性 (pa_e-mail) 的相同電子郵件地址。 然後我想使用另一個屬性,即保存密碼(不需要超強的安全性、md5、雜湊等)作為自訂屬性(pa_pw),並將該密碼發送回Gravity Forms,以透過郵件將其發送給用戶。另外,我想使用永久連結發送該產品的連結。

到目前為止,我的程式碼位於functions.php 中,在重力表單中,我有兩個可以接收動態填充的文字欄位。 (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 '';
  }
}

但是我收到的郵件只有空值,我希望在其中顯示 {Link:6} 和 {Passwort:7}。

P粉447495069P粉447495069310 天前493

全部回覆(2)我來回復

  • P粉262113569

    P粉2621135692024-03-30 19:40:11

    我重新寫了程式碼。

    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;
    
    }

    而且我非常確定,正是這一行導致了「錯誤」:

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

    如果我手動輸入郵件地址作為 $email,那麼它會按預期工作。 因此,Gravity Forms 一定存在某些問題,沒有傳遞電子郵件輸入文字欄位(ID 4)的值。

    有什麼提示嗎?

    回覆
    0
  • P粉841870942

    P粉8418709422024-03-30 16:19:09

    我終於開始工作了! 我的問題是,我的電子郵件欄位 (ID4) 的值無法在「gform_field_value_」過濾器的 $enrtry 變數中存取。

    這是我的工作程式碼:

    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_action( 'gform_pre_submission_7', 'pre_submission' );
    function pre_submission( $entry, $form ) {
        
        $entry_id = $entry['id'];
        
        global $email;
        $email = $_POST['input_2'];
        
        $edit_link = get_product_permalink_by_email( $email );
        if ( $edit_link ) {
            $edit_link .= 'edit';
        }
        $edit_pw = get_pw_by_email( $email );
    
        $_POST['input_3'] = $edit_link;
        $_POST['input_4'] = $edit_pw;
    
    }

    回覆
    0
  • 取消回覆