Support

Account

Home Forums Add-ons Options Page Updating Values from changes on Options Page Repeater Field for Dynamic Populate

Solving

Updating Values from changes on Options Page Repeater Field for Dynamic Populate

  • Howdy,
    I am using the repeater fields on the Options Page for button templates using one of the fields as the label and I wrote custom code to input the rest of the repeater fields into the $value variable to dynamically fill the select fields on my page template, that part works well.

    The problem is when I go into the Options page and update the value later on, for example if I change the background color for a button, it doesn’t automatically update on the front-end for that button. When I go into the Select field on the page template it shows the value, but it isn’t saved into the database for that specific page until I save the changes to that page. Since I will have several pages where this page template is used this would mean every time I make a change on the repeater field on the Options page I will need to go into each individual page and update it. Any way to save the value into the database for each individual page when I save it so I don’t have to do that?

    If it helps this is within a Flexible Content field nested within a Repeater Field nested within a Flexible Content nested within a Repeater
    (Repeater –> Flexible Content –> Repeater –> Flexible Content)

    I am guessing it has something to do with Update Value, but what I have below in my code only works when I save (“update”) each individual page.

    Here is my code:

    //ACF Multiple ACF Fields to Populate Column Title Field Width Select Field
    function acf_load_button_choices( $field ) {
    	
        // reset choices
        $field['choices'] = array();
        
        // if has rows
        if( have_rows('button_options', 'option') ) {
    	    
    	    // while has rows
            while( have_rows('button_options', 'option') ) {
                
                // instantiate row
                the_row();
                
    			$button_background_option = get_sub_field('button_background_color');
                
                // vars
                $value = "background:" . $button_background_option . ';';
                
                $label = get_sub_field('button_template_label');
                
                // append to choices
                $field['choices'][$value]  = $label;
                
            }
            
        }
    
        // return the field
        return $field;
        
    }
    add_filter('acf/load_field/name=column_button_options', 'acf_load_button_choices');
    
    function acf_update_button_values( $value, $post_id, $field ) {
    	// if has rows
    	if( have_rows('button_options', 'option') ) {
        
        // while has rows
        while( have_rows('button_options', 'option') ) {
        the_row();
        
    	$button_background_option = get_sub_field('button_background_color');
    
    	$value = "background:" . $button_background_option . ';';
        return $value;
    		}
    	}
    }
    add_filter('acf/update_value/name=column_button_options', 'acf_update_button_values', 10, 3);
    

    Thanks!
    Tim

  • You can’t automatically update all the places and posts where previous values are stored. To do this you’d need to do a query to get every post where the value might be, loop through all of the posts, then go through all of the fields where it was selected, test them against the new value and update each individual post where is was saved. Might work on a small site, but this process would likely timeout your save if done on a large scale.

    Rather than generating the value that is selected you should just be selecting a setting and then dynamically generating the value when it needs to be output on the page.

    I know this may completely change the way the site works, and may not be possible.

    for example, generating the field choices

    
    //ACF Multiple ACF Fields to Populate Column Title Field Width Select Field
    function acf_load_button_choices( $field ) {
    	
      // reset choices
      $field['choices'] = array();
      
      // if has rows
      if( have_rows('button_options', 'option') ) {
    	  
    	  // while has rows
        while( have_rows('button_options', 'option') ) {
          
          // instantiate row
          the_row();
          
          $label = get_sub_field('button_template_label');
          
          // append to choices
          $field['choices'][$label]  = $label;
          
        }
        
      }
    
      // return the field
      return $field;
      
    }
    add_filter('acf/load_field/name=column_button_options', 'acf_load_button_choices');
    

    On the page where you want to use it I would do something like this

    
    // generate available button options
    if( have_rows('button_options', 'option') ) {
      
      // while has rows
      while( have_rows('button_options', 'option') ) {
        
        // instantiate row
        the_row();
        
      $button_background_option = get_sub_field('button_background_color');
        
        // vars
        $value = "background:" . $button_background_option . ';';
        
        $label = get_sub_field('button_template_label');
        
        // append to choices
        $buttons[$label] = $value;
        
      }
      
    }
    

    now where you use the value you could do something like this

    
    $option = get_field('column_button_options');
    if (isset($button[$option])) {
      echo $button[$option];
    }
    

    I’m sure there are better ways to do this as well.

  • Hi John,
    So your first piece of code still goes in functions.php like before? The second and third code sections go in my page template?

    Also, is $button supposed to be singular in the third chunk of code, I am trying to understand where you get “$buttons” from?

    Thanks,
    Tim

  • Yes, it should be $buttons in the third piece of code. For the rest, yes. The first part of the code is a modification of your function for loading choices. The second two need to go into your template where you’ll be using the buttons. Sorry if any of this is confusing, I was just trying to come up with a way that would avoid completely rebuilding something.

    Although this is going to change the operation of that field anyway and will still require a manual update of all the posts that use the field. But as long as you don’t change the “label” value in your options it will avoid needing to do similar manual updates if you change the values again at some point.

  • Got it, the method I was using before seems to do the same thing as what you are suggesting if I still need to do a manual update on each page. I will submit a support request.

  • @tjmorris81 @hube2 did either of you ever solve this issue? I am pretty much doing the same thing.

    I’ve got a repeater field on an options page, then I’m dynamically putting those values into a checkboxes choices in another field. This works perfectly. But for those values to then be displayed after changing then on the intended wordpress page I need to update those values in the second field after the first field is saved, I have to do this manually at the moment which isn’t good for a clients user experience.

    Look forward to any solutions and would appreciate any advice.

    DD

  • @davenoham, I was originally trying to build a drag and drop theme builder with this code and I gave up on the project because it became too complex. Instead I found Elementor and haven’t looked back. Sorry I can’t help you more.

  • no worries @tjmorris81 I’m using this to make a web to print website. It too is complex for my skill set!

  • Hi @tjmorris81,
    did you solved it ?
    I have the same issue when I update option field, it deletes my choice select on my page…
    Thank you.
    Best

  • Hey,

    what is it you’re trying to do? have you got a link to the site / code.
    I might be able to help as I ended up making the web to print project successfully

  • Hello @davenoham,
    thanks for your answer, it’s very kind to propose to help me on my project.
    Congratulations for your web to print project ;-).
    Here is the new thread with my issues :
    https://support.advancedcustomfields.com/forums/topic/populate-select-color-from-option-to-a-page/
    Best regards.
    Pierre

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

The topic ‘Updating Values from changes on Options Page Repeater Field for Dynamic Populate’ is closed to new replies.