How To Insert Data To The Database Of Contact Form 7?

To insert data to the database – at first – need to create a new contact form using contact form 7 plugin. There are lots of plugin which gives the functionality of that amongst them contact form 7 db is popular. But If you want to do it in a custom way, you need to learn how to insert data to the Database. WordPress is the top-notch cms having its own function which will able to do it at ease.

At first check the contact form 7 code:

<div>[text* your-name class:form-control placeholder "Name"]</div>
<div>[text* your-subject class:form-control class:mb-2 placeholder "Subject"]</div>
<div>[email* your-email class:form-control class:mb-2 placeholder "Email Address"]</div>
<div></div>
<div>[submit class:btn class:btn-primary "Send"]</div>

All are shortcode provided by the contact form 7. This shortcode generate html input field with name key- the above form name key are services,  In php language we need name key to pick the data and in JavaScript we need id.

Now it is the time to create custom database table in wordpress where form data will be stored. In that case we use “init” wordpress hooks to create table and code will use in functions.php. Check the Code

How To Creat Custom WordPress Table:


function sujoyroy_customer_details_table_create() {

global $wpdb;
$table_name = $wpdb->prefix. "customer_details";
global $charset_collate;
$charset_collate = $wpdb->get_charset_collate();
global $db_version;

if( $wpdb->get_var("SHOW TABLES LIKE '" . $table_name . "'") != $table_name)
{ $create_sql = "CREATE TABLE " . $table_name . " (
id INT(11) NOT NULL auto_increment,
form_post_id VARCHAR(256) NOT NULL,
name VARCHAR(256) NOT NULL,
subject VARCHAR(256) NOT NULL,
email VARCHAR(256) NOT NULL,
entry_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,

PRIMARY KEY (id))$charset_collate;";
require_once(ABSPATH . "wp-admin/includes/upgrade.php");
dbDelta( $create_sql );
}

if (!isset($wpdb->packages))
{
$wpdb->packages = $table_name;
$wpdb->tables[] = str_replace($wpdb->prefix, '', $table_name);
}

}
add_action( 'init', 'sujoyroy_customer_details_table_create');

Above code will automatically create custom table with prefix.. If database prefix is ‘wp_’, table name will be ‘wp_customer_details’.

 

Now you can use wordpress insert functionality to store the data in the database. Hooks is ‘wpcf7_before_send_mail’ – it is a contact form 7 hooks works before mail sent and this code will be used in functions.php file in current activate theme folder.

function sujoy_roy_before_send_mail( $form_tag ) {

    global $wpdb;
    $table_name    = $wpdb->prefix.'customer_details';
    $time_now = time();


    $submission   = WPCF7_Submission::get_instance();
    $contact_form = $submission->get_contact_form();


    if ( $submission ) {

        $data = $submission->get_posted_data();
        $form_post_id = $form_tag->id();
        $subject = $data['your-subject'];
        $name = $data['your-name'];
        $email = $data['your-email'];
        $form_date    = current_time('Y-m-d H:i:s');

        $wpdb->insert( $table_name, array(
            'form_post_id' => $form_post_id,
             'name' => $name,
             'subject' => $subject,
             'email' => $email,
             'entry_date' => $form_date
        ) );

    }

}

add_action( 'wpcf7_before_send_mail', 'sujoy_roy_before_send_mail' );

In that 3step process we can store the data.

Leave a Message

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
By commenting you accept the Privacy Policy