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.

1 Answer
1

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.

Leave a Reply

Your email address will not be published. Required fields are marked *