Saturday 31 December 2016

forms - 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:
















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!



P.S. The php script looks like this:




$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 "";
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 "";
}
else{
print "";
}
?>

Answer



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.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...