phpwordpressgravity-forms-plugin

How to cross-check a value of a Gravity Forms email input field with current user email address in WordPress?


I use Gravity Forms. I need to check if the given value in an email input field matches the current user's email address. For that, I am using the following code but the problem is when the field has CSS visibility property set to hidden the code doesn't validate the field. It let the user change the default value(current user email address) to a non-existing email address and submit the form. Apart from that, the code and the form are working properly.

add_filter( 'gform_field_validation_13_24', 'check_current_email', 10, 4 );
function check_current_email( $result, $value, $form, $field ) {
    if ( $field->type === 'email' ) {
        $user = wp_get_current_user();
  
        if ( empty( $value ) || ! email_exists( $value, $user->data->user_email, $user->ID ) ) {
            $result['is_valid'] = false;
            $result['message']  = 'Incorrect current user email. Please try again.';
        }
    }
  
    return $result;
}

Solution

  • To solve the issue please follow the steps. It's simple.

    We need to add a CSS class(e.g.: hidden-email) for the email field and add some custom CSS on the page where we want to display the form. In this case, the CSS code would be .hidden-email {display: none !important;}