Skip to content

Instantly share code, notes, and snippets.

@ethicka
Last active July 3, 2017 15:38
Show Gist options
  • Save ethicka/2d1205a536b56b3390a9033406a60845 to your computer and use it in GitHub Desktop.
Save ethicka/2d1205a536b56b3390a9033406a60845 to your computer and use it in GitHub Desktop.
ACF Validate Facebook Field
<?php
/*
* Validate Facebook URL field
* URL field must be named "facebook_video_url" or you can change is in the filter below.
* Source: https://www.advancedcustomfields.com/resources/acf-validate_value/
*/
function acf_validate_facebook_url( $valid, $value, $field, $input ){
// If already not valid, then exit
if( !$valid ) {
return $valid;
}
// Check if the $value is a Facebook video URL
// REGEX Source: https://gist.github.com/mul14/44579d0ab523ab33d9f0
$video = preg_match('/^http(?:s?):\/\/(?:www\.|web\.|m\.)?facebook\.com\/([A-z0-9\.]+)\/videos(?:\/[0-9A-z].+)?\/(\d+)(?:.+)?$/',$value);
if( !$video ) {
// If not value, then this error:
$valid = 'Must be a Facebook Video URL.';
}
// Return the result
return $valid;
}
add_filter('acf/validate_value/name=facebook_video_url', 'acf_validate_facebook_url', 10, 4);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment