Skip to content

Instantly share code, notes, and snippets.

@yanknudtskov
Created June 25, 2020 14:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yanknudtskov/5790e27670f33a1175ab392eb1581765 to your computer and use it in GitHub Desktop.
Save yanknudtskov/5790e27670f33a1175ab392eb1581765 to your computer and use it in GitHub Desktop.
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