Contact Form Shortcode

<?php
/*
Plugin Name:Contact Form Shortcode
Plugin URI: https://mayanksharma88.wordpress.com/
Description: A simple contact form for simple needs. Usage: <code>[contact email=”your@email.address”]</code>
Version: 1.0
Author: Mayank Sharma
Author URI: https://mayanksharma88.wordpress.com/
*/

// This line of comment holds the place of the amazingly simple code we’re going to write. So you don’t really need to read this.

function wptuts_get_the_ip() {
if (isset($_SERVER[“HTTP_X_FORWARDED_FOR”])) {
return $_SERVER[“HTTP_X_FORWARDED_FOR”];
}
elseif (isset($_SERVER[“HTTP_CLIENT_IP”])) {
return $_SERVER[“HTTP_CLIENT_IP”];
}
else {
return $_SERVER[“REMOTE_ADDR”];
}
}

// the shortcode
function dreamsdesign_contact($atts) {
extract(shortcode_atts(array(
“email” => get_bloginfo(‘admin_email’),
“subject” => ‘WebSite Form’,
“label_name” => ‘Your Name’,
“label_email” => ‘Your E-mail Address’,
“label_subject” => ‘Subject’,
“label_message” => ‘Your Message’,
“label_submit” => ‘lets get started’,
“error_empty” => ‘Please fill in all the required fields.’,
“error_noemail” => ‘Please enter a valid e-mail address.’,
“success” => ‘Thanks for your e-mail! We\’ll get back to you as soon as we can.’
), $atts));

if ($_SERVER[‘REQUEST_METHOD’] == ‘POST’) {
$error = false;
$required_fields = array(“your_name”, “company”, “email”, “contact”, “message”);

foreach ($_POST as $field => $value) {
if (get_magic_quotes_gpc()) {
$value = stripslashes($value);
}
$form_data[$field] = strip_tags($value);
}

foreach ($required_fields as $required_field) {
$value = trim($form_data[$required_field]);
if(empty($value)) {
$error = true;
$result = $error_empty;
}
}

if(!is_email($form_data[’email’])) {
$error = true;
$result = $error_noemail;
}

if ($error == false) {
$email_subject = “[” . get_bloginfo(‘name’) . “] ” . $form_data[‘subject’];
$email_message =”Company Name:” .$form_data[‘company’] . “\n\nContact NO.: ” . $form_data[‘contact’].”\nMessage: ” .$form_data[‘message’] . “\n\nIP: ” . wptuts_get_the_ip();
$headers = “From: “.$form_data[‘your_name’].” <“.$form_data[’email’].”>\n”;
$headers .= “Content-Type: text/plain; charset=UTF-8\n”;
$headers .= “Content-Transfer-Encoding: 8bit\n”;
wp_mail($email, $email_subject, $email_message, $headers);
$result = $success;
$sent = true;
}
}

if($result != “”) {
$info = ‘

‘.$result.’

‘;
}
$email_form = ‘

– Select Project Type –
E-Commerce Website
CMS Website
Dynamic Website
Static Website

– Select Your Budget –
$200 – $500
$500 – $1000
Custom Order
I am Not Sure

‘.$form_data[‘message’].’

</form></div></div>’;

if($sent == true) {
return $info;
} else {
return $info.$email_form;
}
}
add_shortcode(‘contact_form’, ‘my_contact’);

?>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s