最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to check if is page template inside functions.php

programmeradmin2浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 0

So, 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

发布评论

评论列表(0)

  1. 暂无评论