Skip to content

Instantly share code, notes, and snippets.

@gicolek
Created October 7, 2014 08:47
Show Gist options
  • Save gicolek/fcfd0f50ea7c13c0da6e to your computer and use it in GitHub Desktop.
Save gicolek/fcfd0f50ea7c13c0da6e to your computer and use it in GitHub Desktop.
This hooks validates the Gravity Forms form submissions checking if the user, has already been registered in the database.
<?php
// remember, the hook suffix, should contain the form id!
add_filter( "gform_field_validation_3", 'gf_custom_validation', 10, 4 );
/**
* Custom GF validation function used for pagination and required fields
*
* @return string
*/
function gf_custom_validation($result, $value, $form, $field) {
$classes = explode( ' ', $field['cssClass'] );
// lets check if the user with such a username is already in the database
if ( in_array( 'username', $classes ) ) {
$user_id = username_exists( $value );
if ( $user_id ) {
// let's mark this field is invalid
$result['is_valid'] = false;
$result['message'] = 'This username is already taken.';
} else {
// let's mark this field as valid
$result['is_valid'] = true;
}
}
// let's check if the user with such an email has already been created
if ( in_array( 'email', $classes ) ) {
if ( email_exists( $value ) != false ) {
// let's mark this field as invalid
$result['message'] = 'This email is already taken.';
$result['is_valid'] = false;
} else {
// let's mark this field as valid
$result['is_valid'] = true;
}
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment