需要自定义WordPress表单才能发送电子邮件正文中的所有字段

时间:2018-10-03 19:19:57

标签: php wordpress forms

我一直在寻找网站的HTML5兼容和可访问表单插件,但是那里没有一个插件,所以我最终自己编写了一个代码(根据我在这里找到的信息)。它可以工作,但是我希望能够在提交时将所有字段发送到电子邮件的正文中。这只是从文本区域发送消息。我不是PHP方面的专家,所以答案对我来说并不明显。感谢您的帮助!

<pre>
<?php
function html_form_code() 
{
 ?>

 <form action="<?php esc_url($_SERVER['REQUEST_URI']); ?>" method="post">
   <label for="url" class="hidden">URL</label>
     <input type="url" name="url" id="url" class="hidden" />
   <label for="cf-name">Name*</label>
     <input type="text" name="cf-name" id="cf-name" placeholder="Name" pattern="[^.]*" value="<?php isset($_POST['cf-name']) ? esc_attr($_POST['cf-name']) : ''; ?>" />
   <label for="cf-email">Email*</label>
     <input type="email" name="cf-email" id="cf-email" placeholder="Example: name@domain.com" value="<?php isset($_POST['cf-email']) ? esc_attr($_POST['cf-email']) : ''; ?>" />
   <label for="cf-phone">Phone</label>
     <input type="tel" name="cf-phone" id="cf-phone" placeholder="123-456-7890" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}" />
   <label for="cf-message">How Can We Help You?*</label>
     <textarea rows="4" name="cf-message" id="cf-message" placeholder="Your Message"><?php isset($_POST['cf-message']) ? esc_attr($_POST['cf-message']) : ''; ?></textarea>
   <input type="submit" name="cf-submitted" value="SEND"/>
 </form>

 <?php
}

// Form validation
function my_validate_form()
{
     $errors = new WP_Error();

     if (isset($_POST[ 'url' ]) && $_POST[ 'url' ] !== '') {
         $errors->add('cheater', 'Ignore this text box. It is used to detect spammers. If you enter anything into it, your message will not be sent.');
     }

     if (isset($_POST[ 'cf-name' ]) && $_POST[ 'cf-name' ] == '') {
         $errors->add('name_error', 'Please tell us your name.');
     }

     if (isset($_POST[ 'cf-email' ]) && $_POST[ 'cf-email' ] == '') {
         $errors->add('email_error', 'Please fill in a valid email.');
     }

     if (isset($_POST[ 'cf-message' ]) && $_POST[ 'cf-message' ] == '') {
         $errors->add('message_error', 'Please send us a message.');
     }
    return $errors;
}


	// Form delivery
function deliver_mail($args = array())
{

  $defaults = array(
    'name' => '',
    'email' => '',
    'phone' => '',
    'to' => get_option('admin_email'), // can change this to an email address
  );

     $args = wp_parse_args($args, $defaults);
     $headers = "From: {$args['name']}  <{$args['email']}>"."\r\n";
     $subject = "Website Inquiry";

  // Send email returns true on success, false otherwise
 if (wp_mail($args['to'], $subject, $args['message'], $headers)) {
      return;
  } else {
      return false;
  }
}

// Form Sanitize
function my_sanitize_field($input)
{
    return trim(stripslashes(sanitize_text_field($input)));
}

// Success Message
function my_form_message()
{
    global $errors;
    if (is_wp_error($errors) && empty($errors->errors)) {
        echo '<div class="cf-success">';
        echo '<p>Thank you for contacting us '.$_POST['cf-name'].', we will be in touch with you soon.</p>';
        echo '</div>';

    //Empty $_POST because we already sent email
    $_POST = '';
    } else {
        if (is_wp_error($errors) && !empty($errors->errors)) {
            $error_messages = $errors->get_error_messages();
            foreach ($error_messages as $k => $message) {
                echo '<div class="cf-error '.$k.'">';
                echo '<p>'.$message.'</p>';
                echo '</div>';
            }
        }
    }
}



// Form shortcode
add_shortcode('gr_contact_form', 'cf_shortcode');
function cf_shortcode()
{
    ob_start();

    my_form_message();
    html_form_code();

    return ob_get_clean();
}

// Error validation
add_action('init', 'my_cf_form');
function my_cf_form()
{
    if (isset($_POST['cf-submitted'])) {
        global $errors;
        $errors = my_validate_form();
        if (empty($errors->errors)) {
            $args = array(
         'name' => my_sanitize_field($_POST['cf-name']),
         'email' => my_sanitize_field($_POST['cf-email']),
         'message' => my_sanitize_field($_POST['cf-message']),
       );
            deliver_mail($args);
        } else {
            return $errors;
        }
    }
}
?>
</pre>

0 个答案:

没有答案