Support

Account

Home Forums Backend Issues (wp-admin) Filter acf/load_field to Disable Fields not working in User Profile

Solving

Filter acf/load_field to Disable Fields not working in User Profile

  • I have a few fields that I’m trying to make read-only on the User Profile. These are fields that are part of a custom member management system I’m working on for a client, and are largely old notes and old user ids from the old member management system, so not things that need to change. The problem is that when I use the acf/load_field filter to set these fields as disabled, the fields don’t seem to get disabled. I use this code as the heart of my custom filter:

    
    if ($field["name"] == $name) { // $name comes from a foreach loop that loops an array of names of fields I want disabled
       $field['disabled'] = 1;
    }
    

    My code is being run, I have verified that using Xdebug breakpoints. Does ACF simply not respect the disabled flag for the User Profile? Is there some other way that I can make fields on the User Profile read-only? Any advice would be appreciated.

  • What is the priority of your filter? Try making it >10. Without more info that’s my best guess.

  • 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.

  • When ACF adds a field, it works the same no matter where it’s called. It doesn’t do anything different on a user profile. There are some types of fields that cannot be disabled, for example anything that uses select2. Does this happen with all field types? What type of fields are you using?

  • The fields that I’ve tried this with are just simple text fields.

  • I just did a quick test. Created a text field and added it to the user profile page and this disables it.

    
    add_filter('acf/load_field', 'test_load_field');
    function test_load_field($field) {
    	$field['disabled'] = 1;
    	return $field;
    }
    

    Try removing the loop and checking each field name. This should effectively disable every field.

  • 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…

  • There must be something else interfering with it. Maybe a filter in your theme of in another plugin. Try disabling all other plugins and switching themes to see if the problems clears up.

  • 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.

  • My test site has 2017 installed and just ACF, so I’m pretty much stumped.

  • I can see that the initial document being loaded in the admin panel shows the field I want disabled as actually disabled with full semantics:

    <input type="text" id="acf-field_5e4bf7cb12333-field_5e4bf7f212334-row-0-field_5e4bf8e320b2f" name="acf[field_5e4bf7cb12333][field_5e4bf7f212334][row-0][field_5e4bf8e320b2f]" disabled="disabled"/>

    However, it appears as though some JavaScript is removing this prop configuration after the document is fully loaded. I haven’t tracked down the offending JavaScript, yet.

    If I had to guess, this goes for the others in this thread. This is also on a latest/greatest WordPress instance with minimal plugins loaded. I don’t have anything being enqueued in the admin panel that would be doing this, to my knowledge.

    Edit

    Ok, got it:

    prop (jquery.js?ver=5.3.2:7623)
    access (jquery.js?ver=5.3.2:3990)
    prop (jquery.js?ver=5.3.2:7590)
    d (acf-input.min.js?ver=5.8.7:formatted:659)
    (anonymous) (acf-input.min.js?ver=5.8.7:formatted:668)
    each (jquery.js?ver=5.3.2:368)
    each (jquery.js?ver=5.3.2:157)
    i.enable (acf-input.min.js?ver=5.8.7:formatted:666)
    enable (acf-input.min.js?ver=5.8.7:formatted:1762)
    showEnable (acf-input.min.js?ver=5.8.7:formatted:1774)
    show (acf-input.min.js?ver=5.8.7:formatted:4069)
    render (acf-input.min.js?ver=5.8.7:formatted:4066)
    onNewField (acf-input.min.js?ver=5.8.7:formatted:4012)
    u (acf-input.min.js?ver=5.8.7:formatted:987)
    i (acf-input.min.js?ver=5.8.7:formatted:920)
    i.doAction (acf-input.min.js?ver=5.8.7:formatted:399)
    acf.newField (acf-input.min.js?ver=5.8.7:formatted:1815)
    acf.getField (acf-input.min.js?ver=5.8.7:formatted:1886)
    (anonymous) (acf-input.min.js?ver=5.8.7:formatted:1894)
    each (jquery.js?ver=5.3.2:368)
    each (jquery.js?ver=5.3.2:157)
    acf.getFields (acf-input.min.js?ver=5.8.7:formatted:1893)
    r (acf-input.min.js?ver=5.8.7:formatted:1917)
    u (acf-input.min.js?ver=5.8.7:formatted:987)
    i (acf-input.min.js?ver=5.8.7:formatted:920)
    i.doAction (acf-input.min.js?ver=5.8.7:formatted:399)
    (anonymous) (post.php?post=245&action=edit:3365)

    This is the call-stack I was able to grab after setting a break-point on the element being modify, regarding its disabled property.

    This is indeed happening after the document is loaded.

Viewing 11 posts - 1 through 11 (of 11 total)

The topic ‘Filter acf/load_field to Disable Fields not working in User Profile’ is closed to new replies.