So I've got a website with multiple contact form 7 forms. One function is triggered by wpcf7_before_send_mail
hook. This function has to work on only one form. Specified form is used in only one page template, so I came up with simple solution - use is_page_template
function, but it does not work.
How can I check if page template is used, then. Code is placed inside functions.php. Page template is inside page-templates folder.
Here is my code
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 );
}
}
}
}
So I've got a website with multiple contact form 7 forms. One function is triggered by wpcf7_before_send_mail
hook. This function has to work on only one form. Specified form is used in only one page template, so I came up with simple solution - use is_page_template
function, but it does not work.
How can I check if page template is used, then. Code is placed inside functions.php. Page template is inside page-templates folder.
Here is my code
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 );
}
}
}
}
Share
Improve this question
asked Oct 31, 2020 at 17:12
SlingySlingy
311 silver badge5 bronze badges
1 Answer
Reset to default 0So, I figured it out. Using hook wpcf7_before_send_mail
you don't have access to all meta data needed for is_page_template function
to work. So instead, you have to use methods showed here