I’ve tried with disabling all other plugins, the fields remain enabled. Since I’m developing my own plugin that uses ACF (and GravityForms) as dependencies, I really only have those plugins installed on an otherwise vanilla WP install in a Local machine on my Mac (using Local by Flywheel). As it is a purely dev site I’m currently just using the Twenty Seventeen theme. I can try switching themes, and will have a different theme in place (along with lots else) on the real site once my plugin is developed, but I don’t expect a default WP theme to get in the way of ACF.
To test I commented out the loop and if check, so my code was:
function tgmembership_modify_acf_field_on_load( $field ) {
// get plugin settings
$options = get_option( 'tgmembership_options' );
// get names of fields that should be read-only
$readonly_fields = array_map('trim', explode(",", $options["acf_readonly"]));
// loop each readonly slug and set the disabled flag on those fields
//foreach ($readonly_fields as $name) {
//if ($field["name"] == $name) {
$field['disabled'] = 1;
// }
//}
// return the field
return $field;
}
add_filter('acf/load_field', 'tgmembership_modify_acf_field_on_load', 16);
It boils down to being the same as your test. It still left the fields enabled. I set Xdebug breakpoints on the $field['disabled'] = 1;
and return $field;
lines. These let me verify that indeed disabled
was getting set to 1
. Yet still the fields are enabled on the User Profile page. I believe you that your test disabled the fields. I just don’t understand why the same isn’t happening in my code…
The fields that I’ve tried this with are just simple text fields.
Ensuring that the filter priority was more than 10 didn’t fix the problem. Here is the full code for the filter:
/**
* Modify fields on load based on our advanced settings
*/
function tgmembership_modify_acf_field_on_load( $field ) {
// get plugin settings
$options = get_option( 'tgmembership_options' );
// get names of fields that should be read-only
$readonly_fields = array_map('trim', explode(",", $options["acf_readonly"]));
// loop each readonly slug and set the disabled flag on those fields
foreach ($readonly_fields as $name) {
if ($field["name"] == $name) {
$field['disabled'] = 1;
}
}
// return the field
return $field;
}
add_filter('acf/load_field', 'tgmembership_modify_acf_field_on_load', 16);
I really think that ACF just isn’t respecting the disabled
flag when adding fields to the User Profile. If that is the case then maybe I just can’t use ACF for the fields I want read-only (there are a few others I need to do manually anyway as they’re more complex, so that wouldn’t be a real problem). But I’d consider that a bug in ACF.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.