Contact form 7 custom validation

<?php

function get_valid_url( $url ) {

$regex = “((https?|ftp)\:\/\/)?”; // Scheme
$regex .= “([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?”; // User and Pass
$regex .= “([a-z0-9-.]*)\.([a-z]{2,3})”; // Host or IP
$regex .= “(\:[0-9]{2,5})?”; // Port
$regex .= “(\/([a-z0-9+\$_-]\.?)+)*\/?”; // Path
$regex .= “(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?”; // GET Query
$regex .= “(#[a-z_.-][a-z0-9+\$_.-]*)?”; // Anchor

return preg_match(“/^$regex$/”, $url);

}
/*
Name Field VALIDATIONS
*/
function cf7_custom_form_validation($result,$tag) {
$type = $tag[‘type’];
$name = $tag[‘name’];
if($type == ‘text*’ && $_POST[$name] == ”){
$result[‘valid’] = false;
$result[‘reason’][$name] = wpcf7_get_message( ‘invalid_required’ );
}
$allNames = array(‘fullName’, ‘fullName1’);
foreach($allNames as $uniNames) {
if($name == $uniNames) {
$fullName = $_POST[$uniNames];
if($fullName != ” || $fullName == ”) {
if(!preg_match(‘/^[A-Z0-9][a-zA-Z0-9 ]+$/i’, $fullName)) {
$result[‘valid’] = false;
$result[‘reason’][$name] = ‘Please Enter a Valid Name’;
}

if(is_numeric($fullName)){
$result[‘valid’] = false;
$result[‘reason’][$name] = ‘Please Enter a Valid Name’;
}
}

}
}
return $result;
}
add_filter(‘wpcf7_validate_text’,’cf7_custom_form_validation’, 10, 2); // text field
add_filter(‘wpcf7_validate_text*’, ‘cf7_custom_form_validation’, 10, 2); // Req. text field
/*
TEXTAREA VALIDATIONS
*/

function cf7_custom_textarea_validation($result, $tag) {
$type = $tag[‘type’];
$name = $tag[‘name’];
if($type == ‘textarea*’ && $_POST[$name] == ”){
$result[‘valid’] = false;
$result[‘reason’][$name] = wpcf7_get_message( ‘invalid_required’ );
}
if($name == ‘addressTa’){
$addressTa = $_POST[‘addressTa’];

if($addressTa == ”) {
if(!preg_match(‘/^[A-Z0-9][a-zA-Z0-9 ]+$/i’, $fullName)) {
$result[‘valid’] = false;
$result[‘reason’][$name] = ‘Please Enter Your Comment’;
}
}
if($addressTa != ” ) {
if(strlen($addressTa) > 300) {
$result[‘valid’] = false;
$result[‘reason’][$name] = ‘You should not exceed 300 characters length’;
}
}
}
return $result;
}
add_filter( ‘wpcf7_validate_textarea’, ‘cf7_custom_textarea_validation’, 10, 2 );
add_filter( ‘wpcf7_validate_textarea*’, ‘cf7_custom_textarea_validation’, 10, 2 );

function cf7_custom_email_validation($result, $tag) {
$type = $tag[‘type’];
$name = $tag[‘name’];

if($type == ’email*’ && $_POST[$name] == ”){
$result[‘valid’] = false;
$result[‘reason’][$name] = wpcf7_get_message( ‘invalid_required’ );
}

if($name == ’emailAddress’) {
$emailAddress = $_POST[’emailAddress’];

if($emailAddress == ”) {
if(!preg_match(‘/^[A-Z0-9][a-zA-Z0-9 ]+$/i’, $fullName)) {
$result[‘valid’] = false;
$result[‘reason’][$name] = ‘Please Enter A Valid Email’;
}
}

if($emailAddress != ”) {
if(substr($emailAddress, 0, 1) == ‘.’ || !preg_match(‘/^([*+!.&#$¦\’\\%\/0-9a-z^_`{}=?~:-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,4})$/i’, $emailAddress)) {
$result[‘valid’] = false;
$result[‘reason’][$name] = ‘Entered Email is Invalid.’;
}
}
}

return $result;

}
add_filter( ‘wpcf7_validate_email’, ‘cf7_custom_email_validation’, 10, 2 );
add_filter( ‘wpcf7_validate_email*’, ‘cf7_custom_email_validation’, 10, 2 );
?>

Gravity form Custom Validation

<?php
add_filter(‘gform_validation_1’, ‘custom_validation’);
function custom_validation($validation_result){
$form = $validation_result[“form”];
foreach($form[‘fields’] as &$field){
if($_POST[‘input_1’] == “”){
// set the form validation to false
$validation_result[“is_valid”] = false;
if($field[“id”] == “1”){
$field[“failed_validation”] = true;
$field[“validation_message”] = “Please Enter Your Name.”;
}
}
if ($_POST[‘input_2’] == “”){
// set the form validation to false
$validation_result[“is_valid”] = false;
if($field[“id”] == “2”){
$field[“failed_validation”] = true;
$field[“validation_message”] = “Please Enter Your Email Address”;
}
}

if ($_POST[‘input_3’] == “”){
// set the form validation to false
$validation_result[“is_valid”] = false;
if($field[“id”] == “3”){
$field[“failed_validation”] = true;
$field[“validation_message”] = “Please Enter Your Phone Number”;
}
}

} // main
//Assign modified $form object back to the validation result
$validation_result[“form”] = $form;
return $validation_result;
}?>

Replace Default Product image form woocommerce

add_action( ‘init’, ‘custom_fix_thumbnail’ );
function custom_fix_thumbnail() {
add_filter(‘woocommerce_placeholder_img_src’, ‘custom_woocommerce_placeholder_img_src’);
function custom_woocommerce_placeholder_img_src( $src ) {
$upload_dir = wp_upload_dir();
$uploads = untrailingslashit( $upload_dir[‘baseurl’] );
$src = $uploads . ‘/2013/11/placeholder.png’;
return $src;
}
}

Call woocommerce CSS form theme

function jk_enqueue_woocommerce_css(){
wp_register_style( ‘woocommerce’, get_template_directory_uri() . ‘/css/woocommerce.css’ );
if ( class_exists( ‘woocommerce’ ) ) {
wp_enqueue_style( ‘woocommerce’ );
}
}
add_action( ‘wp_enqueue_scripts’, ‘jk_enqueue_woocommerce_css’ );