Skip to content

Instantly share code, notes, and snippets.

@wp-user-manager
Last active February 10, 2023 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wp-user-manager/b06bac3f87539470cdaa6b66985c3035 to your computer and use it in GitHub Desktop.
Save wp-user-manager/b06bac3f87539470cdaa6b66985c3035 to your computer and use it in GitHub Desktop.
WP User Manager - Custom field validation server side with regex
<?php
add_filter( 'submit_wpum_form_validate_fields', function ( $check, $fields, $values, $form_name ) {
foreach ( $fields as $group_key => $group_fields ) {
$field_key = 'wpum_field_33'; // Change this to be your unique field ID with wpum_ prefix
if ( ! isset( $values[ $group_key ][ $field_key ] ) ) {
return $check;
}
$pattern = '/(a)(b)*(c)/'; // Change this to be your regex pattern
preg_match( $pattern, $values[ $group_key ][ $field_key ], $matches);
if ( empty( $matches ) ) {
return new WP_Error( 'validation-error', __( 'This field does not meet the requirement', 'wp-user-manager' ) ); // Change error message
}
}
return $check;
}, 10, 4 );
@wp-user-manager
Copy link
Author

Save this file to your /wp-content/mu-plugins/ directory (you might need to create the mu-plugins directory).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment