Create a custom WordPress plugin to Saving Contact Form 7 Data. Creating a WordPress plugin to display Contact Form 7 submissions in the WordPress dashboard involves a few steps. Here’s a simplified example to get you started:
- Create a new folder for your plugin: In your WordPress plugins directory (usually
wp-content/plugins/
), create a new folder. Let’s name itcf7-submissions-dashboard
. - Create the main plugin file: Inside the
cf7-submissions-dashboard
folder, create a PHP file. Let’s name itcf7-submissions-dashboard.php
.
<?php /* Plugin Name: CF7 Data Description: Custom Plugin to save data Author: Sujeet Version: 1.0 */ register_activation_hook(__FILE__, 'table_creater'); function table_creater(){ global $wpdb; $table_name = $wpdb->prefix . 'cf7_custom_data'; if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) { $sql = "CREATE TABLE $table_name ( id mediumint(9) NOT NULL AUTO_INCREMENT, your_name varchar(200) NOT NULL, your_email varchar(200) NOT NULL, your_subject varchar(200) NOT NULL, your_message TEXT NOT NULL, submission_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } } function save_cf7_data($cf) { global $wpdb; $table_name = $wpdb->prefix . 'cf7_custom_data'; //$form_data = json_encode($cf->posted_data); $submission = WPCF7_Submission::get_instance(); if ( $submission ) { $cf7_data = $submission->get_posted_data(); $your_name = $cf7_data['your-name']; $your_email = $cf7_data['your-email']; $your_subject = $cf7_data['your-subject']; $your_message = $cf7_data['your-message']; //$form_data = json_encode($cf7_data); $wpdb->insert($table_name, array('your_name' => $your_name, 'your_email' => $your_email, 'your_subject' => $your_subject, 'your_message' => $your_message)); } return $cf; } add_action('wpcf7_before_send_mail', 'save_cf7_data'); function cf7_custom_data_menu() { add_menu_page('CF7 Custom Data', 'CF7 Custom Data', 'manage_options', 'cf7-custom-data', 'cf7_custom_data_page'); } add_action('admin_menu', 'cf7_custom_data_menu'); function cf7_custom_data_page() { global $wpdb; $table_name = $wpdb->prefix . 'cf7_custom_data'; $results = $wpdb->get_results("SELECT * FROM $table_name"); echo '<div class="wrap">'; echo '<h1>CF7 Custom Data</h1>'; echo '<table class="wp-list-table widefat fixed striped">'; echo '<thead><tr><th>ID</th><th>Name</th><th>Email</th><th>Subject</th><th>Message</th><th>Submission Date</th></tr></thead>'; echo '<tbody>'; foreach ($results as $result) { echo '<tr>'; echo '<td>' . esc_html($result->id) . '</td>'; echo '<td>' . esc_html($result->your_name) . '</td>'; echo '<td>' . esc_html($result->your_email) . '</td>'; echo '<td>' . esc_html($result->your_subject) . '</td>'; echo '<td>' . esc_html($result->your_message) . '</td>'; echo '<td>' . esc_html($result->submission_date) . '</td>'; echo '</tr>'; } echo '</tbody>'; echo '</table>'; echo '</div>'; }
Leave a Reply