Home › Forums › General Issues › 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()),
);
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.