0 Replies - 860 Views - Last Post: 11 June 2018 - 07:29 AM

#1 ratonhnaketon   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 07-January 16

Can not create fields with code in ACF

Posted 11 June 2018 - 07:29 AM

I want to create some custom fields based on my taxonomy terms, like creating a custom field for every term of a taxonomy. I use advanced custom fields plugin for this and I'm trying to create fields with php codes since my taxonomy terms might change. This is the code I use:

function af_custom_prices_fields() {


    // Get second level services
    $whole_services  = get_terms( 'service', array( 'hide_empty' => false ) );
    $second_services = array_filter( $whole_services, function ( $t ) {
        # This term has a parent, but its parent does not.
        return $t->parent != 0 && get_term( $t->parent, 'service' )->parent == 0;
    } );

    # create an empty array for fields
    $services_fields = array();

    # create fields based on services taxonomy terms
    foreach ( $second_services as $service ) {
        $services_field = array(
            'key'               => 'prices-' . $service->term_id,
            'label'             => 'price for ' .  $service->name ,
            'name'              => 'prices_' . $service->term_id,
            'type'              => 'text',
            'prefix'            => '',
            'instructions'      => '',
            'required'          => 0,
            'conditional_logic' => 0,
            'wrapper'           => array(
                'width' => '',
                'class' => '',
                'id'    => '',
            ),
            'default_value'     => '',
            'placeholder'       => '',
            'prepend'           => '',
            'append'            => '',
            'maxlength'         => '',
            'readonly'          => 0,
            'disabled'          => 0,
        );
        array_push( $services_fields, $services_field );
    }

    # create acf fields
    acf_add_local_field_group( array(
        'key'                   => 'prices',
        'title'                 => 'Prices',
        'fields'                => $services_fields,
        'location'              => array(
            array(
                array(
                    'param'    => 'post_type',
                    'operator' => '==',
                    'value'    => 'barbershop',
                ),
            ),
        ),
        'menu_order'            => 0,
        'position'              => 'normal',
        'style'                 => 'default',
        'label_placement'       => 'top',
        'instruction_placement' => 'label',
        'hide_on_screen'        => '',
    ));
}

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


But this function is not creating the group field or fields at all, I printed the $services_fields variable and this was the result:

Array
(
    [0] => Array
        (
            [key] => prices-15
            [label] => price for اصلاح مو
            [name] => prices_15
            [type] => text
            [prefix] => 
            [instructions] => 
            [required] => 0
            [conditional_logic] => 0
            [wrapper] => Array
                (
                    [width] => 
                    [class] => 
                    [id] => 
                )

            [default_value] => 
            [placeholder] => 
            [prepend] => 
            [append] => 
            [maxlength] => 
            [readonly] => 0
            [disabled] => 0
        )

    [1] => Array
        (
            [key] => prices-14
            [label] => price for اکستنشن مو
            [name] => prices_14
            [type] => text
            [prefix] => 
            [instructions] => 
            [required] => 0
            [conditional_logic] => 0
            [wrapper] => Array
                (
                    [width] => 
                    [class] => 
                    [id] => 
                )

            [default_value] => 
            [placeholder] => 
            [prepend] => 
            [append] => 
            [maxlength] => 
            [readonly] => 0
            [disabled] => 0
        )

    [2] => Array
        (
            [key] => prices-12
            [label] => price for رنگ ناخن
            [name] => prices_12
            [type] => text
            [prefix] => 
            [instructions] => 
            [required] => 0
            [conditional_logic] => 0
            [wrapper] => Array
                (
                    [width] => 
                    [class] => 
                    [id] => 
                )

            [default_value] => 
            [placeholder] => 
            [prepend] => 
            [append] => 
            [maxlength] => 
            [readonly] => 0
            [disabled] => 0
        )

    [3] => Array
        (
            [key] => prices-13
            [label] => price for کاشت ناخن
            [name] => prices_13
            [type] => text
            [prefix] => 
            [instructions] => 
            [required] => 0
            [conditional_logic] => 0
            [wrapper] => Array
                (
                    [width] => 
                    [class] => 
                    [id] => 
                )

            [default_value] => 
            [placeholder] => 
            [prepend] => 
            [append] => 
            [maxlength] => 
            [readonly] => 0
            [disabled] => 0
        )

)


This variable sounds to be right, so where is the problem ?

Is This A Good Question/Topic? 0
  • +

Page 1 of 1