Home Wordpress Hooks Wordpress hook to Save contact form 7 data into custom db

Wordpress hook to Save contact form 7 data into custom db

by therichpost
6 comments
How to add custom meta title and meta description in Wordpress?

Hello, welcome to therichpost.com. In this post, I will tell you, Wordpress hook to Save contact form 7 data into custom db. WordPress is the best cms. WordPress hooks(add_action, add_filter) give us the power to edit or change the code without interruption into the files and this is the best thing about wordpress. Now I am going to tell you how the hooks work.

Here is the Wordpress hook to Save contact form 7 data into custom db and you also need to make custom database to custom data and you need to add this wordpress hook into your wordpress theme’s functions.php file: 
1) Create Custom table

 CREATE TABLE candidate(
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  title VARCHAR(50)
);
2) Create contact form 7 fields

[text* title]
[submit "Send"]
3) Add Below code to function.php

  function contactform7_before_send_mail( $form_to_DB ) {
    //set your db details
    $mydb = new wpdb('root','','cistom_db','localhost');

    $form_to_DB = WPCF7_Submission::get_instance();
    if ( $form_to_DB ) 
        $formData = $form_to_DB->get_posted_data();
    $title = $formData['title'];

    $mydb->insert( 'candidate', array( 'title' =>$title ), array( '%s' ) );
}
remove_all_filters ('wpcf7_before_send_mail');
add_action( 'wpcf7_before_send_mail', 'contactform7_before_send_mail' );

 There are so many hooks in wordpress and i will let you know all. Please do comment if you any query related     to this post. Thank you. Therichpost.com

You may also like

6 comments

tarek August 16, 2018 - 12:38 pm

did this work for you ?

Reply
Ajay Malhotra August 17, 2018 - 5:20 am

Yes and tell me code error please..

Reply
Isuru September 11, 2020 - 5:53 am

Hi, Can know how to apply this code to one specific contact form only.

It means. I have few forms from contact form 7. but I only need to save data from, 1 form only.

Reply
Ajay Malhotra September 11, 2020 - 6:02 am

With below code:
$form_id = $contact_form->id();
if ($form_id == 2654 ) // 123 => Your Form ID.
{
}

Reply
Isuru September 11, 2020 - 10:16 am

Thank you very much!

Reply
Isuru September 19, 2020 - 1:55 pm

function contactform7_before_send_mail( $form_to_DB ) {

//set your db details
$mydb = new wpdb(‘smartlaw_cfform’,’WU&BNgxXKm95′,’smartlaw_formtable’,’localhost’);
$form_id = $contact_form->id();

$form_to_DB = WPCF7_Submission::get_instance();

if ($form_id == 7202 ) // 123 => Your Form ID

{
if ( $form_to_DB ) {
$formData = $form_to_DB->get_posted_data();

$form_name = $formData[‘your-name’];
$User_email = $formData[‘your-email’];
$User_subject = $formData[‘your-subject’];
$your_message = $formData[‘your-message’];

$mydb->insert( ‘dbtest’, array( ‘your-name’ =>$form_name, ‘your-email’ =>$User_email,’your-subject’ =>$User_subject, ‘your-message’ =>$your_message ), array( ‘%s’ ) );
}
}
remove_all_filters (‘wpcf7_before_send_mail’);
add_action( ‘wpcf7_before_send_mail’, ‘contactform7_before_send_mail’ );
}

Can I know, How this code needs to be changed? when I add this code to function.php., it shows – There has been a critical error on your website. Please check your site admin email inbox for instructions.

Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.