Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Remove ACF validation for administrators
<?php
// Disable Ready Only for Administrators
// add_filter( 'acf/load_field', 'yanco_acf_disable_read_only', 10000 );
function yanco_acf_disable_read_only( $field ) {
// Applies to administrators
if( is_admin() && get_post_type() == 'afrapportering' ) {
global $pagenow;
if( $pagenow !== 'post-new.php' ) {
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
if( yanco_user_has_role( $user_id, 'administrator' ) ) {
$field['required'] = false;
$field['readonly'] = false;
}
}
}
return $field;
}
add_action('acf/input/admin_head', 'yanco_acf_admin_head');
function yanco_acf_admin_head() {
if( is_admin() && get_post_type() == 'afrapportering' ) {
global $pagenow;
if( $pagenow !== 'post-new.php' ) {
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
if( yanco_user_has_role( $user_id, 'administrator' ) ) {
echo '<script type="text/javascript">
window.acf.validation.active = false;
</script>';
}
}
}
}
add_action('acf/validate_save_post', 'yanco_acf_validate_save_post', 10, 0);
function yanco_acf_validate_save_post() {
if( is_admin() && get_post_type() == 'afrapportering' ) {
global $pagenow;
if( $pagenow !== 'post-new.php' ) {
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
if( yanco_user_has_role( $user_id, 'administrator' ) ) {
acf_reset_validation_errors();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.