Created
February 24, 2014 14:55
-
-
Save mayoralito/9189776 to your computer and use it in GitHub Desktop.
Form to include dynamically into your post with shortcode.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function drawContactForm($contactFor = 'personal'){ | |
ob_start(); | |
$randomName = generateRandomString(); | |
?> | |
<form name="form-<?php echo $randomName; ?>" action="" method="post"> | |
<div id="comment-input"> | |
<div class="row"> | |
<label><?php echo __('First Name', 'Avada'); ?> <span>*</span> | |
<input type="text" name="first_name" id="author" value="<?php if(isset($_POST['first_name']) && !empty($_POST['first_name'])) { echo $_POST['first_name']; } ?>" size="22" tabindex="1" aria-required="true" class="input-name"> | |
</label> | |
<label><?php echo __('Last Name', 'Avada'); ?> <span>*</span> | |
<input type="text" name="last_name" id="last_name" value="<?php if(isset($_POST['last_name']) && !empty($_POST['last_name'])) { echo $_POST['last_name']; } ?>" size="22" tabindex="2" aria-required="true" class="input-name"> | |
</label> | |
</div> | |
<div class="row"> | |
<label><?php echo __('Email', 'Avada'); ?> <span>*</span> | |
<input type="text" name="email" id="email" value="<?php if(isset($_POST['email']) && !empty($_POST['email'])) { echo $_POST['email']; } ?>" size="22" tabindex="2" aria-required="true" class="input-name"> | |
</label> | |
<label><?php echo __('Company', 'Avada'); ?> <span>*</span> | |
<input type="text" name="company" id="company" value="<?php if(isset($_POST['company']) && !empty($_POST['company'])) { echo $_POST['company']; } ?>" size="22" tabindex="3" class="input-name"> | |
</label> | |
</div> | |
<div class="row"> | |
<label><?php echo __('Phone Number', 'Avada'); ?> <span>*</span> | |
<input type="tel" name="phone_number" id="phone_number" value="<?php if(isset($_POST['phone_number"']) && !empty($_POST['phone_number"'])) { echo $_POST['phone_number"']; } ?>" size="22" tabindex="3" class="input-name"> | |
</label> | |
<label><?php echo __('Alternate Phone Number', 'Avada'); ?> | |
<input type="tel" name="phone_alternate" id="phone_alternate" value="<?php if(isset($_POST['phone_alternate']) && !empty($_POST['phone_alternate'])) { echo $_POST['phone_alternate']; } ?>" size="22" tabindex="3" class="input-name"> | |
</div> | |
</div> | |
<div id="comment-textarea"> | |
<div class="row"> | |
<label><?php echo __('Message', 'Avada'); ?> <span>*</span></label> | |
<textarea name="msg" id="comment" cols="39" rows="4" tabindex="4" class="textarea-comment"><?php if(isset($_POST['msg']) && !empty($_POST['msg'])) { echo $_POST['msg']; } ?></textarea> | |
</div> | |
</div> | |
<div id="comment-submit"> | |
<p><div><input name="submit" type="submit" id="submit" tabindex="5" value="<?php echo __('Submit', 'Avada'); ?>" class="comment-submit button small green"></div></p> | |
</div> | |
<input type="hidden" name="contactTo" value="<?php echo $contactFor; ?>"> | |
</form> | |
<?php | |
$output = ob_get_contents(); | |
ob_end_clean(); | |
return $output; | |
} | |
function generateRandomString($length = 10) { | |
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
$randomString = ''; | |
for ($i = 0; $i < $length; $i++) { | |
$randomString .= $characters[rand(0, strlen($characters) - 1)]; | |
} | |
return $randomString; | |
} | |
// Add Shortcode | |
function basic_contact_form_shortcode( $atts ) { | |
// Attributes | |
extract( shortcode_atts( | |
array( | |
'email_address_to' => '', | |
'subject_prefix'=> '', | |
), $atts ) | |
); | |
//If the form is submitted | |
if(isset($_POST['submit']) && $subject_prefix == trim($_POST['contactTo'])) { | |
//Check to make sure that the name field is not empty | |
if(trim($_POST['first_name']) == '') { | |
$hasError = true; | |
} else { | |
$first_name = trim($_POST['first_name']); | |
} | |
if(trim($_POST['last_name']) == '') { | |
$hasError = true; | |
} else { | |
$last_name = trim($_POST['last_name']); | |
} | |
//Subject field is not required | |
$subject = 'Contact from [' . $subject_prefix . ']'; | |
//Check to make sure sure that a valid email address is submitted | |
if( trim($_POST['email']) == '') { | |
$hasError = true; | |
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) { | |
$hasError = true; | |
} else { | |
$email = trim($_POST['email']); | |
} | |
if(trim($_POST['company']) == '') { | |
$hasError = true; | |
} else { | |
$company = trim($_POST['company']); | |
} | |
if(trim($_POST['phone_number']) == '') { | |
$hasError = true; | |
} else { | |
$phone_number = trim($_POST['phone_number']); | |
} | |
if(trim($_POST['phone_alternate']) == '') { | |
$hasError = true; | |
} else { | |
$phone_alternate = trim($_POST['phone_alternate']); | |
} | |
//Check to make sure comments were entered | |
if(trim($_POST['msg']) == '') { | |
$hasError = true; | |
} else { | |
if(function_exists('stripslashes')) { | |
$comments = stripslashes(trim($_POST['msg'])); | |
} else { | |
$comments = trim($_POST['msg']); | |
} | |
} | |
//If there is no error, send the email | |
if(!isset($hasError)) { | |
$emailTo = $email_address_to; //Put your own email address here | |
$body = __('First Name:', 'Avada')." $first_name \n\n"; | |
$body .= __('Last Name:', 'Avada')." $last_name \n\n"; | |
$body .= __('Email:', 'Avada')." $email \n\n"; | |
$body .= __('Company:', 'Avada')." $company \n\n"; | |
$body .= __('Phone Number:', 'Avada')." $phone_number \n\n"; | |
$body .= __('Alternate Phone Number:', 'Avada')." $phone_alternate \n\n"; | |
$body .= __('Message:', 'Avada')."\n $comments"; | |
$headers .= 'Reply-To: ' . $first_name .' ' . $last_name . ' <' . $email . '>' . "\r\n"; | |
$mail = wp_mail($emailTo, $subject, $body, $headers); | |
$emailSent = true; | |
} | |
} | |
if(isset($hasError)) { //If errors are found | |
echo '<div class="alert error"><div class="msg">'; | |
echo __("Please check if you've filled all the fields with valid information. Thank you.", 'Avada'); | |
echo '</div></div><br />'; | |
} | |
if(isset($emailSent) && $emailSent == true) { //If email is sent | |
echo '<div class="alert success"><div class="msg"> '; | |
echo __('Thank you', 'Avada'); | |
echo ' <strong>'.$first_name.'</strong> '; | |
echo __(' for using our contact form ('.$subject_prefix.')! Your email was successfully sent!', 'Avada'); | |
echo '</div></div><br />'; | |
} | |
// Code | |
return drawContactForm($subject_prefix); | |
} | |
add_shortcode( 'basic_contact_form', 'basic_contact_form_shortcode' ); | |
/** | |
* | |
* How to use: | |
* 1.- include contact-form-shortcode.php into your function theme file. | |
* 2.- Create new page or post and include: | |
* | |
* [basic_contact_form email_address_to="[email protected]" subject_prefix="Goes into subject email"] | |
* | |
* 3.- that's it. | |
* | |
* | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment