Set the Custom PHP Script BEFORE Register User Action!
In case of errors (duplicated DNI) it will stop next actions execution and display the custom error message.
Copy this script into the Custom PHP field
if ($ajax_handler->is_success) {
global $wpdb;
$sql = 'SELECT * FROM '.$wpdb->prefix.'usermeta WHERE meta_key = "dni" AND meta_value = "'.$fields['dni'].'"'; // set the wanted meta key, in combination with Register User
$count = $wpdb->get_results( $sql );
if (!empty($count)) {
wp_send_json_error([
'message' => 'A User with this DNI is already registered!', // customize error text
'data' => $ajax_handler->data,
]);
}
}