How do I check if email submitted via Contact form 7 exists in my database?

后端 未结 2 1549
有刺的猬
有刺的猬 2021-01-07 01:53

When customer submits email via contact form 7, how do I check if email already exists in my database and change notification message to \"Your email already exists in our d

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-07 02:28

    I've added a filter on validation:

    add_filter( 'wpcf7_validate', 'email_already_in_db', 10, 2 );
    
    function email_already_in_db ( $result, $tags ) {
        // retrieve the posted email
        $form  = WPCF7_Submission::get_instance();
        $email = $form->get_posted_data('your-email');
        // if already in database, invalidate
        if( email_exists( $email ) ) // email_exists is a WP function
            $result->invalidate('your-email', 'Your email exists in our database');
        // return the filtered value
        return $result;
    }
    

    In this case the email field is named your-email.

    The function email_exists is a native WP function to handle this.

提交回复
热议问题