Contact form in WordPress


I have a contact form script that I would like to use in my wordpress blog but doesn’t seem to work correctly in wordpress. Is there anything special I have to do in the markup to get it working??


Is this a plugin, and if so, what plugin?

Or, is this a completely customized script?

Its a custom script. Not sure if I have to set the cantact page up diffrent. Any help would be great!

In that case, much more detail on the script and problem would be necessary. Relevant source code would also be helpful.

What version of wordpress are you running?

Was this script working before an upgrade and not after?

heres the whole file im trying to get to work:

 * Template Name: Contact
 *Contact Page Template
 * The "Template Name:" bit above allows this to be selectable
 * from a dropdown menu on the edit page screen.
 * @package WordPress
 * @subpackage The Aggressive Network
 * @since The Aggressive Network V1.0

if(isset($_POST['Submit_x'])) {
	$Your_Name = htmlspecialchars($_POST['Name']);
	$email = htmlspecialchars($_POST['Email']);
	$email2 = htmlspecialchars($_POST['Email2']);
	$message = htmlspecialchars($_POST['Message']);
	$security1 = $_POST['security_code'];
	$security2 = $_SESSION['security_code'];
	$subject = htmlspecialchars($_POST['Subject']);
	$website = htmlspecialchars($_POST['WebsiteURL']);
if($Your_Name == '') {
			$err = true;
			$msg = '! Please enter your name';}
		if (!eregi("^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,6})$",$email) or $email == '') {
			$err4 = true;
			$msg4 = '! Please enter a valid email address';}
		if (!eregi("^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,6})$",$email2) or $email2 == '') {
			$err8 = true;
			$msg8 = '! Please enter a valid email address';}
	list($userName, $mailDomain) = split("@", $email);
		if ($email2 !== $email) {
	 		$err8 = true;
	 		$msg8 = '! Email Addresses do not match';}
		if($subject == 1) {
	    	$err6 = true;
			$msg6 = '! Please select subject';}
		if($message == '') {
			$err5 = true;
			$msg5 = '! Please type a message';}
		if($security1 == '') {
	    	$err3 = true;
			$msg3 = '! Please enter security code before trying to submit the form.';}
		if($security2 != $security1) {
	    	$err2 = true; 
	    	$msg2 = '! Sorry, you have provided an invalid security code. Please try again.';
	} else { 
		if (eregi("\\r",$email, $email2) || eregi("\
",$email, $email2)){ 
		die ("spam!");	 
    } else {
				if(($err == false) && ($err4 == false) && ($err8 == false) && ($err6 == false) && ($err5 == false) && ($err3 == false) && ($err2 == false) && ($err9 == false) && ($err10 == false)) {
				$showForm = false;					 
				$mailmsg = 'The following email has been sent from the Dragon Ink contact form:' . "\
				$mailmsg.= 'Name: ' . $Your_Name . "\
				$mailmsg.= 'Email: ' . $email . "\
				$mailmsg.= 'Website: ' . $website . "\
				$mailmsg.= 'Subject: ' . $subject . "\
				$mailmsg.= 'Message: ' . $message . "\
				mail('',$subject, $mailmsg, "From: Dragon-Ink"); 
				$emailSent = true;
				$thy = true;
                $thymsg = '<img src="contact/images/thankyou.jpg" width="188" height="33" alt="Thank You" border="0"><br />
<p class="thy">Your message was sent: <span class="date">' . date("l, F j, Y @ g:ia") . '</span></p>';

get_header(); ?>

<div id="main">
<div id="left">
<div class="left_Content"><div class="white_border">

<h2>Contact Us</h2>
<p>Want to get a hold of us? Feel free to offer any suggestions or if you have a question or a concern. Just simply fill out the form below. We will try our best to respond back to you as soon as we can.</p>

<?php if($emailSent == true) { ?>

	<div class="thanks">
		<p>Your email was successfully sent. I will be in touch soon.</p>

<?php } else { ?>

	<?php if (have_posts()) : ?>
	<?php while (have_posts()) : the_post(); ?>
		<h1><?php the_title(); ?></h1>
		<?php the_content(); ?>
<p class="news_dates">* Required Fields </p>

<form action="<?php the_permalink(); ?>" method="POST" class="form">
<div class="labelContainer"><label for="First_name"><span class="news_dates">*</span> Name:</label></div>
<div class="fieldContainer"><input type="text" name="Name" id="Name" value="<?php echo $Your_Name; ?>" />

if($err == true) {
			echo '<p class="error">'. $msg . '</p>';

<div class="labelContainer"><label for="email"><span class="news_dates">*</span> Email:</label></div>
<div class="fieldContainer"><input type="text" name="Email" id="Email" value="<?php echo $email; ?>" />

if($err4 == true) {
			echo '<p class="error">'. $msg4 . '</p>';
if($err7 == true) {
			echo '<p class="error">'. $msg7 . '</p>';
<div class="labelContainer"><label for="email2"><span class="news_dates">*</span> Verify Email:</label>
<div class="fieldContainer"><input type="text" name="Email2" id="Email2" value="<?php echo $email2; ?>" />

if($err8 == true) {
			echo '<p class="error">'. $msg8 . '</p>';

<div class="labelContainer"><label for="name">Your Site:</label></div>
<div class="fieldContainer"><input name="WebsiteURL" type="text" id="WebsiteURL" value="http://www." /></div>
<div class="labelContainer"><label for="subject"><span class="news_dates">*</span> Subject:</label></div>
<div class="fieldContainer"><select name="Subject" size="1" id="Subject" class="Default_selectDropDown">
<option value="1">------------Please Select------------</option>
<option class="Selected_selectDropDown">Website Related</option>
<option class="Selected_selectDropDown">Advertisement</option>
<option class="Selected_selectDropDown">Articles Question or Comment</option>
<option class="Selected_selectDropDown">Products</option>
<option class="Selected_selectDropDown">Privacy Policy Question</option>
<option class="Selected_selectDropDown">Other</option>

if($err6 == true) {
			echo '<p class="error">'. $msg6 . '</p>';

<div class="labelContainer"><label for="message"><span class="news_dates">*</span> Message:</label></div>
<div class="fieldContainer"><textarea rows="8" cols="60" name="Message" id="Message" ><?php echo $message; ?></textarea>

if($err5 == true) {
			echo '<p class="error">'. $msg5 . '</p>';

<div class="mailing_check"><input name="mailing" type="checkbox" id="mailing" value="true"> Add me to your mailing list!</div>
<div class="codelabelContainer"><label for="code"><span class="news_dates">*</span> Code:</label></div>
<div class="captcha"><img src="<?php bloginfo('template_directory'); ?>/contact/CaptchaSecurityImages.php?width=120&height=40&characters=5" alt="Image Code" style="border: 1px solid #666; "/></div>
<div class="labelContainer"><label for="security_code"><span class="news_dates">*</span> Enter Code:</label></div>
<div class="fieldContainer"><input name="security_code" type="text" id="security_code" value="Enter above code here" onFocus="if(this.value=='Enter above code here')this.value='';" />

if($err2 == true) {
			echo '<p class="error">'. $msg2 . '</p>';
if($err3 == true) {
			echo '<p class="error">'. $msg3 . '</p>';

<div class="submit"><input name="Submit" type="submit" value="Submit" alt="Submit" /></div>
		<?php endwhile; ?>
	<?php endif; ?>
<?php }				}

</div><!-- white_border --></div><!-- left_Content -->
</div><!-- left -->
<?php include(TEMPLATEPATH . '/sidebar1.php'); ?>
</div><!-- main -->
</div><!-- close wrap -->
</div><!-- bg_top_grad -->
<?php get_footer(); ?>

ALSO i dont know why my captcha images isnt showing. i placed it in my themes folder named contact. it was working before trying to use it on this wordpress blog.

What isn’t working exactly? Do you see any error messages? (use PHP: error_reporting - Manual to enable error reporting, so long as it isn’t turned off at the server level)


I get the following:

Notice: Undefined variable: emailSent in /home/content/D/r/a/Dragon60154739/html/wp-content/themes/Aggressive/contact.php on line 86

Notice: Undefined variable: err in /home/content/D/r/a/Dragon60154739/html/wp-content/themes/Aggressive/contact.php on line 108

Notice: Undefined variable: err4 in /home/content/D/r/a/Dragon60154739/html/wp-content/themes/Aggressive/contact.php on line 118

Notice: Undefined variable: err7 in /home/content/D/r/a/Dragon60154739/html/wp-content/themes/Aggressive/contact.php on line 121

Notice: Undefined variable: err8 in /home/content/D/r/a/Dragon60154739/html/wp-content/themes/Aggressive/contact.php on line 132

Notice: Undefined variable: err6 in /home/content/D/r/a/Dragon60154739/html/wp-content/themes/Aggressive/contact.php on line 152

Notice: Undefined variable: err5 in /home/content/D/r/a/Dragon60154739/html/wp-content/themes/Aggressive/contact.php on line 162

Notice: Undefined variable: err2 in /home/content/D/r/a/Dragon60154739/html/wp-content/themes/Aggressive/contact.php on line 175

Notice: Undefined variable: err3 in /home/content/D/r/a/Dragon60154739/html/wp-content/themes/Aggressive/contact.php on line 178

ok well, the script isnt showing the errors or sending the email if all fields are filled out right… any idea?

None of the variables within your if-statement are available where you try to access them in your HTML.

If you want to use variables that are within in your if-statement, you will need to define them outside in order to have access to them

For example, this is what your code is doing:


    <?php if($thing==true){?><p>Thing is true!</p><?php } ?>

This would throw an error because the $thing variable isn’t accessible from inside the if-statement.

This is how you should write it to have access to the $thing variable outside of the if-statement:




    <?php if($thing==true){?><p>Thing is true!</p><?php } ?>

[edit]: nevermind, I had forgotten that PHP doesn’t have its own scope for if-statements, so while this isn’t technically valid for PHP, it’s still good to put into practice to ensure good, clean, bug-free code.

thanks for all of the help!

the problem was this

<?php if(isset($_POST['Submit_x'])) {

had to change it to this:

<?php if(isset($_POST['Submit'])) {