Skip to content

Instantly share code, notes, and snippets.

@vvickedvveb
Last active May 6, 2022 05:36
Show Gist options
  • Save vvickedvveb/f9a7715c9ada8d358538d1e7a478ffc8 to your computer and use it in GitHub Desktop.
Save vvickedvveb/f9a7715c9ada8d358538d1e7a478ffc8 to your computer and use it in GitHub Desktop.
Simple email contact form plugin for WordPress so people can email you. Just drop file in your "plugins" director and activate. To view add "shortcode" in page and add "[vick_form]". Change '$to' email address below to yours. Create a page to submit form e.g.: permalink "thank-you-submit". TODO: Add Captcha.
<?php
/**
* Plugin Name: Simple Email Form
*
*/
function vick_form_plugin()
{
$content = '';
$content .= '<form method="post" action="' . site_url() . '/thank-you-submit">';
$content .= '<label for="your_name">Name</label>';
$content .= '<input type="text" class="form-control" name="your_name" placeholder="Enter your name" required>';
$content .= '<label for="your_email">Email</label>';
$content .= '<input type="text" class="form-control" name="your_email" placeholder="Enter your email" required>';
$content .= '<label for="your_comments">Questions / Comments</label>';
$content .= '<textarea class="form-control" name="your_comments" placeholder="Enter your questions or comments" required></textarea>';
$content .= '<input class="form-control mt-2 btn btn-primary" type="submit" name="vick_form_submit" value="Send">';
$content .= '</form>';
return $content;
}
add_shortcode( 'vick_form', 'vick_form_plugin' );
function vick_form_capture()
{
if (isset($_POST['vick_form_submit'])) {
$name = sanitize_text_field( $_POST['your_name'] );
$email = sanitize_text_field( $_POST['your_email'] );
$comments = sanitize_textarea_field( $_POST['your_comments'] );
$to = 'MAKE_SURE_TO_PUT_YOUR_EMAIL_HERE@ASDASDSA.COM.AU.IN.JP.CA';
$subject = 'Site Form';
$message = $name . ' - (' . $email . ')\n\n' . $comments;
wp_mail($to, $subject, $message);
}
}
add_action( 'wp_head', 'vick_form_capture')
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment