7

I've tried a bunch of different functions and approaches but so far I haven't been able to get it working. The goal is to add an Advanced Custom Field group to the backend of Wordpress with some PHP-code. In the best scenario we add the PHP-code to a method of a class.

public function create_group( $group_name ) {

    if ( $this->does_group_already_exists( $group_name ) ) {
        return false;
    }

    acf_add_local_field_group( array(
        'key'      => 'group_1',
        'title'    => 'My Group',
        'fields'   => array(
            array(
                'key'   => 'field_1',
                'label' => 'Sub Title',
                'name'  => 'sub_title',
                'type'  => 'text',
            )
        ),
        'location' => array(
            array(
                array(
                    'param'    => 'post_type',
                    'operator' => '==',
                    'value'    => 'post',
                ),
            ),
        ),
    ) );

    return true;
}

Nothing gets added with the code above. I also tried adding it to functions.php and it with a add_action() function like so:

add_action( 'acf/init', array( $this, 'create_group' ) );

But again, no results.

Hope some one can share a working solution.

Floris
  • 2,727
  • 2
  • 27
  • 47
  • check out https://www.advancedcustomfields.com/resources/register-fields-via-php/. If that doesn't help I usually add the field in the GUI on a local dev wordpress build, use the ACF tools to generate the export code and then modify that if needed and paste into your project. – mrben522 Jan 07 '20 at 14:26
  • Actually you can create code over ACF in the WP-Backend itself. But im not really sure if that is a pro function. Under Admin -> Custom Fields -> Tools -> Export -> Create PHP. – GDY Jan 07 '20 at 14:57

3 Answers3

5

Today I finally discovered a solution for adding a ACF group to the backend dynamically with PHP-code.

It can be done by adding a new post directly with the acf-field-group post type. Here is my implementation for those awesome people from the future that are interested:

public function create_form( $form_name ) {

    $new_post = array(
        'post_title'     => $form_name,
        'post_excerpt'   => sanitize_title( $form_name ),
        'post_name'      => 'group_' . uniqid(),
        'post_date'      => date( 'Y-m-d H:i:s' ),
        'comment_status' => 'closed',
        'post_status'    => 'publish',
        'post_type'      => 'acf-field-group',
    );
    $post_id  = wp_insert_post( $new_post );

    return $post_id;
}

Where $form_name is the name of the ACF group. It works. And there was no need for using a specific hook. I could just call this method directly.

Floris
  • 2,727
  • 2
  • 27
  • 47
  • 1
    `acf/init` works only with the Pro version of ACF. In the basic free version you must use `acf/register_fields`. [reference](https://wordpress.stackexchange.com/a/306492/23316 "wordpress.stackexchange") – Lenin Oct 16 '20 at 18:12
  • @Lenin, is this still the case? I'm able to use `acf/init` with the free version. – Motivated Oct 05 '22 at 23:01
  • @Floris, are you approaching this in a different way? Using the example you posted, it creates multiple ACF field groups with the same details rather than just the one. – Motivated Oct 05 '22 at 23:02
  • @Motivated I am not sure. I am a bit away from ACF for about an year now. – Lenin Mar 14 '23 at 14:02
0

Actually you can create such code over ACF in the WP-Backend itself (not sure if this only works in ACF Pro). Under Admin -> Custom Fields -> Tools -> Export -> Create PHP. The generated code is a great starting point for a programmatic ACF integration.

It should look something like this:

acf_add_local_field_group(array(
    'key' => 'group_5d146d18eeb92',
    'title' => 'My Group Title',
    'fields' => array(
        array(
            'key' => 'field_5d146d1f27577',
            'label' => 'My Field Title',
            'name' => 'my_field_name',
            'type' => 'true_false',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array(
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'message' => '',
            'default_value' => 0,
            'ui' => 1,
            'ui_on_text' => '',
            'ui_off_text' => '',
        ),
    ),
    'location' => array(
        array(
            array(
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'my_custom_post_type',
            ),
        ),
    ),
    'menu_order' => 0,
    'position' => 'side',
    'style' => 'default',
    'label_placement' => 'top',
    'instruction_placement' => 'label',
    'hide_on_screen' => '',
    'active' => true,
    'description' => '',
));

Check out the ACF page for registering fields via PHP.

GDY
  • 2,872
  • 1
  • 24
  • 44
  • Yes. I know about the export tool. But when I run code like this in my plugin it doesn't get added to the backend unfortunately. – Floris Jan 07 '20 at 15:45
0

Action acf/init is available for pro version only, maybe that was the reason it did not work at the first place..

For basic version you have to use acf/register_fields to register your custom fields.

Lenin
  • 570
  • 16
  • 36