Categories

Thursday, December 26, 2024
#919814419350 therichposts@gmail.com
Wordpress Hooks

WordPress hook to Save contact form 7 data into custom db

How to add custom meta title and meta description in Wordpress?

Wordpress hook to Save contact form 7 data into custom db

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

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 19, MedusaJs, Next.js, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

6 Comments

  • 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.

        • 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.

Leave a Reply

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