Skip to content

Instantly share code, notes, and snippets.

@acobster
Created March 28, 2019 23:07
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 acobster/b03b7b2c1a95cb9e761d0f6d862dfe87 to your computer and use it in GitHub Desktop.
Save acobster/b03b7b2c1a95cb9e761d0f6d862dfe87 to your computer and use it in GitHub Desktop.
Custom ACF select validation
<?php
/*
* Disallow certain choices based on post_type and/or page template
*/
add_filter('acf/prepare_field/key=field_5c6c913e0ea64', function(array $field) {
//unset($field['choices']['full-width-image-carousel']);
//unset($field['choices']['full-width-image']);
return $field;
});
add_filter('acf/validate_value/key=field_5c6c913e0ea64', function(bool $valid, $value, $field, $input) {
$post = new Timber\Post($_POST['post_id'] ?? $_POST['ID']);
if (!in_array($value, ['full-width-image', 'full-width-image-carousel'])) {
return $valid;
}
if ( $post->post_type === 'page' ) { // or whatevs
$valid = "Full-width image carousels are not allowed on pages using the default General Interior template.";
}
return $valid;
}, 10, 4);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment