2

This is the code i exported from the ACF when i tried to create a field via admin panel :

function af_barbershop_address_field() {
    if(function_exists("register_field_group"))
    {
        register_field_group(array (
            'id' => 'acf_address',
            'title' => 'Address',
            'fields' => array (
                array (
                    'key' => 'field_address',
                    'label' => 'address',
                    'name' => 'address',
                    'type' => 'textarea',
                    'required' => 1,
                    'default_value' => '',
                    'placeholder' => 'type the address',
                    'maxlength' => '',
                    'rows' => '',
                    'formatting' => 'br',
                ),
            ),
            'location' => array (
                array (
                    array (
                        'param' => 'post_type',
                        'operator' => '==',
                        'value' => 'barbershop',
                        'order_no' => 0,
                        'group_no' => 0,
                    ),
                ),
            ),
            'options' => array (
                'position' => 'normal',
                'layout' => 'no_box',
                'hide_on_screen' => array (
                ),
            ),
            'menu_order' => 0,
        ));
    }
}

add_action( 'acf/init', 'af_barbershop_address_field' );

I also tried to include the acf.php file into my functions.php file :

$acf_url = WP_PLUGIN_DIR . '/advanced-custom-fields/acf.php';
include_once( $acf_url );

All codes are in my functions file. After exporting the code, i deleted the field that created via panel and tried to create the exact same field with code, but it’s not working. What is the problem ? I’ve been dealing with this problem since last week.

4
  • Is ACF still active? The code that's exported still requires ACF to be active. Commented Jun 19, 2018 at 11:02
  • Also, you might want to check acf_add_local_field_group() instead of register_field_group(). The latter one seems to be deprecated.
    – Kumar
    Commented Jun 19, 2018 at 11:06
  • @jacob-peattie Yes the plugin is still active.
    – Amirition
    Commented Jun 19, 2018 at 11:14
  • @sven I tried that, but it's not working either, that function is for PRO version of plugin. I think the function that plugin exports is the right function.
    – Amirition
    Commented Jun 19, 2018 at 11:15

1 Answer 1

4

Action acf/init is available for pro version only, and I think you forgot to mention that you're using the free version because with pro version above code worked fine.

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

So you need to modify your code to:

function af_barbershop_address_field() {
    if ( function_exists( "register_field_group" ) ) {
        register_field_group( array(
            'id'         => 'acf_address',
            'title'      => 'Address',
            'fields'     => array(
                array(
                    'key'           => 'field_address',
                    'label'         => 'address',
                    'name'          => 'address',
                    'type'          => 'textarea',
                    'required'      => 1,
                    'default_value' => '',
                    'placeholder'   => 'type the address',
                    'maxlength'     => '',
                    'rows'          => '',
                    'formatting'    => 'br',
                ),
            ),
            'location'   => array(
                array(
                    array(
                        'param'    => 'post_type',
                        'operator' => '==',
                        'value'    => 'barbershop',
                        'order_no' => 0,
                        'group_no' => 0,
                    ),
                ),
            ),
            'options'    => array(
                'position'       => 'normal',
                'layout'         => 'no_box',
                'hide_on_screen' => array(),
            ),
            'menu_order' => 0,
        ) );
    }
}

add_action( 'acf/register_fields', 'af_barbershop_address_field' );

This should work fine. This won't work for Pro version, only earlier code would work. So you might even hook to both the actions so that in case you happen to upgrade in future the code still works.

2
  • 2
    I think they should mention in the docs that every action or function is in the pro or free version, but they don't. Thank you.
    – Amirition
    Commented Jun 20, 2018 at 12:37
  • @Kumar, is this still the case? I'm able to use acf/init with the free version.
    – Motivated
    Commented Oct 5, 2022 at 23:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.