Back to Blog

Contact Form 7 Tips, Tricks and Resources

Oct 11, 2021 Updated: Oct 11, 2021

Remove p and br tags

add_filter('wpcf7_autop_or_not', '__return_false');

Disable Email address validator

add_filter( 'wpcf7_validate_configuration', '__return_false' );

Add class to radio and checkbox

Source Code

add_filter('wpcf7_form_elements', function ($content) {
    $content = preg_replace('/<label><input type="(checkbox|radio)" name="(.*?)" value="(.*?)" \/><span class="wpcf7-list-item-label">/i', '<label class="custom-control custom-\1"><input type="\1" name="\2" value="\3" class="custom-control-input"><span class="wpcf7-list-item-label custom-control-label">', $content);
    return $content;
});

Custom Validation

Add validation using filter. The example below validates email input.

add_filter( 'wpcf7_validate_email*', 'custom_email_confirmation_validation_filter', 20, 2 );

function custom_email_confirmation_validation_filter( $result, $tag ) {
  if ( 'your-email-confirm' == $tag->name ) {
    $your_email = isset( $_POST['your-email'] ) ? trim( $_POST['your-email'] ) : '';
    $your_email_confirm = isset( $_POST['your-email-confirm'] ) ? trim( $_POST['your-email-confirm'] ) : '';

    if ( $your_email != $your_email_confirm ) {
      $result->invalidate( $tag, "Are you sure this is the correct address?" );
    }
  }
  return $result;
}

Save submissions to database

Use Contact Form Submissions plugin by Jason Green to save the contact form submissions along with file attachments.

Contact

Got A Question For Me?

Feel free to ask anything directly on call or fill the form and I will contact back within few hours.