How to check if is page template inside functions.php

Question

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 );
                
            }
        }
    }
}
0
Slingy 3 months 0 Answers 17 views 0

Leave an answer