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

  • does this function “acf_remove_local_field_group” actually work?

  • Just wanted to pop in here to say that acf_remove_local_field_group is working great for me in a child theme where the parent theme defines the group in PHP.

    You just have to put in a hook function that fires after acf loads like this:

    
    function myslug_remove_my_field_group() {
    	acf_remove_local_field_group( 'group_example_7384' );
    }
    add_action( 'acf/init', 'myslug_remove_my_field_group' );
    
  • also… don’t run acf_get_local_store( 'groups' )->remove( $key ) directly like some other post here suggests. This is literally exactly what acf_remove_local_field_group does at the moment if you look in the code.

    acf_get_local_store is not meant to be run directly, I don’t think.

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

The topic ‘Function to delete groups added programatically’ is closed to new replies.