How can we insert contact form 7 data/email in another table in wordpress?

If we want to save the form to a custom table in our WP database and If we have to insert email in wp_newsletter table if email is not exist in table.

Also The email should still be sent.

For this we just have to use the below code in our theme's function.php file:


/*
 insert email in newsletter after submitting contact form 7
*/
add_action("wpcf7_before_send_mail", "wpcf7_do_something_else");  
function wpcf7_do_something_else($cf7) {
    $wpcf = WPCF7_ContactForm::get_current();
 
 $submission = WPCF7_Submission::get_instance();
 $posted_data = $submission->get_posted_data();
    $youremail = $posted_data['your-email'];  //If  your-email is email field name.
    // if we have to insert email in wp_newsletter table if email is not exist in table.
  global $wpdb; 
  $query= "INSERT INTO wp_newsletter (email,status) 
     SELECT '$youremail','C'
     FROM dual 
     WHERE NOT EXISTS
     ( SELECT *
       FROM wp_newsletter
       WHERE email = '$youremail'
       AND status= 'C'
     ) ; ";
  
  $wpdb->query($wpdb->prepare($query));
 /*
 mail("amu02.aftab@gmail.com","My subject",$query); 
    */ 
 
    return $wpcf;
}

No comments:

Post a Comment