Skip to content

Instantly share code, notes, and snippets.

@swoboda
Created April 11, 2018 16:27
Show Gist options
  • Save swoboda/aae2c48aeda05677b029ae17fad884d0 to your computer and use it in GitHub Desktop.
Save swoboda/aae2c48aeda05677b029ae17fad884d0 to your computer and use it in GitHub Desktop.
Custom URL Validation for WooCommerce Flexible Checkout Fields
<?php
// Do NOT include the opening php tag
/**
* Function to validate the URL
*
*/
function wpdesk_fcf_validate_url( $field_label, $value ) {
if ( filter_var( $value, FILTER_VALIDATE_URL ) == false ) {
wc_add_notice( sprintf( '%s is not a valid URL.', '<strong>' . $field_label . '</strong>' ), 'error' );
}
}
add_filter( 'flexible_checkout_fields_custom_validation', 'wpdesk_fcf_custom_validation_url' );
/**
* Add custom URL validation
*
*/
function wpdesk_fcf_custom_validation_url( $custom_validation ) {
$custom_validation['url'] = array(
'label' => 'URL',
'callback' => 'wpdesk_fcf_validate_url'
);
return $custom_validation;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment