Enter your keyword

Contact Form

How to add dynamic Author Emails in Contact Form 7

function author_email($tag)  {
$curauth = ( get_query_var( 'author_name' ) ) ? get_user_by( 'slug', get_query_var( 'author_name' ) ) : get_userdata( get_query_var( 'author' ) );
  $view = get_query_var( 'view' );
  $role = '';
  if ( in_array( 'author_role', $curauth->roles ) ) {
    $role = 'author_role';
  }
  $html = $curauth->user_email;
  return $html;
}
add_shortcode('authormail', 'author_email');

Use this Short Code in Contact form 7 => [dynamichidden email-author "authormail"] and use the contact Email shortcode  [email-author]

How to Validate Numbers in Contact Form 7

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

if ($name == 'phone' || $name == 'fax') { // Validation applies to these textfield names. Add more with || inbetween
$stripped = preg_replace( '/\D/', '', $_POST[$name] );
$_POST[$name] = $stripped;
if( strlen( $_POST[$name] ) != 10 ) { // Number string must equal this
$result['valid'] = false;
$result['reason'][$name] = $_POST[$name] = 'Please enter a 10 digit phone number.';
}
}
return $result;
}

add_filter( 'wpcf7_validate_text', 'is_number', 10, 2 );
add_filter( 'wpcf7_validate_text*', 'is_number', 10, 2 );

Add Checkbox to send mail copy to self in contact form7

Step1:

Add this function in theme functions.php

function check_mail_send_contactform($cf7) {
//get CF7's mail and posted_data objects
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
$posted_data = $submission->get_posted_data();
}
$mail = $cf7->prop( 'mail' );

if($posted_data['contact_sendmail'][0]) { //if Checkbox checked
$mail2 = $cf7->prop( 'mail_2′ ); //get CF7's mail_2 object
//now activate mail_2, the value 0 means not active
$mail2['active'] = 1;
$cf7->set_properties( array( 'mail_2′ => $mail2 ) );
}
return $cf7;
}
add_action('wpcf7_before_send_mail','check_mail_send_contactform');

 

Step2:

Add this Shortcode in Contact form:

/* This shortcode add checkbox in contact Form */
[checkbox contact_sendmail default:1 "Send a copy of this email to yourself"]

Step3:

UnCheck on Use mail (2) checkbox in contact form7 and put contact form mail field code into the To fields in Mail (2) Section.