简体   繁体   中英

Linking to a custom .php file in custom Wordpress theme

I'm working on a Wordpress theme that has a Jquery animated contact form in it. I've got the js working (a div that's gets shown/hidden on click, with the form in it). Here's a static HTML/PHP example (click the 'contact' button). What's the problem you say? The problem is that the actual form does not get send. I've got the php file 'contact engine.php' that sends the form inside a folder called 'php' in my template directory. In the HTML i've got:

<form method="post" action="<?php bloginfo('template_directory'); ?>/php/contactengine.php">
            <label for="Name">Name:</label>
            <input type="text" name="Name" id="Name" />
            <label for="Email">Email:</label>
            <input type="text" name="Email" id="Email" />
            <label id="bericht" for="Message">Message:</label><br />
            <textarea name="Message" rows="10" cols="20" id="Message"></textarea>
            <input type="submit" name="submit" value="Submit" class="submit-button" />
</form>

After a bit of fiddling I've narrowed the problem down to the actual link to the php file. Wordpress does not send the input to the php-file, and nothing gets done. After some Google sessions I found that most people use a plugin for this, which I'm not a fan of. Also it seems that Wordpress does not let you write your own php snippets to implement in the theme. I've also found something that suggests I should put the php snippet in the functions.php file that comes with every template, but I wouldn't know how to link to a specific php snippet inside functions.php. Anyone knows how to solve this? Thanks in advance!

PS The php script looks like this:

<?php

$EmailFrom = "contactformulier@stefanhagen.nl";
$EmailTo = "info@stefanhagen.nl";
$Subject = "Contactformulier StefanHagen.nl";
$Name = Trim(stripslashes($_POST['Name'])); 
$Tel = Trim(stripslashes($_POST['Tel'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Message = Trim(stripslashes($_POST['Message'])); 

// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
// $Body .= "Tel: ";
// $Body .= $Tel;
// $Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=../index.html\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>

I know when you submit a form within WordPress your code will not work since WordPress filters all the post variables. When you use your own script that shouldn't happen. So how does your contactengine.php script looks like. Do you use an include to WordPress in it? If so you do handle WordPress script.

What I see on your site is that < ?php bloginfo('template_directory'); ?> doesn't show up. It is also better to use < ?php echo get_template_directory_uri(); ?>

In case of the plugins people use, I do that too. Easy to change the form in the backend but that can be a negative point since the customer can change it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM