Support

Account

Home Forums ACF PRO Exlude fields in acf_form() function

Solved

Exlude fields in acf_form() function

  • Hello,
    how can i exclude specific fields from the acf_form() function?

    I think there is no option for that.

    Did anybody know how to “fix” this issue?

    Thanks in advance!
    Manu

  • There is not an “exclude” fields feature. It does let you list the specific fields that you want shown, so you could use a list of fields minus the ones you don’t want.

  • Can you give me a little example please?

  • 
    $options = array(
      'fields' => array(
        'field_1234567890abc',
        'field_69743bc7377e2',
        // etc
      )
    );
    acf_form($options);
    
  • So simple! Thank you very much!

  • 2 questions following up on this:

    1. It has been announced in 2013 already that a ‘back-end’ only option would become available (in this topic). Am I unaware of this option ? If not, would you know the status of this ? Don’t know how your ‘connection’ to @elliot is…

    2. Is it (easily) possible to get an array of all field_keys for a certain field group ? And then just remove the keys you don’t want from this array.

    I think it’s easier to ‘remove’ (for example) 3 values than to list 20 fields which are allowed. Less fields = less possible errors.

  • 1) I don’t know the status of this. (for anyone that’s interested) My connection is that I answer questions here on this forum, I sometimes report bug that I’ve confirmed through testing to E. I’m part of the support staff, but only as far as the forum is concerned.

    2) Elliot has added a new featured in 5.5 to the acf/prepare_field hook. If you return false from this filter then the field is not rendered. https://www.advancedcustomfields.com/resources/acfprepare_field/.

    I think that this feature is as far as something like the other topic might get. For example, you can add a filter to all fields, have a list of the field keys you don’t want displayed on the front end. See if you’re showing the front end form and if you are and the field is one you don’t want to show then return false.

    
    add_filter('acf/prepare_field', 'my_acf_admin_only_fields');
    function my_acf_admin_only_fields($field) {
      if (is_admin()) {
        return $field;
      }
      $fields = array(
        'field_123',
        'field_abc',
        'field_xyz'
      );
      if (in_array($field['key'], $fields)) {
        return false;
      }
      return $field;
    }
    
  • Thanks for the feedback… I understand the idea, but what I’m mainly looking for is to get rid of the manual definition of field id’s since I feel it’s quite error prone. A typo is easily made. I’d like to make that generic/variable.

    I was thinking in the line of something like below. The functions might be wrong, but I think the idea is clear

    
    function get_field_ids( $field_group_id ) {
        $all_fields = get_field_object();
        $field_keys = array();
        foreach ( $all_fields as $field ) {
            $field_keys[] = $field['key'];
        }
        return $field_keys;
    }
    

    Then you can remove only the fields you don’t need from this array.
    It’s easily forgotten to add a new field ID, when you add fields to the field group.

    But I understand that it’s not possible (yet).

  • Yes, this can work.

    In this plugin I added my own field setting. Before ACF 5.5 it uses an ACF filter to remove fields from the display. After ACF 5.5 it uses the acf/prepare_field filter to remove fields.

    https://github.com/Hube2/acf-user-role-field-setting

    There are also some internal functions in ACF that can be used to get field groups for a specific post as well as the fields in those groups. For example

    
    $groups = acf_get_field_groups(array('post_id' => $post_id);
    

    that will give you groups on a page

    
    $fields = acf_get_fields($group['key']);
    

    I think will give you an array of fields in a groups…. but you’ll need to look in the ACF code to make sure how to use these.

    However, this can’t be used to remove fields from a group. In order to not show fields by removing them from a group the fields need to be removed when the field group is loaded/created

  • Thanks, will investigate further.

  • Got it….

    
    $group      = get_page_by_title( 'ACF Group name', OBJECT, 'acf-field-group' );
    $fields     = array();
    $fields     = acf_get_fields( $group->ID );
    
    // array of excluded field keys
    $excluded_fields = array(
        'field_584c779a0114c',  
        'field_584c77690114b',  
        'field_584c67a239f35'   
    );
    
    $field_keys = array();
    if ( $fields ) {
        foreach ( $fields as $field ) {
            if ( ! in_array( $field['key'], $excluded_fields ) ) {
                $field_keys[] = $field['key'];
            }
        }
    }
    // $fields_keys is an array with 'allowed' field keys which can be 'fed' into acf_form
    
Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.