Home › Forums › Add-ons › Options Page › Dynamically add message based on another field value
Hi,
I am trying to dynamically populate a Message field based on another field in a Repeater.
But I always get white screen with my code, the_row(): is added.
The debug info says
PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 20480 bytes) in /home/xxxx/webapps/xxx/wp-includes/class-wp-list-util.php on line 134
This works:
function cban_shortcode_disable_field($field) {
$field['disabled'] = false;
//$field['readonly'] = true;
$field['message'] = 'ALMA';
print_r($field);
return $field;
}
add_filter('acf/load_field/name=canb_shortcode', 'cban_shortcode_disable_field',20,1);
But if I try to load the message dynamically I got white screen:
add_filter('acf/load_field/name=canb_shortcode', 'canb_shortcode_load_field');
function canb_shortcode_load_field($field){
// if has rows
if( have_rows('canb_notification_bars', 'option') ) :
// while has rows
while( have_rows('canb_notification_bars', 'option') ) : the_row();
/* Get the name from the Notification bar name field */
$canb_name = get_sub_field('canb_notification_bar_name','option');
$canb_name = sanitize_title_with_dashes( $canb_name );
$field['message'] = $canb_name;
endwhile;
endif;
return $field;
}
Can someone point me in the right direction?
I have tried to use Example 2 from this page:
https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/
I want to create a small plugin and in the message I would like to display a shortcode variable which the user can use to embed the notification bar.
Thanks.
Regards,
Peter.
remove ‘option’ from here
this
$canb_name = get_sub_field('canb_notification_bar_name','option');
should be
$canb_name = get_sub_field('canb_notification_bar_name');
I’m not sure that is causing your error, but that’s the only thing I can see wrong. For some reason your code is causing an infinite loop.
Also, you should be using the acf/prepare_field hook to modify the field message. While acf/load_field can be used here it can have unintended side effects.
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.