0
votes

I would like to point out that I'm not really a developer, but rather a designer. I'm trying to fit a custom contact form on a Wordpress Template file. It works on principle based on a tutorial that's at Cats Who Code: How to create a built-in contact form for your Wordpress theme.

I have modified the contact form to have the following:

  • Name (Text Field)
  • Company (Text Field)
  • Address (Text Area)
  • Email Address (Text Field)
  • Telephone (Text Field)
  • Mobile (Text Field)
  • Enquiry (Text Area)

However, when I integrate it into a custom template, and put it on the Wordpress install, it just gives me a blank page, with blank code. I am wondering why this is happening, and when I remove the contact form from the template, it's all good without the contact form. Here is the entire template code below.

<?php
if(isset($_POST['submitted'])) {
if(trim($_POST['name']) === '') {
    $nameError = 'Please enter your name.';
    $hasError = true;
} else {
    $name = trim($_POST['name']);
}
if(trim($_POST['company']) === '') {
    $companyError = 'Please enter your company name.';
    $hasError = true;
} else {
    $company = trim($_POST['company']);
}
if(trim($_POST['address']) === '') {
    $addressError = 'Please enter your address.';
    $hasError = true;
} else {
    if(function_exists('stripslashes')) {
        $address = stripslashes(trim($_POST['address']));
    } else {
        $address = trim($_POST['address']);
    }
}
    if(trim($_POST['email']) === '')  {
    $emailError = 'Please enter your email address.';
    $hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
    $emailError = 'You entered an invalid email address.';
    $hasError = true;
} else {
    $email = trim($_POST['email']);
}
if(trim($_POST['telephone']) === '') {
    $telephoneError = 'Please enter your telephone number.';
    $hasError = true;
} else {
    $telephone = trim($_POST['telephone']);
}
if(trim($_POST['mobile']) === '') {
    $mobileError = 'Please enter your mobile phone number.';
    $hasError = true;
} else {
    $mobile = trim($_POST['mobile']);
}
if(trim($_POST['enquiry']) === '') {
    $enquiryError = 'Please enter a message.';
    $hasError = true;
} else {
    if(function_exists('stripslashes')) {
        $enquiry = stripslashes(trim($_POST['enquiry']));
    } else {
        $enquiry = trim($_POST['enquiry']);
    }
}

if(!isset($hasError)) {
    $emailTo = get_option('tz_email');
    if (!isset($emailTo) || ($emailTo == '') ){
        $emailTo = get_option('admin_email');
    }
    $subject = '[Blue Doors] From '.$name;
    $body = "Name: $name \n\nCompany: $company \n\nAddress: $address \n\nEmail: $email \n\nTel: $telephone \n\nMobile: $mobile \n\nDetails of Enquiry: $enquiry";
    $headers = 'From: '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

    mail($emailTo, $subject, $body, $headers);
    $emailSent = true;
}
} ?>

<?php get_header(); ?>

<section class="content">
    <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>

    <header class="content-title">
        <h1><?php the_title(); ?></h1>
    </header>

        <article class="content-body">  
            <?php the_post_thumbnail(); ?>
            <?php the_content(); ?>
        </article>

        <article class="content-body">

        <?php if(isset($emailSent) && $emailSent == true) { ?>
                        <div class="thanks">
                            <p>Thanks, your email was sent successfully.</p>
                        </div>
                        <?php } else { ?>
                        <?php the_content(); ?>
                        <?php if(isset($hasError) || isset($captchaError)) { ?>
                            <p>Sorry, an error occured.<p>
                        <?php } ?>

            <form action="<?php the_permalink(); ?>" id="contactform" method="post">
                <ul>
                    <li>
                        <label>What is your name?</label>
                        <input type="text" name="name" id="name" value="<?php if(isset($_POST['name'])) echo $_POST['name'];?>" class="required"/>
                        <?php if($nameError != '') { ?>
                                <span class="error"><?=$nameError;?></span>
                            <?php } ?>
                    </li>
                    <li>
                        <label>What is your company's name?</label>
                        <input type="text" name="company" id="company" value="<?php if(isset($_POST['company'])) echo $_POST['company'];?>" class="required"/>
                    </li>
                    <li>
                        <label class="address">What is your address?</label>
                        <textarea name="address" id="address" rows="5" cols="30" class="required"><?php if(isset($_POST['address'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['address']); } else { echo $_POST['address']; } } ?></textarea>
                            <?php if($addressError != '') { ?>
                                <span class="error"><?=$addressError;?></span>
                            <?php } ?>
                    </li>
                    <li>
                        <label>What is your email address?</label>
                        <input type="text" name="email" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" class="required" />
                            <?php if($emailError != '') { ?>
                                <span class="error"><?=$emailError;?></span>
                            <?php } ?>
                    </li>
                    <li>
                        <label>What is your telephone no?</label>
                        <input type="text" name="telephone" id="telephone" value="<?php if(isset($_POST['telephone'])) echo $_POST['telephone'];?>" class="required" />
                            <?php if($nameError != '') { ?>
                                <span class="error"><?=$telephoneError;?></span>
                            <?php } ?>
                    </li>
                    <li>
                        <label>What is your mobile no?</label>
                        <input type="text" name="mobile" id="mobile" value="<?php if(isset($_POST['mobile'])) echo $_POST['mobile'];?>" class="required" />
                            <?php if($mobileError != '') { ?>
                                <span class="error"><?=$mobileError;?></span>
                            <?php } ?>
                    </li>
                    <li>
                        <label>What would you like to discuss about with Blue Doors?</label>
                        <textarea name="enquiry" id="enquiry" rows="8" cols="30" class="required"><?php if(isset($_POST['enquiry'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['enquiry']); } else { echo $_POST['enquiry']; } } ?></textarea>
                            <?php if($enquiryError != '') { ?>
                                <span class="error"><?=$enquiryError;?></span>
                            <?php } ?>
                    </li>
                    <li>
                        <button type="submit" class="submitbutton">Submit your enquiry</button>
                    </li>
                </ul>
                <input type="hidden" name="submitted" id="submitted" value="true" />
            </form>
        </article>

    <?php endwhile; endif; ?>

</section>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

I'm trying to figure out where it's breaking but I'm not really any good with PHP nor can understand it. Could anyone point out where I'm going wrong, it would be much appreciated.

1
Did you added your Template to the Contact Page?Libin
Yeah I did. It came out as blank every time I went to the page. If I removed the form, the contact page works fine.j-mes
Put some dummy content in your contact page through WordPress dashboard and check those content are displaying or not.Libin
I tried that method, and it works fine with a Lorem Ipsum paragraph, without the contact form. With the contact form, it's a blank page.j-mes

1 Answers

0
votes

You had put your contact form code inside the loop <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>

Try put the whole contact form code starting from <?php if(isset($emailSent) && $emailSent == true) { ?> to </form> outside the loop. ie put that code part after this line <?php endwhile; endif; ?>

Hope this will help.

EDIT:

You are missing one curly braces on the code. Update the below part of code. I had only added one more braces at the last line.

<?php if(isset($emailSent) && $emailSent == true) { ?>
                        <div class="thanks">
                            <p>Thanks, your email was sent successfully.</p>
                        </div>
                        <?php } else { ?>
                        <?php the_content(); ?>
                        <?php if(isset($hasError) || isset($captchaError)) { ?>
                            <p>Sorry, an error occured.<p>
                        <?php } } ?>

And it is perfectly working for me.