Saving contact form 7 data into custom db and not wordpress db

前端 未结 1 424
忘掉有多难
忘掉有多难 2021-01-07 06:56

I am using contact form 7 for creating my form and I have a custom db hosted in the same server that should contain the related data.

I want to store the data from t

相关标签:
1条回答
  • 2021-01-07 07:26

    Follow the steps to store contact form 7 data in custom table:

    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' );
    

    Hope this works for you.

    0 讨论(0)
提交回复
热议问题