Created
October 1, 2021 10:06
-
-
Save nfsarmento/02bce3eb8f43dcd34cef0903f438ff97 to your computer and use it in GitHub Desktop.
Custom contact form shortcode - used for custom product enquiry on WooCommerce
This file contains hidden or 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
/** | |
* | |
* Shortcode contact form | |
*/ | |
// @codingStandardsIgnoreStart | |
function decorativefair_shortcode_product_form($item) { | |
ob_start(); | |
global $post, $product; | |
$author_name = get_the_author_meta( 'user_email', $product->post->post_author ); | |
$producttitle = get_the_title($item['id']); | |
if(isset($_POST['submitted'])) { | |
if(trim($_POST['contactName']) === '') { | |
$nameError = 'Please enter your name.'; | |
$hasError = true; | |
} else { | |
$name = trim($_POST['contactName']); | |
} | |
if(trim($_POST['email']) === '') { | |
$emailError = 'Please enter your email address.'; | |
$hasError = true; | |
} else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) { | |
$emailError = 'You entered an invalid email address.'; | |
$hasError = true; | |
} else { | |
$email = trim($_POST['email']); | |
} | |
if(trim($_POST['phone']) === '') { | |
$phoneError = 'Please enter your phone.'; | |
$hasError = true; | |
} else { | |
$phone = trim($_POST['phone']); | |
} | |
if(trim($_POST['comments']) === '') { | |
$commentError = 'Please enter a message.'; | |
$hasError = true; | |
} else { | |
if(function_exists('stripslashes')) { | |
$comments = stripslashes(trim($_POST['comments'])); | |
} else { | |
$comments = trim($_POST['comments']); | |
} | |
} | |
if(!isset($hasError)) { | |
$emailTo = get_option('tz_email'); | |
if (!isset($emailTo) || ($emailTo == '') ){ | |
$emailTo = $author_name; | |
} | |
$subject = 'Decorative Fair Website - Product name: '.$producttitle; | |
//$body = "Name: $name \n\nEmail: $email \n\nComments: $comments"; | |
$body .= "Name: " . $name . "\n"; | |
$body .= "Email: " . $email . "\n"; | |
$body .= "Enquire: " . $comments . "\n"; | |
$body .= "Product: " . $producttitle . "\n"; | |
$headers = 'From: '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email; | |
$headers = [ | |
//'Content-Type: text/html; charset=UTF-8', | |
'Cc: Decorative Fair Team <[email protected]>', | |
]; | |
wp_mail($emailTo, $subject, $body, $headers); | |
$emailSent = true; | |
} | |
} | |
?> | |
<div class="product-contact-form-container"> | |
<?php if(isset($emailSent) && $emailSent == true) { ?> | |
<div class="thanks"> | |
<p>Thank you, your enquire was sent successfully.</p> | |
</div> | |
<?php } else { ?> | |
<?php if(isset($hasError) || isset($captchaError)) { ?> | |
<p class="error">Sorry, an error occured.<p> | |
<?php } ?> | |
<form action="<?php the_permalink(); ?>" id="contactForm" method="post"> | |
<div class="product-contact-form"> | |
<div class="product-form-name"> | |
<label class="form-label" for="contactName">Your name*</label> | |
<input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="required requiredField" /> | |
<?php if($nameError != '') { ?> | |
<span class="error"><?php echo $nameError; ?></span> | |
<?php } ?> | |
</div> | |
<div class="product-form-email"> | |
<label class="form-label" for="email">Your email address*</label> | |
<input type="text" name="email" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" class="required requiredField email" /> | |
<?php if($emailError != '') { ?> | |
<span class="error"><?php echo $emailError; ?></span> | |
<?php } ?> | |
</div> | |
<div class="product-form-telefone"> | |
<label class="form-label" for="phone">Your phone number*</label> | |
<input type="tel" id="phone" name="phone" value="<?php if(isset($_POST['phone'])) echo $_POST['phone'];?>" class="required requiredField phone" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}"> | |
<?php if($phoneError != '') { ?> | |
<span class="error"><?php echo $phoneError; ?></span> | |
<?php } ?> | |
</div> | |
<div class="product-form-message"> | |
<label class="form-label" for="commentsText">Your message</label> | |
<textarea name="comments" id="commentsText" rows="10" cols="10" class="required requiredField"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea> | |
<?php if($commentError != '') { ?> | |
<span class="error"><?php echo $commentError; ?></span> | |
<?php } ?> | |
</div> | |
<div class="product-form-privacy"> | |
<p class="product-form-privacy-text"> By clicking contact dealer, you accept our Privacy Policy.</p> | |
</div> | |
<div class="product-form-submit"> | |
<input class="product-form-submit-btn" type="submit" value="CONTACT DEALER"> | |
</div> | |
</div> | |
<input type="hidden" name="submitted" id="submitted" value="true" /> | |
</form> | |
<?php } ?> | |
</div> | |
<?php | |
return ob_get_clean(); | |
} | |
add_shortcode( 'productform', 'decorativefair_shortcode_product_form' ); | |
// @codingStandardsIgnoreEnd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment