WP User Manager - Custom field validation server side with regex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Save this file to your /wp-content/mu-plugins/ directory (you might need to create the mu-plugins directory).