我正在尝试制作某种密码恢复功能(不要问为什么)。
我有一个 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}。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
我重新编写了代码。
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,那么它会按预期工作。 因此,Gravity Forms 一定存在某些问题,没有传递电子邮件输入文本字段(ID 4)的值。
有什么提示吗?
我终于开始工作了! 我的问题是,我的电子邮件字段 (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; }