Support

Account

Home Forums General Issues update_field() doesn’t store value in repeater field

Solving

update_field() doesn’t store value in repeater field

  • I have a repeater that has a read-only field that has a shortcode with an unique id. I need this ID, so I can have shortcodes that can output the other values in the repeater row.

    This is the (summarized) way I add fields:

    
    acf_add_local_field_group(
        'key' => 'pricing_group',
        'title' => 'Pricing',
        'fields' => [
            'key' => 'pricing_user_monthly_fees',
            'label' => 'User Monthly Fees',
            'name' => 'user_monthly_fees',
            'type' => 'repeater',
            'instructions' => 'Please create different fee ranges depending on the amount of users the customer wants to sign-up for.',
            'layout' => 'table',
            'button_label' => 'Add Range',
            'sub_fields' => [
                [
                    'key' => 'pricing_user_monthly_fees_minimum',
                    'label' => 'Minimum',
                    'name' => 'minimum',
                    'type' => 'number',
                    'instructions' => 'Please enter a minimum value for this range.',
                    'wrapper' => [ 'width' => '20' ],
                    'min' => 0,
                    'parent_repeater' => 'pricing_user_monthly_fees',
                ],
                [
                    'key' => 'pricing_user_monthly_fees_maximum',
                    'label' => 'Maximum',
                    'name' => 'maximum',
                    'type' => 'number',
                    'instructions' => 'Please enter a maximum value for this range.',
                    'wrapper' => [ 'width' => '20' ],
                    'min' => 0,
                    'parent_repeater' => 'pricing_user_monthly_fees',
                ],
                [
                    'key' => 'pricing_user_monthly_fees_fee',
                    'label' => 'Fee',
                    'name' => 'fee',
                    'type' => 'number',
                    'instructions' => 'Please enter a fee for this range.',
                    'wrapper' => [ 'width' => '20' ],
                    'parent_repeater' => 'pricing_user_monthly_fees',
                ],
                [
                    'key' => 'pricing_user_shortcode',
                    'label' => 'Shortcode',
                    'name' => 'shortcode',
                    'type' => 'text',
                    'readonly' => 1,
                    'instructions' => 'When you save this range, a shortcode is generated as a read-only field. You can use this shortcode to display the range\'s fee.',
                    'wrapper' => [ 'width' => '40' ],
                    'parent_repeater' => 'pricing_user_monthly_fees',
                ],
            ],
        ],
        'location' => [
            [
                [
                    'param' => 'options_page',
                    'operator' => '==',
                    'value' => 'acf-options-pricing',
                ],
            ],
        ],
    );
    

    This is the function that generates a shortcode with an unique id:

        
    public function store_unique_shortcode(string $post_id)
        {
            $screen = get_current_screen();
    
            if ($post_id !== 'options'
                && !isset($screen->id)
                && !strpos($screen->id, 'acf-options-pricing')) { return; }
    
            $field = get_field('pricing_user_monthly_fees', $post_id);
    
            if (!$field) { return; }
    
            $user_monthly_fees = array_map(function($user_monthly_fee) {
                return array_merge($user_monthly_fee, [
                    'shortcode' => $user_monthly_fee['shortcode'] ?:
                        sprintf('[pricing_user_monthly_fee id="%s"]', uniqid()),
                    ]);
            }, $field);
    
            update_field('pricing_user_monthly_fees', $user_monthly_fees, $post_id);
        }
    

    The problem is that when I use get_field() it shows an empty shortcode value while I expect it to contain the shortcode string with a unique id.

    If I dump $field I get the following:

    
    array(1) {
      [0]=>
      array(4) {
        ["minimum"]=>
        string(1) "0"
        ["maximum"]=>
        string(1) "3"
        ["fee"]=>
        string(4) "18.5"
        ["shortcode"]=>
        string(0) ""
      }
    }
    

    If I dump $user_monthly_fees I get the array with the shortcode and the generated unique id.

    
    array(1) {
      [0]=>
      array(4) {
        ["minimum"]=>
        string(1) "0"
        ["maximum"]=>
        string(1) "3"
        ["fee"]=>
        string(4) "18.5"
        ["shortcode"]=>
        string(50) "[pricing_user_monthly_fee id="646c94d8b29f2"]"
      }
    }
    

    If I dump right after update_field() I get an array with an empty shortcode value.

    
    array(1) {
      [0]=>
      array(4) {
        ["minimum"]=>
        string(1) "0"
        ["maximum"]=>
        string(1) "3"
        ["fee"]=>
        string(4) "18.5"
        ["shortcode"]=>
        string(0) ""
      }
    }
    

    Why doesn’t get_field() return the shortcode with the generated unique id?

  • I’m surprised you get anything at all using what you have posted.

    All group keys must start with “group_”
    All field keys must start with “field_”

    if your keys are actually like what you have posted then you will get unexpected results.

  • I’ve renamed my keys to group_ and field_, but that didn’t make a difference.

  • It’s interestingly enough related to the square brackets in the shortcode that I’m trying to store.

    The following works fine:

    
    'shortcode' => $user_monthly_fee['shortcode'] ?:
        sprintf('pricing_user_monthly_fee id="%s"', uniqid()),
    );
    

    While the following doesn’t store the shortcode:

    
    'shortcode' => $user_monthly_fee['shortcode'] ?:
        sprintf('[pricing_user_monthly_fee id="%s"]', uniqid()),
    );
    
  • I honestly don’t have any idea why.

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

You must be logged in to reply to this topic.