Support

Account

Home Forums ACF PRO Disable specific fields within admin

Solved

Disable specific fields within admin

  • Hey there,

    I’m hoping someone might be able to help me out there. I’m working on project with ThreeWP Broadcast (https://wordpress.org/plugins/threewp-broadcast/) where a master site will be setup by the client and will push down ACF fields with data to a bunch of child sites. However, I want specific master ACF fields (typically the first set in a repeater) to be disabled on the child sites. So, we’re trying to figure out a way to hook into the fields to disable them as needed via a plugin that is active on the child sites.

    So far, I’ve figured out that we can use the ‘acf/input/admin_head’ action and currently have something like this to hide a specific field within a repeater:

    acf.add_action('ready append', function($el){
      acf.get_fields().each(function(){
        if($(this).data('name') == 'intro_text'){
          $(this).hide();
        }
      });
    });

    However, we run into a few issues:

    1) When the user adds a new group, the field is hidden since ACF clones from above (so it seems).

    2) We’d preferably like to disable fields, not hide them.

    Since there is lack of documentation around this so far, we’re having issues figuring out exactly where to go from here to target the first group of fields in a repeater or a specific group of fields and further disable them (or even if it is possible to disable certain fields like a WYSIWYG field).

    Any insight, help or links that assist working through the jQuery options and functions would be greatly appreciated.

    Thanks and don’t hesitate to let me know if you need any further explanation.

    Much appreciated!

  • Hi @KornDev

    Looking at your code, I can see that you are searching for all fields each time a new repeater row is appended to the page. To get around this issue, please make use of the ‘$el’​ parameter​​​:

    
    acf.get_fields('', $el).each(function(){
    

    If you only want wysiwyg fields, try this:

    
    acf.get_fields({ type : 'wysiwyg'}, $el).each(function(){
    
  • Thanks, Elliot. This is helpful. What are the other parameters we can use here? For example, can we target a specific field by name or all fields within a specific repeater?

    To get some insight, here is our current code that works well, but not sure if it is the best way about it. For example, if you decide to change the class names (which you did in a recent update, this approach will break):

    acf.add_action('load', function($el){
              // Loop through each ACF field
              acf.get_fields().each(function(){
    
                // Specific for Repeaters
                var table = $(this).parents('.acf-table-wrap'), // Get the table wrapper for the repeater
                    row = table.parent('.acf-row'), // Get the acf-row containing everything
                    hidden = table.find('[data-name="<?php echo self::UNIQUE_ID_FIELD_NAME; ?>"] input'); // Get the unique_id hidden field
                // Check if the unique_id field has a value
                if(hidden.length != 0 && hidden.val().length != 0){
                  // We are looking at a parent item since the unique_id is set
                  var remove_row = row.find('.acf-repeater-remove-row'),
                      input = $(this).find('.acf-input'), // Get the input wrapper for the field
                      field_name = $(this).data('name'),
                      field_type = $(this).data('type'),
                      field = null,
                      data = null;
    
                  if(field_type == 'wysiwyg'){
                    field = input.find('.acf-editor-wrap');
                    data = field.find('textarea').val();
                  } else {
                    switch(field_name){
                      case '<?php echo self::ACF_TOPIC_TITLE; ?>':
                        field = input.find('input');
                        data = field.val();
                        break;
                    }
                  }
    
                  if(field != null){
                    // Hide the entire WYSIWYG field
                    field.hide();
                    // Hide remove icon/ability
                    remove_row.css('visibility', 'hidden');
                    // Display the data from the WYSIWYG field
                    input.append(data);
                  }
                }
                // End Repeaters
    
              });
            });
  • Hi @KornDev

    Using the get_fields parameter {type: 'repeater'} will always work through all versions of ACF PRO even if the class structure changes.

    You can also target via name or key:
    {name: 'test'}
    {key: 'field_1234'}

    Hope that helps

  • Thanks, @Elliot

    One more question. If we use {type: ‘repeater’} is there then an easy way to loop through each of the repeater rows/objects so we’re are relying on the classes to find things?

  • Hi @korndev

    You can loop over the rows by using jquery to find ‘.acf-row’

    You can also find all sub fields by using the acf.get_fields function. Please note the second parameter is for a jQuery object to confine the search within. ie. if you pass the repeater jQuery object as this parameter, acf will return only fields found within this repeater object

  • Thanks. That’s what I’ve been doing, just thought there might be a way that didn’t relay on the DOM objects. Reason being, as we were working on this, I think I was accessing something where the class name changed in a recent update you pushed out and obviously things stopped working as expected. We found the change and all went back to normal, but this isn’t ideal.

    Appreciate the responses and we’ll continue to push forward and hope you don’t change the class names again. 🙂

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

The topic ‘Disable specific fields within admin’ is closed to new replies.