ACF does not create fields with php

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 acf 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 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
)

)

I think this array doesn’t have any problem. So where is the problem ?

Read more here:: ACF does not create fields with php

Leave a Reply

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