Hack: The Simplest Contact Form Plugin

I don’t understand why there isn’t a built-in way to have a contact form on your WordPress site. There are plenty of plugins to do it, and you can easily hack it too. Here’s a very simple plugin that does it with a minimal number of lines of code.

It really is a bar minimum. It doesn’t check your email address, ask for a name, or do any kind of filtering or a captcha or anything. But I think it is important to have these simple alternatives. Maybe you can build from it. Or maybe you just don’t need anything more. If you already have a swiss-army-knife plugin of your own, you can add this code to it instead of using it as its own plugin. Heck, you could even just add the code to functions.php.

<?php
/*
Plugin Name: WP Tips and Hacks Simplest Contact Form
Plugin URI: https://wptipsandhacks.com
Description: A very simple way to put a contact form on a page
Version: 1.0
Author: WP Tips and Hacks
Author URI: https://wptipsandhacks.com
License:
*/

add_shortcode("simplestcontact","sc_simplestcontact");

function sc_simplestcontact($attributes,$content="") {
	ob_start();
	if (!isset($_POST['contacttext'])) { ?>
		<form action="" method="post" id="contactform" name="contactform">
		<b>Your Email Address</b> (Required)<br>
		<input type="text" class="formitem" title="Your Email Address" name="contactemail"/><br><br>
		<b>Text</b><br>
		<textarea class="formitem" rows="10" name="contacttext"></textarea><br><br>
		<input type="submit" class="formitem" value="SEND INQUIRY"/></form>
		<?
	} else {
		wp_mail(get_option('admin_email'), 'WordPress Contact', $_POST['contacttext'], "reply-to:".$_POST['contactemail']);	
		echo("Inquiry sent.");
	}
	return ob_get_clean();
}

Once you have the code added to your plugin, functions.php, or as its own plugin, you get to use the shortcode [simplestcontact] where you want the form to appear. It could just be on a page with nothing else.

Leave a Reply

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