Diwakar Academy

  • Home
  • WordPress
  • About Us
  • Contact Us
You are here: Home / WordPress / Building a Custom WordPress Plugin: Saving Contact Form 7 Data

November 10, 2023

Building a Custom WordPress Plugin: Saving Contact Form 7 Data

Spread the love

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:

  1. Create a new folder for your plugin: In your WordPress plugins directory (usually wp-content/plugins/), create a new folder. Let’s name it cf7-submissions-dashboard.
  2. Create the main plugin file: Inside the cf7-submissions-dashboard folder, create a PHP file. Let’s name it cf7-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>';
}


https://drive.google.com/file/d/1XuADc2yhGjTTX6EcaOhPxnt3FpPPn3n8/view https://diwakaracademy.com/wp-content/uploads/2025/01/seo.zip

Article by Diwakar Academy / WordPress Leave a Comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Home
  • WordPress
  • About Us
  • Contact Us

Copyright © 2025 · All Rights Reserved · Terms and Conditions · Privacy Policy