Support

Account

Home Forums Backend Issues (wp-admin) Dynamically set image field to readonly/disabled?

Solving

Dynamically set image field to readonly/disabled?

  • I’m trying to use the acf/load_field callback to dynamically set some fields to readonly and/or disabled. This works fine for most fields, but doesn’t appear to work for image fields. Is it possible to dynamically block image fields from being changed in the backend?

    
        add_filter('acf/load_field/name=test_image', function($field) {
            $field['readonly'] = 1;
            $field['disabled'] = 1;
            return $field;
        });
    
    
  • Not by using that filter. Those settings only work for simple fields. I do not know the details but this is probably something that you’d need to do using JavaScript. To be honest I’m not even sure if that’s possible.

  • A solution I came up for this is to add a class to the field and then use CSS to hide the edit/action buttons. e.g.

    add_filter( 'acf/prepare_field', 'marc_acf_prepare_field');
    function marc_acf_prepare_field($field){
        
        $screen = get_current_screen();
        // If we are editing a page in the admin
        if (is_admin() && $screen->parent_base == 'edit'){
    
            if (Whatever your condition for disabling the field is)
    
                    $field['disabled'] = true;
                    $field['readonly'] = true;
                    $field['wrapper']['class'] .= ' custom-class-disabled-field';
            }
        }
        return $field;
    }
    
    .custom-class-disabled-field .acf-actions{
        display:none !important;
    }
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.