Created
March 28, 2019 23:07
-
-
Save acobster/b03b7b2c1a95cb9e761d0f6d862dfe87 to your computer and use it in GitHub Desktop.
Custom ACF select validation
This file contains hidden or 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 | |
/* | |
* 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