所以我有一个有多种表格7表格的网站。一个函数由wpcf7_before_send_mail挂钩触发。此函数只能在一种形式上工作。指定的表单只在一个页面模板中使用,所以我想出了一个简单的解决方案--使用is_page_template函数,但它不起作用。
那么,如何检查是否使用了页面模板。代码放置在functions.php中。页面模板在页面-模板文件夹中。
这是我的密码
add_action( 'wpcf7_before_send_mail', 'wpcf7_add_text_to_mail_body' );
function wpcf7_add_text_to_mail_body($contact_form) {
if ( is_page_template( 'page-templates/konfigurator.php' ) ) {
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
$email = $submission->get_posted_data('your-email');
// $first_name = $formdata['text-7'];
//$password = 'lorem';
$user_pass = wp_generate_password();
if( !email_exists( $email )) {
$user_id = wp_create_user( $email, $user_pass, $email );
$user = new WP_User($user_id);
$user->set_role('customer');
$emails = WC()->mailer()->get_emails();
$emails['WC_Email_Customer_New_Account']->trigger( $user_id, $user_pass, true );
}
}
}
}发布于 2020-11-01 09:59:32
所以我想出来了。使用钩子wpcf7_before_send_mail,您无法访问is_page_template function工作所需的所有元数据。因此,您必须使用显示这里的方法
https://wordpress.stackexchange.com/questions/377458
复制相似问题