Ovifdiu COmrasdd
Ovifdiu COmrasdd

Reputation: 55

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. 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("[email protected]", $subject, $message, $from);
                        echo "Email sent!";
                        }
                    }  
                ?> 

Can you tell me were i am failing? And why?

Upvotes: 1

Views: 169

Answers (2)

galfaro
galfaro

Reputation: 208

<?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 = "[email protected]";
      $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');
?>

Upvotes: 1

nim
nim

Reputation: 507

you should also add the header variable with below value, if you use the html in message:

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

Upvotes: 0

Related Questions