Support

Account

Home Forums General Issues can not create fields with php

Solving

can 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 ?

  • Field keys must start with “field_”, group keys must start with “group_”. https://www.advancedcustomfields.com/resources/register-fields-via-php/ Not using the correct naming requirements on fields will can cause issues.

  • I tried this and it didn’t work out. I think something is wrong with my action, is my action ok ?

  • Other than the key values you’re using I don’t see anything wrong with the filter.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.