Showing posts with label Contact Form 7. Show all posts
Showing posts with label Contact Form 7. Show all posts

Contact Form 7 Custom Validations - Wordpress




If you wanted to add custom Validations for Contact from 7 fields, here is the code
 But the name we use in Wordpress back-end should match with $name in the below code. I added validations for URL, Zip and Phone number

 For Custom URL use "url" - [text* url]
 For Zip Code use "ZipCode" - [text* ZipCode]
 For Phone Number we can use "'PhoneNumber1', 'PhoneNumber2', 'PhoneNumber3', 'PhoneNumber4', 'PhoneNumber5', 'PhoneNumber6', 'PhoneNumber7'"  - [text* PhoneNumber1]



<?php

// URL Validations
function get_valid_url( $url ) {

    $reg_cond = "((https?|ftp)\:\/\/)?";
$reg_cond .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?";
$reg_cond .= "(\:[0-9]{2,5})?";
    $reg_cond .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?";
    $reg_cond .= "([a-z0-9-.]*)\.([a-z]{2,3})";
    $reg_cond .= "(\/([a-z0-9+\$_-]\.?)+)*\/?";
    $reg_cond .= "(#[a-z_.-][a-z0-9+\$_.-]*)?";

    return preg_match("/^$reg_cond$/", $url);
}

function contact7_validations($result,$tag) {
$type = $tag['type'];
$name = $tag['name'];


if($type == 'text*' && $_POST[$name] == ''){
$result['valid'] = false;
$result['reason'][$name] = wpcf7_get_message( 'invalid_required' );
}



// Custom URL Validation
if($name == 'url') {
$url = $_POST['url'];

if($url != '') {
if(get_valid_url($url)){
$result['valid'] = true;
} else {
$result['valid'] = false;
$result['reason'][$name] = 'URL Entered is invalid.';
}
}
}


// Custom Zip Code Validation
if($name == 'ZipCode') {
$ZipCode = $_POST['ZipCode'];

if($ZipCode != '') {
if(!preg_match('/^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$/', $ZipCode)) {
if(!preg_match('/^\d{5}(-\d{4})?$/', $ZipCode)) {
$result['valid'] = false;
$result['reason'][$name] = 'Zipcode Entered is Invalid';
}
}
}
}

// Phone Validation

$PhoneNumber = array('PhoneNumber1', 'PhoneNumber2', 'PhoneNumber3', 'PhoneNumber4', 'PhoneNumber5', 'PhoneNumber6', 'PhoneNumber7');

foreach($PhoneNumber as $phoneValid) {
if($name == $phoneValid) {
$validPhone = $_POST[$phoneValid];

if($validPhone != '') {
if(preg_match('/^(?:1(?:[. -])?)?(?:\((?=\d{3}\)))?([2-9]\d{2})(?:(?<=\(\d{3})\))? ?(?:(?<=\d{3})[.-])?([2-9]\d{2})[. -]?(\d{4})(?: (?i:ext)\.? ?(\d{1,5}))?$/', $validPhone)
|| preg_match('/^([\+][0-9]{1,3}[\ \.\-])?([\(]{1}[0-9]{2,6}[\)])?([0-9\ \.\-\/]{3,20})((x|ext|extension)[\ ]?[0-9]{1,4})?$/', $validPhone)
&& strlen($validPhone) > 9
&& strlen($validPhone) < 30
&& (int)($validPhone)) {
} else {
$result['valid'] = false;
$result['reason'][$name] = 'Phone Number Entered is Invalid';
}
}
}
}


return $result;
}

//Filter For Text Field
add_filter('wpcf7_validate_text','contact7_validations', 10, 2);
add_filter('wpcf7_validate_text*', 'contact7_validations', 10, 2);
 ?>

Add a Download link or button after filling the contact from 7 using jQuery in wordpress

Step1:

1. Install "Contact From 7" WordPress plugin

Step2:

1. Create a from Dashboard - > Contact from 7 - > Add New
2. Copy the Short-code example:

[contact-form-7 id="658" title="Contact Form"]

Note: Change the short-code with yours

Step3:

1. Add Short-code to your page where you wanted to display your Contact from.
2. Inspect your page and you can find an div with id above the from which looks like

<div role="form" class="wpcf7" id="wpcf7-f656-o1" lang="en-US" dir="ltr">

Step4:

1. Save the ID , we will  use it as jQuery selector.

Now here comes the script

Before adding this script check weather you have included the jQuery Library before your script

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

// Ignore this if you have included the library.

<script>

$(document).ready(function() {

$downloadurl =  "http://www.webiste.com/download.pdf"; 

// File to be downloaded
// you can add this url dynimcally if you have multiple forms in different pages using ACF plugin

$('#wpcf7-f656-o1').find('.wpcf7-submit').on("click", function(){ 

// on Click of Submit button
// #wpcf7-f656-o1 is my form id which we copied earlier, use your id here

setTimeout(function()
{

if($('.wpcf7-response-output').hasClass('wpcf7-mail-sent-ok'))
{
$('.wpcf7-form').prepend("<a href="+$downloadurl+" class='download_link' target='_blank'>Download Pdf</a>");

$('.download_link').on("click", function(){

// To reload the page after downlading the file to reset the from for next download.

location.reload(true);
});
}
},2000)

// Timeout for 2 seconds

});
});

</script>


Custom Captcha for Contact Form 7

Custom Captcha for Contact From 7 in wordpress is so simple . At present Contact From 7 uses "Really Simple Captcha" plugin to display a Captcha. I will us use the same plugin , But with some minor modifications.

Step 1: Install Contact Form 7 plugin
Step 2: Install Really Simple Captcha plugin
Step 3: We will start with editing Contact Form 7. Open contact-form-7\modules\captcha.php from your Plugins Folder.

Go to line #36  and replace the array with below code.

$op = array(
'img_size' => array( 150, 24 ),
'base' => array( 6, 18 ),
'font_size' => 14,
'font_char_width' => 15 );

If you are using a Multi Site you can control Captcha for specific website using the code shown below.

$current_blog_id = $GLOBALS['blog_id'];
if ($current_blog_id == 120) { // Change your Blog Id
$op = array( // Default
'img_size' => array( 150, 24 ),
'base' => array( 6, 18 ),
'font_size' => 14,
'font_char_width' => 15 );
}else{
$op = array(
'img_size' => array( 72, 24 ),
'base' => array( 6, 18 ),
'font_size' => 14,
'font_char_width' => 15);
}

Step4: Now add the below code into your theme's function.php to disable updates

function filter-plugin_updates ($value){
unset($value ->response['contact-form-7/wp-contact-form-7.php']);
unset($value ->response['really-simple-captcha/really-simple-captcha.php']);
return $value;
}
add_filter('site_transient_update_plugins', 'filter-plugin_updates' );

Step5: Open "really-simple-captcha.php" from Really Simple Captcha Plugin Folder.

Go to line #39 and replace with code below

$this->char_length = 8; // Edit length of Captcha (I am displaying 8 characters)

If you are using a Multi Site use this code
$current_blog_id = $GLOBALS['blog_id'];

if ($current_blog_id == 120) { // Edit your Blog id
$this->char_length = 8;
}else{
$this->char_length = 4;
}

Step6:

Go to line #52 and replace with the code below

$this->img_size = array( 150, 24 ); // height and width of Cpatch image

If you are using a Multi Site use this code

if ($current_blog_id == 120) {
$this->img_size = array( 150, 24 ); // height and width of Cpatch image
}else{
$this->img_size = array( 72, 24 );
}

Step7: Done !!