0
X
Add Snippet To Project
New Project
Add To Existing Project
<?php
/**
* Plugin Name: Contact Form
* Description: Display a meta field or a custom field as a block on the front end, supporting ACF fields.
* Plugin URI: https://quadlayers.com/create-a-form-in-wordpress/
* Requires at least: 6.3
* Requires PHP: 7.4
* Version: 1.0.0
* Author: Eric Thomson
* License: GNU General Public License v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*
* @copyright Copyright(c) 2024, Eric Thomson
*
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/****************************************
* Contact Form
*/
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$name = sanitize_text_field( $_POST["name"] );
$email = sanitize_email( $_POST["email"] );
$message = sanitize_textarea_field( $_POST["message"] );
// Add code to check if the email already exists in the database
global $wpdb;
$table_name = $wpdb->prefix . 'contact_form_submission';
$existing_row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table_name WHERE email = %s", $email ) );
if ( $existing_row ) {
$response = array(
'success' => false,
'message' => 'A submission with this email already exists.',
);
} else {
$data = array(
'name' => $name,
'email' => $email,
'message' => $message,
'submission_time' => current_time( 'mysql' ),
);
$insert_result = $wpdb->insert( $table_name, $data );
if ( $insert_result === false ) {
$response = array(
'success' => false,
'message' => 'Error saving the form data.',
);
} else {
$response = array(
'success' => true,
'message' => 'Form data saved successfully.',
);
// Add the redirect header for successful submission
header( 'Location: /ok/' );
exit;
}
}
// Return the JSON response
header( 'Content-Type: application/json' );
echo json_encode( $response );
exit;
}
/* Display Form Submissions on Dashboard */
function display_contact_form_submissions_page() {
global $wpdb;
$table_name = $wpdb->prefix . 'contact_form_submission';
$form_data = $wpdb->get_results( "SELECT * FROM $table_name WHERE name <> '' AND email <> '' AND message <> '' ORDER BY submission_time DESC", ARRAY_A );
?>
<div class="wrap">
<h1>Contact Form Submissions</h1>
<table class="wp-list-table widefat fixed striped">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Message</th>
<th>Submission Time</th>
</tr>
</thead>
<tbody>
<?php foreach ( $form_data as $data ) : ?>
<tr>
<td><?php echo esc_html( $data['name'] ); ?></td>
<td><?php echo esc_html( $data['email'] ); ?></td>
<td><?php echo esc_html( $data['message'] ); ?></td>
<td><?php echo esc_html( $data['submission_time'] ); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php }
function register_contact_form_submissions_page() {
add_menu_page(
'Contact Form Submissions',
'Form Submissions',
'manage_options',
'contact_form_submissions',
'display_contact_form_submissions_page',
'dashicons-feedback'
);
}
add_action( 'admin_menu', 'register_contact_form_submissions_page' );
This snippet adds a contact form that checks to see if the Email already exists in the database table before submitting. If the submission is successful it redirects to another page.