0

I have a custom contact form that I am trying to process in other file:

The form:

<form method="post" action="<?php echo get_template_directory_uri() . '/templates/contact.php' ?>" name="contactform" id="contactform">
                <div class="col-sm-6">
                    <fieldset>
                        <input name="name" type="text" id="name" size="30" value="" placeholder="Name" />
                        <br />
                        <input name="email" type="text" id="email" size="30" value="" placeholder="Email" />
                        <br />
                        <input name="phone" type="text" id="phone" size="30" value="" placeholder="Phone" />
                        <br />
                    </fieldset>
                </div>
                <div class="col-sm-6">
                    <fieldset>
                        <textarea name="comments" cols="40" rows="8" id="comments" placeholder="Message"></textarea>
                        <button type="submit" class="btn btn-green-border btn-lg" id="submit" value="submit">Send Message</button>
                        <input type="hidden" name="address" value="<?php echo $t_one_opt['contact_email'] ?>">
                    </fieldset>
                </div>
            </form>

But in the processing file I can achieve to get options values. I am using the Redux Framework, and work fine in all the site. I have put the global varible, too.

contact.php

<?php
if(!$_POST) exit;

global $t_one_opt;

$name     = $_POST['name'];
$email    = $_POST['email'];
$phone   = $_POST['phone'];
$comments = $_POST['comments'];


if(trim($name) == '') {
    echo '<div class="alert alert-danger text-center">Please enter your name.</div>';
    exit();
} else if(trim($email) == '') {
    echo '<div class="alert alert-danger text-center">Please enter a valid email address.</div>';
    exit();
} else if(trim($phone) == '') {
    echo '<div class="alert alert-danger text-center">Please enter a valid phone number.</div>';
    exit();
} else if(!is_numeric($phone)) {
    echo '<div class="alert alert-danger text-center">Phone number can only contain digits.</div>';
    exit();
} else if(!isEmail($email)) {
    echo '<div class="alert alert-danger text-center">You have enter an invalid e-mail address,please try again.</div>';
    exit();
}

if(trim($comments) == '') {
    echo '<div class="alert alert-danger text-center">Please enter your message.</div>';
    exit();
} 

if(get_magic_quotes_gpc()) {
    $comments = stripslashes($comments);
}


// Configuration option.
// Enter the email address that you want to emails to be sent to.
// Example $address = "joe.doe@yourdomain.com";

//$address = "example@themeforest.net";
//global $t_one_opt;
$address = $t_one_opt['contact_email'];


// Configuration option.
// i.e. The standard subject will appear as, "You've been contacted by John Doe."

// Example, $e_subject = '$name . ' has contacted you via Your Website.';

$e_subject = 'You\'ve been contacted by ' . $name . '.';


// Configuration option.
// You can change this if you feel that you need to.
// Developers, you may wish to add more fields to the form, in which case you must be sure to add them here.

$e_body = "You have been contacted by $name with regards to $subject, their additional message is as follows." . PHP_EOL . PHP_EOL;
$e_content = "\"$comments\"" . PHP_EOL . PHP_EOL;
$e_reply = "You can contact $name via email, $email or via phone $phone";

$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );

$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;

if(mail($address, $e_subject, $msg, $headers)) {

    // Email has sent successfully, echo a success page.

    echo "<fieldset>";
    echo "<div class='alert alert-success text-center'>";
    echo "<h3>Wohoooo ! Well done </h3>";
    echo "<p>Thank you <strong>$name</strong>, your message has been submitted to us.</p>";
    echo "</div>";
    echo "</fieldset>";

} else {

    echo 'ERROR!';
}

Neither it doesn't recognize Wordpress functions.

Why is this file "out" of WordPress?

1

It doesn't recognize WP, because as far as PHP is concerned it's not. It's just a PHP file that happens to be in one of the folders related to WordPress.

You need to explicitly include WordPress core into it to have access to its functionality. However such custom handlers are notoriously fragile.

It would be much more reliable to create endpoint by means of WordPress functionality. See Rewrite API and around in Codex.

  • I'd also say that an additional file to process it is unnecessary, the same template can be used to do both – Tom J Nowell Apr 24 '14 at 17:11
  • I use other file because I use jQuery to submit it. – Genethic Apr 24 '14 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.