Skip to content

Instantly share code, notes, and snippets.

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 plugin-republic/c1030eb31231682df5974fef624131be to your computer and use it in GitHub Desktop.
Save plugin-republic/c1030eb31231682df5974fef624131be to your computer and use it in GitHub Desktop.
<?php
/**
* Validate a text string to check first character is a letter and 2nd to 7th characters are numeric
*/
function prefix_filter_validate_text_string( $passed, $post, $item ) {
// Change the field ID as necessary
if( isset( $item['field_id'] ) && $item['field_id'] == 4904 ) {
$string = $post[$item['id']];
$first_letter = substr( $string, 0, 1 );
$six_letters = substr( $string, 1, 6 );
if( ! ctype_alpha( $first_letter ) ) {
$passed = false;
wc_add_notice( apply_filters( 'pewc_filter_validation_notice', esc_html( $item['field_label'] ) . __( ' must have a letter as the first character.', 'pewc' ), $item['field_label'], $item ), 'error' );
}
if( ! ctype_digit( $six_letters ) ) {
$passed = false;
wc_add_notice( apply_filters( 'pewc_filter_validation_notice', esc_html( $item['field_label'] ) . __( ' must have numbers as the second to seventh characters.', 'pewc' ), $item['field_label'], $item ), 'error' );
}
}
return $passed;
}
add_filter( 'pewc_filter_validate_cart_item_status', 'prefix_filter_validate_text_string', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment