Advertisement
ralrom

Working Version

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