Support

Account

Home Forums Feature Requests Function to delete groups added programatically

Solved

Function to delete groups added programatically

  • On line 626, the function acf_delete_field_group() makes this check:

    
    // Bail early if field group was not found.
    if( !$field_group || !$field_group['ID'] ) {
    	return false;
    }
    

    However, when the group is added programatically with acf_add_local_field_group(), the ID is 0, so it bails at that point.

    By checking the rest of the code, I understand it bails because it expects the group to be saved on DB, but, it would be nice to have a way to delete a group added programatically.

    Usage Scenario: Writing tests, where I have to register groups, fields, make assertions, and be able to delete them to keep the test scope clean for the next test.

  • I was able to achieve that like this:

    
    acf_remove_local_field_group( 'group_foo' );
    acf_get_store( 'field-groups' )->remove( 'group_foo' );
    
  • Actually, to remove fields too:

    
    $group_key = 'group_foo';
    
    $fields = acf_get_local_fields( $group_key );
    
    foreach ( $fields as $field ) {
    	acf_remove_local_field( $field['key'] );
    	acf_get_local_store( 'fields' )->remove( $field['key'] );
    }
    
    acf_remove_local_field_group( $group_key );
    acf_get_store( 'field-groups' )->remove( $group_key );
    
  • Sorry, I’m confused about why you would do this. Why would you delete a group that was added through code, why wouldn’t you just remove the code and not add it in the first place?

  • I work on a company that has its own framework for working with WordPress.

    One of the concepts it has is a facade pattern for handling meta data such as those provided by ACF. This means we can theoretically change from ACF to CMB2 in any project at any given time, and vice-versa.

    What I’m doing is writing integration tests for this facade layer, thus I need to create, insert and get data from, then delete multiple groups and fields during runtime.

    Hope I didn’t suck at explaining,
    Lucas

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

You must be logged in to reply to this topic.

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.