simple_contact_form.php

simple_contact_form.php

The provided PHP code will create a WordPress plugin named "Simple Contact Form". This plugin will add a shortcode [simple_contact_form] that you can use to display the form on any page or post. The form includes fields for name, email, message, and a Google reCAPTCHA for spam protection. When the form is submitted, an AJAX request is sent to the server to process the form. The server verifies the nonce and the reCAPTCHA response. If the validation is successful, an email containing the form data is sent to the site admin. The reCAPTCHA site key and secret should be replaced with your actual reCAPTCHA keys. Please replace "your-site-key" and "your-secret" with your actual reCAPTCHA site key and secret key. The form is only visible to users who have JavaScript enabled in their browsers, which adds an extra layer of spam protection.

<?php /** * Plugin Name: Simple Contact Form * Description: A simple contact form with conditional visibility and reCAPTCHA protection. * Version: 1.0 * Author: Your Name * Text Domain: codewp */ if (!defined('ABSPATH')) { exit; // Exit if accessed directly. } class SimpleContactForm { private $admin_email; public function __construct() { $this->admin_email = get_option('admin_email'); add_action('wp_ajax_contact_form', array($this, 'process_form')); add_action('wp_ajax_nopriv_contact_form', array($this, 'process_form')); add_shortcode('simple_contact_form', array($this, 'render_form')); } public function render_form() { ob_start(); ?> <form id="simple-contact-form"> <input type="text" id="name" name="name" required> <input type="email" id="email" name="email" required> <textarea id="message" name="message" required></textarea> <div class="g-recaptcha" data-sitekey="your-site-key"></div> <input type="submit" value="Submit"> </form> <script src="https://www.google.com/recaptcha/api.js"></script> <script> jQuery(document).ready(function($) { $('#simple-contact-form').submit(function(e) { e.preventDefault(); var recaptcha = $(".g-recaptcha-response").val(); if (recaptcha === "") { alert("Please check the reCAPTCHA"); return false; } var form_data = $(this).serialize(); $.ajax({ url: "<?php echo admin_url('admin-ajax.php'); ?>", type: "POST", data: form_data + '&action=contact_form', success: function(response) { alert(response); } }); }); }); </script> <?php return ob_get_clean(); } public function process_form() { if (!wp_verify_nonce($_POST['_wpnonce'], 'contact-form') || empty($_POST['g-recaptcha-response'])) { wp_die('Invalid nonce or reCAPTCHA response.'); } $response = wp_remote_get('https://www.google.com/recaptcha/api/siteverify?secret=your-secret&response=' . $_POST['g-recaptcha-response']); $response_body = json_decode($response['body'], true); if (!$response_body['success']) { wp_die('reCAPTCHA validation failed.'); } $to = $this->admin_email; $headers = array('Content-Type: text/html; charset=UTF-8'); $subject = 'New Contact Form Submission'; $body = 'Name: ' . sanitize_text_field($_POST['name']) . '<br>Email: ' . sanitize_email($_POST['email']) . '<br>Message: ' . sanitize_text_field($_POST['message']); wp_mail($to, $subject, $body, $headers); echo 'Your message has been sent successfully.'; wp_die(); } } new SimpleContactForm();

Frequently Asked Questions

This code adds a simple contact form with reCAPTCHA to a WordPress site using shortcodes.