Home › Forums › General Issues › how to disable non-text fields, like a checkbox field › Reply To: how to disable non-text fields, like a checkbox field
ok, i’ve got something better :
the code looks like this :
/*
* utility function, to check if the field contains the class 'disable_acf'
* I added the class in the admin acf field panel, for each field i want to disable
*
*/
function is_acf_field_disabled($field) {
if (!isset($field['wrapper'], $field['wrapper']['class'])) {
return false;
}
$class = $field['wrapper']['class'];
if (!str_contains($class, 'disable_acf')) {
return false;
}
return true;
}
/*
* filter the fields when they render, to disable them
* acf have a way to do it for some fields
* for the others, add a property that will allow to use other filters on them
* and only if it was render first (so it will not affect server side field updates)
*
*/
function disable_acf_field($field) {
if (!is_acf_field_disabled($field)) {
return $field;
}
$type = $field['type'];
if (in_array($type, array('text', 'textarea', 'number', 'email', 'url', 'password', 'date_picker', 'time_picker', 'select'))) {
$field['disabled'] = 1;
}
else if (in_array($type, array('checkbox', 'radio'))) {
if (!isset($field['choices'])) {
return $field;
}
$choices = $field['choices'];
$to_disable = array_keys($choices);
// acf cast the strings, so do i
// line 128 : advanced-custom-fields/includes/fields/class-acf-field-radio.php
$to_disable = array_map(function($e){return (string)$e;}, $to_disable);
$field['disabled'] = $to_disable;
}
else {
$field['client_side_disabled'] = 1;
}
return $field;
}
add_filter('acf/load_field', 'disable_acf_field');
/*
* add 'inert' attribute to the wrapper of the field
* it prevents all focus actions, keyboard and mouse
* but it does not prevents to submit values, instead of 'disabled' attribute
*
*/
function change_acf_field_wrapper($wrapper, $field) {
if (!isset($field['client_side_disabled'])) {
return $wrapper;
}
error_log("+field: " . json_encode($field));
$wrapper["inert"] = "true";
return $wrapper;
}
add_filter('acf/field_wrapper_attributes', 'change_acf_field_wrapper', 10, 2);
/*
* if the field has property 'client_side_disabled'
* don't save it
* it does not prevent updates with update_field() on server side
*
*/
function disable_other_acf_field($null, $value, $post_id, $field) {
if (isset($field['client_side_disabled'])) {
return false;
}
return $null;
}
add_filter('acf/pre_update_value', 'disable_other_acf_field', 10, 4);
I think it’s better
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.