简体   繁体   English

联系表格WordPress无法正常工作

[英]Contact form wordpress not working

I tried to make a contact form for a website made in WP and i added the following code to the theme and i can't figure it out if i did it in the right way. 我试图为使用WP制作的网站制作联系表格,并且将以下代码添加到主题中,但是如果我以正确的方式进行操作,我将无法确定。 So i am using the following code: 所以我正在使用以下代码:

<?php
                $action=$_REQUEST['action'];
                if ($action=="")    /* display the contact form */
                    {
                    ?>
                        <form method="post" action="#">
                        <input type="hidden" name="action" value="submit">
                            <div class="row 50%">
                                <div class="6u 12u(mobile)"><input type="text" name="name" placeholder="Nume" /></div>
                                <div class="6u 12u(mobile)"><input type="email" name="email" placeholder="Email" /></div>
                            </div>
                            <div class="row 50%">
                                <div class="12u"><textarea name="message" placeholder="Mesajul tau catre noi..." rows="6"></textarea></div>
                            </div>
                            <div class="row">
                                <div class="12u">
                                    <ul class="actions">
                                        <li><input type="submit" value="Trimite Mesajul" /></li>
                                    </ul>
                                </div>
                            </div>
                        </form>

                          <?php
                    } 
                else                /* send the submitted data */
                    {
                    $name=$_REQUEST['name'];
                    $email=$_REQUEST['email'];
                    $message=$_REQUEST['message'];
                    if (($name=="")||($email=="")||($message==""))
                        {
                        echo "All fields are required, please fill <a href=\"\">the form</a> again.";
                        }
                    else{        
                        $from="From: $name<$email>\r\nReturn-path: $email";
                        $subject="Message sent using your contact form";
                        mail("myemail@gmail.com", $subject, $message, $from);
                        echo "Email sent!";
                        }
                    }  
                ?> 

Can you tell me were i am failing? 你能告诉我我失败了吗? And why? 又为什么呢?

<?php
/*
Plugin Name: Simple Contact Form
Plugin URI: 
Description: This is a simple contact form
Author: Gabriel Alfaro
Version: 1.0
Author URI: 
License: GPLv2
*/

function custom_form_setup(){
  $nonce = wp_create_nonce( 'form-submit' );
  ?>

  <form action="" method="post" enctype="multipart/form-data">
    <input type="text" name="custom_name" placeholder="Name">
    <input type="email" name="custom_email" placeholder="Email">
    <textarea name="custom_message" placeholder="Mesajul tau cartr noi...."></textarea>
    <input type="submit" value="Trimite Mesajui">
    <input type="hidden" name="custom_nounce" value="<?php echo $nonce; ?>">
    <input type="hidden" name="custom_form" value="true">
  </form>

  <?php
  if($_POST['custom_form']){
    if( ! wp_create_nonce( $nonce, 'form-submit' ) ){
      // This nounce is not valid
      die ( 'Security check' );
    }else{
      $name    = sanitize_text_field($_POST['custom_name']);
      $email   = sanitize_text_field($_POST['custom_email']);
      $message = sanitize_text_field($_POST['custom_message']);

      $to = "myemail@gmali.com";
      $subject = "Message sent using your contact form";
      $message = $message;
      $headers = "From: $name<$email>\r\nReturn-path: $email";
      mail($to, $subject, $message, $headers);

      echo "<br/ >Your message was successfully sent";
    }
  }

}
add_shortcode('custom-form', 'custom_form_setup');
?>

you should also add the header variable with below value, if you use the html in message: 如果您在消息中使用html,还应该添加标头变量,使其具有以下值:

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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