Advertisement
ralrom

Broken Version

May 20th, 2013
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.33 KB | None | 0 0
  1. //CONTACT.PHP
  2.  
  3. <?php
  4. /*
  5. Plugin Name: Ralrom Simple Contact Form
  6. Plugin URI: Website
  7. Description: Creates a contact form on the page using wordpress shortcode API. Inspired by wptuts.
  8. Version: 1.0
  9. Author: Robert Al-Romhein
  10. Author URI: Website
  11. */
  12.  
  13. $path = plugin_dir_url(__FILE__);
  14.  
  15. function get_sender_ip() {
  16.     if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
  17.         return $_SERVER["HTTP_X_FORWARDED_FOR"];
  18.     }
  19.     elseif (isset($_SERVER["HTTP_CLIENT_IP"])) {
  20.         return $_SERVER["HTTP_CLIENT_IP"];
  21.     }
  22.     else {
  23.         return $_SERVER["REMOTE_ADDR"];
  24.     }
  25. }
  26.  
  27. function contact_form_sc(){
  28.     //shortcode attributes
  29.     extract(shortcode_atts(array(
  30.         "email" => get_bloginfo('admin_email'),
  31.         "dreamhost_email" => "email@email.com",
  32.         "subject" => "",
  33.         "label_name" => "Name:",
  34.         "label_email" => "Email:",
  35.         "label_subject" => "Subject:",
  36.         "label_message" => "Message:",
  37.         "label_submit" => "Send",
  38.         "error_empty" => "Please fill in all the fields.",
  39.         "error_noemail" => "Please enter a valid e-mail address.",
  40.         "success" => "Thank you for your e-mail! I'll get back to you shortly."
  41.     ), $atts));
  42.    
  43.     $email_form = '
  44.     <div id="contact"><a name="contact"></a>
  45.     <div id="intro">
  46.     <h1>Nice to meet you</h1>
  47.     What would you like?
  48.  
  49.     </div>
  50.     <!--intro-->
  51.  
  52.     <form method ="post" action="' . $path."validate.php" . '">
  53.     <div class="field"><label for="cf_name">'.$label_name.'</label>
  54.     <input id="cf_name" tabindex="1" type="text" name="client_name" value="Enter your name" required/></div>
  55.     <!--field-->
  56.     <div class="field"><label for="cf_email">' . $label_email . '</label>
  57.     <input id="cf_email" tabindex="2" type="email" name="email" value="Enter your e-mail address" required/></div>
  58.     <!--field-->
  59.     <div class="field"><label for="cf_subject">' . $label_subject . '</label>
  60.     <input id="cf_subject" tabindex="3" type="text" name="subject" value="Enter your subject" required/></div>
  61.     <!--field-->
  62.     <div class="field"><label for="cf_message">' . $label_message . '</label>
  63.     <textarea id="cf_message" tabindex="4" cols="30" name="message" rows="10" required></textarea></div>
  64.     <!--field-->
  65.     <input class="send" tabindex="4" type="submit" name="submit" value="' . $label_submit . '" />
  66.  
  67.     </form></div>';
  68.    
  69.     return $info.$email_form;
  70. }
  71.  
  72. add_shortcode('contact', 'contact_form_sc');
  73.  
  74.  
  75.  
  76. //VALIDATE.PHP
  77.  
  78. <?php
  79.  
  80.     if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  81.         $error = false;
  82.        
  83.         foreach ($_POST as $field => $value) {
  84.             if(!isset($field)){
  85.                 $error = true;
  86.                 $result = $error_empty;
  87.             }
  88.         }
  89.        
  90.         //sanitize all fields
  91.         $s_name = sanitize_text_field($_POST["client_name"]);
  92.         $s_subject = sanitize_text_field($_POST["subject"]);
  93.         $s_message = esc_textarea($_POST["message"]);
  94.        
  95.         //sanitize email
  96.         if(is_email($_POST["email"])){
  97.             $s_email = is_email($_POST["email"]);
  98.         } else {
  99.             $error = true;
  100.             $result = $error_noemail;
  101.         }
  102.         //send email if no error
  103.         if(!$error){
  104.             $email_subject = "[" . get_bloginfo('name') . "] " . $s_subject;
  105.             $email_message = "Sender IP: " . get_sender_ip() . "\nFrom: " . $s_name . "\nReply to: " . $s_email . "\nMessage:\n\n" . $s_message;
  106.             $headers[] = 'From: RalRom Contact Form <email@email.com>';
  107.             $headers[] = 'Cc: Contact RalRom <email@email.com>';
  108.             wp_mail($email, $email_subject, $email_message, $headers);
  109.             $result = "<h1>" . $success . "</h1>";
  110.             $sent = true;
  111.         }
  112.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement