我一直在关注如何制作自定义 WordPress 联系表格的WordPress 联系表格教程。
我已经检查,重新检查并再次检查,但我仍然无法使其正常工作。当我提交完成所有字段的表单时,我只是得到'消息未发送。再试一次。'
由于这是用 WordPress 编写的,我无法确定是否发生了变化。我的代码是这样的:为了确保我没有犯错,我完全复制了教程。
<?php
/*
Template Name: Contact Page
*/
?>
<?php
//response generation function
$response = "";
//function to generate response
function my_contact_form_generate_response($type, $message){
global $response;
if($type == "success") $response = "<div class='success'>{$message}</div>";
else $response = "<div class='error'>{$message}</div>";
}
//response messages
$not_human = "Human verification incorrect.";
$missing_content = "Please supply all information.";
$email_invalid = "Email Address Invalid.";
$message_unsent = "Message was not sent. Try Again.";
$message_sent = "Thanks! Your message has been sent.";
//user posted variables
$name = $_POST['message_name'];
$email = $_POST['message_email'];
$message = $_POST['message_text'];
$human = $_POST['message_human'];
//php mailer variables
$to = get_option('admin_email');
$subject = "Someone sent a message from ".get_bloginfo('name');
$headers = 'From: '. $email . "\r\n" .
'Reply-To: ' . $email . "\r\n";
if(!$human == 0){
if($human != 2) my_contact_form_generate_response("error", $not_human); //not human!
else {
//validate email
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
my_contact_form_generate_response("error", $email_invalid);
else //email is valid
{
//validate presence of name and message
if(empty($name) || empty($message)){
my_contact_form_generate_response("error", $missing_content);
}
else //ready to go!
{
$sent = wp_mail($to, $subject, strip_tags($message), $headers);
if($sent) my_contact_form_generate_response("success", $message_sent); //message sent!
else my_contact_form_generate_response("error", $message_unsent); //message wasn't sent
}
}
}
}
else if ($_POST['submitted']) my_contact_form_generate_response("error", $missing_content);
?>
<?php Starkers_Utilities::get_template_parts( array( 'parts/shared/html-header', 'parts/shared/header' ) ); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header>
<div class="entry-content group">
<?php the_content(); ?>
<style type="text/css">
.error{
padding: 5px 9px;
border: 1px solid red;
color: red;
border-radius: 3px;
}
.success{
padding: 5px 9px;
border: 1px solid green;
color: green;
border-radius: 3px;
}
form span{
color: red;
}
</style>
<div id="respond">
<?php echo $response; ?>
<form action="<?php the_permalink(); ?>" method="post">
<p><label for="name">Name: <span>*</span> <br><input type="text" name="message_name" value="<?php echo esc_attr($_POST['message_name']); ?>"></label></p>
<p><label for="message_email">Email: <span>*</span> <br><input type="text" name="message_email" value="<?php echo esc_attr($_POST['message_email']); ?>"></label></p>
<p><label for="message_text">Message: <span>*</span> <br><textarea type="text" name="message_text"><?php echo esc_textarea($_POST['message_text']); ?></textarea></label></p>
<p><label for="message_human">Human Verification: <span>*</span> <br><input type="text" style="width: 60px;" name="message_human"> + 3 = 5</label></p>
<input type="hidden" name="submitted" value="1">
<p><input type="submit"></p>
</form>
</div>
</div><!-- .entry-content -->
</article><!-- #post -->
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php Starkers_Utilities::get_template_parts( array( 'parts/shared/footer','parts/shared/html-footer' ) ); ?>
检查日志后,我在联系页面上发现:
Notice: Undefined index: message_name in C:\xampp\htdocs\wordpress\wp-content\themes\starkers-master\page-contact-us.php on line 33
Notice: Undefined index: message_email in C:\xampp\htdocs\wordpress\wp-content\themes\starkers-master\page-contact-us.php on line 34
Notice: Undefined index: message_text in C:\xampp\htdocs\wordpress\wp-content\themes\starkers-master\page-contact-us.php on line 35
Notice: Undefined index: message_human in C:\xampp\htdocs\wordpress\wp-content\themes\starkers-master\page-contact-us.php on line 36
Notice: Undefined index: submitted in C:\xampp\htdocs\wordpress\wp-content\themes\starkers-master\page-contact-us.php on line 66
Notice: get_current_theme is deprecated since version 3.4! Use wp_get_theme() instead. in C:\xampp\htdocs\wordpress\wp-includes\functions.php on line 2841