Last active
November 26, 2024 00:55
-
-
Save Kevinlearynet/411c6a8432fcc7be2ed7ef3cfff35ea4 to your computer and use it in GitHub Desktop.
Simple WordPress contact form that sends an admin email when submissions happen.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Basic Contact Form | |
Plugin URI: https://kevinleary.net | |
Description: A simple contact form plugin that sends email submissions using wp_mail(). | |
Version: 1.0 | |
Author: kevinlearynet | |
Author URI: https://kevinleary.net | |
*/ | |
// Register the shortcode for the contact form | |
function basic_contact_form_shortcode() | |
{ | |
ob_start(); | |
if ( | |
$_SERVER["REQUEST_METHOD"] == "POST" && | |
isset($_POST["basic_contact_form_submit"]) | |
) { | |
$name = sanitize_text_field($_POST["name"]); | |
$email = sanitize_email($_POST["email"]); | |
$inquiry_type = sanitize_text_field($_POST["inquiry_type"]); | |
$message = sanitize_textarea_field($_POST["message"]); | |
$admin_email = get_option("admin_email"); | |
if ( | |
!empty($name) && | |
!empty($email) && | |
is_email($email) && | |
!empty($message) | |
) { | |
$subject = "New Inquiry: $inquiry_type"; | |
$body = "Name: $name\nEmail: $email\nInquiry Type: $inquiry_type\n\nMessage:\n$message"; | |
$headers = ["Content-Type: text/plain; charset=UTF-8"]; | |
if (wp_mail($admin_email, $subject, $body, $headers)) { | |
echo "<p>Thank you for your submission. We will get back to you shortly.</p>"; | |
} else { | |
echo "<p>There was an issue sending your message. Please try again later.</p>"; | |
} | |
} else { | |
echo "<p>Please fill in all fields correctly.</p>"; | |
} | |
} | |
?> | |
<form method="post"> | |
<p> | |
<label for="name">Name</label> | |
<input type="text" id="name" name="name" required> | |
</p> | |
<p> | |
<label for="email">Email</label> | |
<input type="email" id="email" name="email" required> | |
</p> | |
<p> | |
<label for="inquiry_type">Inquiry Type</label> | |
<select id="inquiry_type" name="inquiry_type" required> | |
<option value="Donate">Donate</option> | |
<option value="Questions">Questions</option> | |
<option value="Feedback">Feedback</option> | |
<option value="Other">Other</option> | |
</select> | |
</p> | |
<p> | |
<label for="message">Message</label> | |
<textarea id="message" name="message" rows="5" required></textarea> | |
</p> | |
<p> | |
<button type="submit" name="basic_contact_form_submit">Send</button> | |
</p> | |
</form> | |
<?php return ob_get_clean(); | |
} | |
add_shortcode("basic_contact_form", "basic_contact_form_shortcode"); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment