Borang graviti harus mengekstrak data dari mysql
P粉447495069
P粉447495069 2024-03-30 00:05:24
0
2
361

Saya cuba membuat beberapa jenis ciri pemulihan kata laluan (jangan tanya mengapa).

Saya mempunyai laman web WordPress dan menggunakan borang graviti untuk meminta pelanggan alamat e-mel mereka. Saya kemudiannya ingin menggunakan alamat itu untuk mencari produk woo-commerce yang sepadan yang mempunyai alamat e-mel yang sama ditambah sebagai atribut tersuai (pa_e-mail). Saya kemudiannya ingin menggunakan atribut lain, iaitu untuk menyimpan kata laluan (tidak memerlukan keselamatan super kuat, md5, pencincangan, dll.) sebagai atribut tersuai (pa_pw) dan menghantar kata laluan itu kembali ke Gravity Forms untuk menghantarnya e-mel kepada pengguna. Juga, saya ingin menghantar pautan ke produk menggunakan pautan kekal.

Kod saya setakat ini dalam functions.php dan dalam bentuk graviti saya mempunyai dua medan teks yang boleh menerima pengisian dinamik. (edit_pw dan 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 '';
  }
}

Tetapi e-mel yang saya terima hanya mempunyai nilai nol di mana saya mahu {Pautan:6} dan {Passwort:7} dipaparkan.

P粉447495069
P粉447495069

membalas semua(2)
P粉262113569

Saya menulis semula kod tersebut.

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;

}

Dan saya agak pasti, baris inilah yang menyebabkan "ralat":

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

Jika saya memasukkan alamat e-mel secara manual sebagai $e-mel, ia berfungsi seperti yang diharapkan. Jadi mesti ada beberapa isu dengan Borang Graviti yang tidak melepasi nilai medan teks input e-mel (ID 4).

Ada petua?

P粉841870942

Saya akhirnya dapat bekerja! Masalah saya ialah nilai medan e-mel saya (ID4) tidak boleh diakses dalam pembolehubah $enrtry penapis "gform_field_value_".

Ini kod kerja saya:

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;

}
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!