如何开发连接到个人电子邮件的自定义Wordpress联系表单

时间:2017-05-26 21:59:34

标签: php wordpress post permalinks contact-form

我试过到处寻找这个问题的解决方案,但无济于事。我已经为WordPress网站整理了一个自定义联系表单。我正在使用表单的自定义模板,以便我可以使用php get_header();页脚和侧边栏功能。一切都很好,但我似乎遇到了形式方法post和动作标签的问题,这将加载“找不到页面”窗口而不是回声。我真诚地感谢您提供的任何建议或指示。

我试图找到WordPress解决方法的代码部分是:

<form method="post" id="customer" action="<?php the_permalink(); ?>">

与...结合使用:

   <?php

   $name = $_POST['name'];
   $name = htmlspecialchars($name);
   $service = $_POST['service'];
   $service = htmlspecialchars($service);
   $number = $_POST['number'];
   $number = htmlspecialchars($number);
   $email = $_POST['email']; 
   $email = htmlspecialchars($email);
   $message = $_POST['message'];
   $message = htmlspecialchars($message);
   $from = 'From: XXXXXX'; 
   $to = 'XXXXXXXXX@XXXXX.com'; 
   $subject = 'Customer';
   $human = $_POST['human'];

   $body = "From: $name\n E-Mail: $email\n Service: $service\n Number:\n 
   $number\n Message:\n $message\n";


   if ($_POST['submit']) {
   if ($name != '' && $email != '') {
if ($human == '7') {         
    if (mail ($to, $subject, $body, $from)) { 
  echo '<p><font color="#f58b7b" size="20px">Your message has been sent!</font></p>';
   } else { 
  echo '<p><font color="#f58b7b" size="20px">Something went wrong, go back and try again!</font></p>'; 
  } 
  } else if ($_POST['submit'] && $human != '7') {
  echo '<p><font color="#f58b7b" size="20px">You answered the anti-spam 
  question incorrectly!</font></p>';
  }
  } else {
echo '<p><font color="#f58b7b" size="20px">*You need to fill in all 
required fields!</font></p>';
}
}

?>

以下是该文件的完整来源:

<?php /* Template Name: contactform */ ?>
<?php get_header(); ?>
<style type="text/css">
h1 {
text-align: none;
width: 100%;
margin: 20px auto;
}

textarea {
width: 200px;
border: 0.1px solid #6ec166;
border-radius: 1px;
padding: 3px;
outline: 0;
}
</style>
<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">
        <section class="body">
  <form method="post" id="customer" action="<?php the_permalink(); ?>">
<div class="container main">
<div class="row">
    <div class="twelve columns portfolio">
        <h1><font color="#f97228" size="150px"><strong>We look forward to hearing from you!</strong></font></h1>
</div>
    <div class="twelve columns portfolio">
 <p>My name is  <textarea name="name" placeholder="First and Last name">
</textarea> and I am contacting you <select id="services" name="service" 
 form="customer"><option value="1">to say hi</option>
<option value="2">for a review request</option>
<option value="3">to submit my review</option>
<option value="4">for an advertising opportunity</option>
</select>.</br>
Please contact me over the phone <textarea name="number" placeholder="Your 
phone number"></textarea> or through email <textarea name="email" 
placeholder="Your email"></textarea>.</br>
Here is my message:<textarea name="message" placeholder="detailed message">
</textarea>.</br></p>
</div>
<div class="twelve columns portfolio">
<label>*What is 5+2? (Anti-spam)</label>
<input name="human" placeholder="Type Here">

<input id="submit" name="submit" type="submit" value="Submit">

</div>
</div>
</div> 

<?php

$name = $_POST['name'];
$name = htmlspecialchars($name);
$service = $_POST['service'];
$service = htmlspecialchars($service);
$number = $_POST['number'];
$number = htmlspecialchars($number);
$email = $_POST['email'];
$email = htmlspecialchars($email);
$message = $_POST['message'];
$message = htmlspecialchars($message);
$from = 'From: XXXXXX'; 
$to = 'XXXXXXXXX@XXXXX.com'; 
$subject = 'Customer';
$human = $_POST['human'];

$body = "From: $name\n E-Mail: $email\n Service: $service\n Number:\n $number\n Message:\n $message\n";


if ($_POST['submit']) {
if ($name != '' && $email != '') {
    if ($human == '7') {         
        if (mail ($to, $subject, $body, $from)) { 
      echo '<p><font color="#f58b7b" size="20px">Your message has been sent!</font></p>';
  } else { 
      echo '<p><font color="#f58b7b" size="20px">Something went wrong, go back and try again!</font></p>'; 
  } 
  } else if ($_POST['submit'] && $human != '7') {
  echo '<p><font color="#f58b7b" size="20px">You answered the anti-spam 
  question incorrectly!</font></p>';
  }
  } else {
    echo '<p><font color="#f58b7b" size="20px">*You need to fill in all 
 required fields!</font></p>';
}
}

?>
</form>
</section>
<?php
  while ( have_posts() ) : the_post();

    get_template_part( 'template-parts/content', 'page' );

    // If comments are open or we have at least one comment, load up the comment template.
    if ( comments_open() || get_comments_number() ) :
      comments_template();
    endif;

  endwhile; // End of the loop.
  ?>


    </main><!-- #main -->
</div><!-- #primary -->

<?php
get_sidebar(); ?>
<?php get_footer(); ?>

0 个答案:

没有答案